Spring RMI 실현

22888 단어 스프링 프레임
글 목록
  • 1. 서버 엔 드 코드
  • 2. 클 라 이언 트 코드
  • 3. 주의 점
  • 이 블 로그 원 격 인터페이스 와 그 실현 코드 는 더 이상 올 리 지 않 습 니 다. 블 로그 웹 프로젝트 에서 JAVA RMI 실현 중의 코드 를 참고 하 십시오.
    서버 쪽 코드
  • 원 격 인 터 페 이 스 를 만 듭 니 다. 이 인 터 페 이 스 는 더 이상 JAVA RMI 에서 Romote 류 를 계승 하지 않 아 도 됩 니 다.
  • 원 격 인터페이스의 실현 클래스 를 만 듭 니 다. 이 클래스 는 JAVA RMI 에서 java. rmi. UnicastRemoteObject 클래스 를 계승 하지 않 아 도 되 고 구조 방법 도 만 들 필요 가 없습니다.
  • spring 프로필 application. xml 파일 을 설정 합 니 다.
    
    <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>
    
    
  • 웹. xml 에 Dispatcher Servlet 을 설정 하고 contextConfigLocation 값 을 application. xml
  • 로 설정 합 니 다.
  • 프로젝트 발표
  • 2. 클 라 이언 트 코드
  • 서버 쪽 과 같은 원 격 인터페이스 만 들 기
  • spring 프로필 application. xml 파일 을 설정 합 니 다.
    <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));
        }
    }
    
  • 3. 조심 하 세 요.
    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>
    

    좋은 웹페이지 즐겨찾기