본문 바로가기

프로그램

Spring - RMI

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Dependency


        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>




SERVER

Remote Server

▶XML 파일 생성 : 위치는 당연히 main/resources

▶Example File Name : remote-server-config.xml 


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                                     http://www.springframework.org/schema/aop
                                     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                                     http://www.springframework.org/schema/context
                                     http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="serviceRemote" class="com.odin.remoteServer.ServiceRemoteImpl" />

    <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="serviceName" value="serviceRemote" />
        <property name="service" ref="serviceRemote" />
        <property name="serviceInterface" value="com.odin.remoteServer.ServiceRemote" />
        <property name="registryPort" value="1099" />
    </bean>
</beans>





Remote Server Run


public class RMIserver {    
    private ApplicationContext context;
    
    public RMIserver() {
        
    }
    
    public void run(){
        context = new ClassPathXmlApplicationContext("remote-server-config.xml");
        logger.info("Remote Server Open / Waiting Client ...");
    }    
}






public interface ServiceRemote {
    public String rmiAction(String command);
}




Remote Server 구현 Class


public class ServiceRemoteImpl implements ServiceRemote{
    
    @Override
    public String rmiAction(String command) {
        if(command.equals("confirm")){
            return "connect";
        }
        else{
            MemoryDB memoryDB = new MemoryDB();
            memoryDB.db_synchronization(command);
            return "update";
        }
    }    
}







Client

Remote Client xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
<bean id="serviceRemote" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://서버IP:1099/serviceRemote"/>
<property name="serviceInterface" value="com.chience.odin.remote.ServiceRemote"/>
</bean> 

</beans>


 


Remote Client Run & Connection

public class RMIclient {    
    private static ApplicationContext context = null; 
    private static ServiceRemote serviceRemote = null;
    
    public RMIclient() {     }
    
    public void connectRemote(){
        try {
            context = new ClassPathXmlApplicationContext("remote-client-config.xml");
            serviceRemote = (ServiceRemote) context.getBean("serviceRemote");
            System.out.println("connect RMI");        
        }catch (Exception e){
            System.out.println("disconnect RMI");
            context = null;
        }
    }        
    public static ApplicationContext getContext() {
        return context;
    }
    public static void setContext(ApplicationContext context) {
        RMIclient.context = context;
    }

   //기능
    public String confirm_connect(){
        return serviceRemote.rmiAction("confirm");
    }    
    public String db_command(String command){
        return serviceRemote.rmiAction(command);
    }    
}




Remote Client Service


public interface ServiceRemote {
    public String rmiAction(String confirm);    
}





Remote Client Synchronized

public class StaticRMI {
    private static RMIclient rmIclient;
    public StaticRMI() {
        rmIclient = new RMIclient();
    }
   //Web Project 가 Client로 사용됨에 따라 static synchronized
    public static synchronized RMIclient getRmIclient() {
        return rmIclient;
    }
    public static synchronized void setRmIclient(RMIclient rmIclient) {
        StaticRMI.rmIclient = rmIclient;
    }
}











'프로그램' 카테고리의 다른 글

FCM - Firebase 클라우드 메시징 테스트  (0) 2017.08.21
quartz scheduler  (0) 2017.04.26
JSCH  (0) 2017.04.20