Spring RMI 실현
22888 단어 스프링 프레임
서버 쪽 코드
<bean id="emailService" class="com.jackmouse.rmi.email.service.EmailService"/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="emailService">property>
<property name="serviceName" value="email">property>
<property name="serviceInterface" value="com.jackmouse.rmi.email.service.IEmailService"/>
<property name="registryPort" value="8888">property>
bean>
<bean id="emailService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
//
<property name="serviceUrl" value="rmi://127.0.0.1:8888/email"/>
<property name="serviceInterface" value="com.jackmouse.rmi.email.service.IEmailService"/>
<property name="lookupStubOnStartup" value="false"/>
<property name="refreshStubOnConnectFailure" value="true">property>
bean>
package packageofrmi;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import com.jackmouse.rmi.email.service.IEmailService;
/**
*
*/
public class TestClient {
public static void main(String[] args) {
String title = "rmi ";//
String password = "XXX";//
String to = "[email protected]";//
String from = "[email protected]";//
String content="123";//
ApplicationContext ctx =new ClassPathXmlApplicationContext("application.xml");
IEmailService emailService = (IEmailService)ctx.getBean("email");
System.out.println(emailService.send(from,password,title,content,to));
}
}
rmi 서버 가 원 격 서버 에 있 을 때 원 격 서버 에 여러 개의 네트워크 카드 가 있 으 면 특정한 ip 를 지정 해 야 합 니 다.서버 쪽 에 서 는 JAVA RMI 에서 ServletContextListener 모니터 의 하위 클래스 EmailServiceListener 를 만 들 고 contextInitialized 방법 에 다음 코드 를 기록 해 야 합 니 다.
System.setProperty("java.rmi.server.hostname","49.*.*.*");
그러나 spring 에 서 는 MethodInvoking Factory Bean 을 설정 하여 정적 주입 을 통 해 일부 시스템 설정 을 설정 할 수 있 기 때문에 application. xml 에 다음 과 같은 설정 을 추가 하면 상기 코드 의 역할 을 대체 할 수 있 습 니 다.
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<props>
<prop key="java.rmi.server.hostname" > ipprop>
props>
property>
bean>
\ # {@ systemProperties} 은 java. lang. System. getProperties () 방법 으로 되 돌아 오 는 Properties 대상 과 같 습 니 다.이 설정 의 통속 적 인 이해: bean 의 생명주기 초기 화 단계 에서 SpEL 표현 식 을 통 해 systemProperties 의 bean 을 참조 하고 MethodInvoking Factory Bean 을 통 해 bean 을 참조 하 는 putAll 방법 을 호출 하여 설정 한 속성 (moaLogPath, moaPort... 등) 을 System 의 Properties 에 주입 합 니 다.아이디어 에서 실행 중 오류 가 발생 하지 않 았 지만 컴 파 일 러 는 putAll 이 존재 하지 않 는 다 는 것 을 알 렸 다.그래서 상기 코드 를
<bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System"/>
<property name="targetMethod" value="getProperties"/>
bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="sysProps">
property>
<property name="targetMethod" value="putAll" />
<property name="arguments">
<props>
<prop key="java.rmi.server.hostname" > ipprop>
props>
property>
bean>