Spring integrates with RMI to realize remote access

Using Spring's support for RMI, you can build your distributed applications very easily. On the server side, you can expose your services through Spring's org.springframework.remoting.rmi.RmiServiceExporter; on the client side, you can use the services exposed by the server through org.springframework.remoting.rmi.RmiProxyFactoryBean, which is very convenient. This access method of the C/S model can shield the complexity of RMI itself, such as the processing details of the server Skeleton and the client Stub, etc. These are transparent to the service development and service personnel, without the need for Focus too much on developing your business logic.

The following is an example to illustrate how to integrate RMI through Spring.

 

 

Server-side publishing service

We define a service interface, the server implements the service interface to complete its complex logic, and the client can call the service exposed by the server through this interface, as shown below:

 

package org.shirdrn.spring.remote.rmi;  
  
public interface AccountService {  
    int queryBalance(String mobileNo);  
    String shoopingPayment(String mobileNo, byte protocol);  
}

 Service implementation , an example is as follows:

 

 

package org.shirdrn.spring.remote.rmi;  
  
import org.apache.log4j.Logger;  
  
public class MobileAccountServiceImpl implements AccountService {  
  
    private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);  
    public int queryBalance(String mobileNo) {  
        if (mobileNo != null)  
            return 100;  
        return 0;  
    }  
  
    public String shoopingPayment(String mobileNo, byte protocol) {  
        StringBuffer sb = new StringBuffer().append("Your mobile number is \"").append(mobileNo).append("\" protocol type is \"").append(protocol).append("\".");    
        LOG.info("Message is: " + sb.toString());    
        return sb.toString();  
    }  
}  

 The server publishes services for the client to make (remote method) calls. The Spring configuration server.xml is as follows:

 

 

<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  
    <!-- define service implementation -->
    <bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
    
    <!-- Use RmiServiceExporter to expose the interface of the service object as an RMI object-->
    <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">    
        <property name="serviceName" value="MobileAccountService" />    
        <property name="service" ref="accountService" />    
        <property name="serviceInterface" value="org.shirdrn.spring.remote.rmi.AccountService" />    
        <property name="registryPort" value="8080" />    
        <property name="servicePort" value="8088" />    
    </bean>
  
</beans>

 The above configuration specifies the name of the exposed service, which is injected into RmiServiceExporter through the serviceName attribute. The service name is MobileAccountService, and the client can call it through the service name.

 

Let 's start the server and publish the service, as shown below:

 

package org.shirdrn.spring.remote.rmi;  
  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class RmiServer {  
  
    public static void main(String[] args) throws InterruptedException {  
        new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");  
          
        Object lock = new Object();  
        synchronized (lock) {  
            lock.wait();  
        }  
    }  
}

 

 

 

 

Client calls service

The client service call configuration client.xml is as follows:

 

<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  
    <!-- Clients use RmiProxyFactoryBean to connect to remotely exposed services -->
    <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
        <property name="serviceUrl" value="rmi://localhost:8080/MobileAccountService" />  
        <property name="serviceInterface" value="org.shirdrn.spring.remote.rmi.AccountService" />  
    </bean>  
  
    <!-- Assemble using dependency injection -->
    <bean class="org.shirdrn.spring.remote.rmi.RmiClient">
    	<property name="accountService" ref="mobileAccountService"/>
    </bean>
  
</beans>

 In the configuration, inject a serviceUrl and serviceInterface into the RmiProxyFactoryBean to make a remote method call ( service call ). An example of the call is as follows:

 

 

package org.shirdrn.spring.remote.rmi;  
  
import org.apache.log4j.Logger;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class RmiClient {  
  
    private static final Logger LOG = Logger.getLogger(RmiClient.class);  
      
    /**
     * Containers are assembled via dependency injection
     */
    private static AccountService accountService;
	
    public void setAccountService(AccountService accountService) {
	this.accountService = accountService;
    }



    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/client.xml");  
        
        /**
         * The container is instantiated directly
         */
//      AccountService accountService = (AccountService) ctx.getBean("mobileAccountService");  
        String result = accountService.shoopingPayment("13800138000", (byte) 5);  
        System.out.println(result);
        LOG.info(result);  
    }  
  
}

 It can be seen that it is very easy to achieve remote access

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941157&siteId=291194637