📜 ⬆️ ⬇️

Spring Remoting - Spring + RMI

Spring remoting

The spring framework provides extensive capabilities for building distributed applications. It not only helps to create remote services, but also simplifies access to them. At the moment, using the framework, you can organize remote access using a large number of technologies - Caucho's Hessian and Burlap, your own implementation of remote access via HTTP, RMI, etc. Under the cat a brief overview of the capabilities of the Spring framework for building distributed applications using RMI .


')

Spring remoting



Let's return to the example begun in the last article .

An Action interface representing a mathematical operation and its two implementations ActionAdd and ActionMultiply were considered:

public interface Action { long performAction(long op1, long op2); String getName(); } public class ActionAdd implements Action { @Override public long performAction(long op1, long op2) { return op1 + op2; } @Override public String getName() { return " + "; } } public class ActionMultiply implements Action { @Override public long performAction(long op1, long op2) { return op1 * op2; } @Override public String getName() { return " * "; } } 


And the ICalculator interface with Calculator implementation:

 public interface ICalculator { public void setAction(Action act); public String calc(String[] args); } public class Calculator implements ICalculator { private Action action; @Override public void setAction(Action action) { this.action = action; } @Override public String calc(String[] args) { long op1 = Long.parseLong(args[0]); long op2 = Long.parseLong(args[1]); return op1 + action.getName() + op2 + " = " + action.performAction(op1, op2); } } 


Objects were placed in an IoC spring container:

 <beans> <bean id="multiply" class="springtest.operations.ActionMultiply" /> <bean id="add" class="springtest.operations.ActionAdd" /> <bean id="calculator" class="springtest.calculator.Calculator"> <property name="action" ref="add" /> </bean> </beans> 


And already created object was removed from the container:

 ICalculator calc = (ICalculator) factory.getBean("alculator"); System.out.print(calc.calc(new String[] {"30", "60"})); 


Distribution.



Now the decision has been made to make our application distributed - one computer cannot cope with a large computational load. Java provides for such cases RMI - Remote Method Invocation, a programmatic interface for calling remote methods, which is not so difficult to do. But using Spring Remoting, everything is more than simple.

To create an RMI server, all we need is to declare in the container a special bean - the RMI Service:

 <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="serviceName" value="Calculator"/> <property name="service" ref="calculator"/> <property name="serviceInterface" value="springtest.calculator.ICalculator"/> <property name="registryPort" value="1199"/> </bean> 


We start the server of our distributed application:

 public class ActionServer { public static void main(String[] args) { new ClassPathXmlApplicationContext("xml-beans.xml"); } } 


Reading the XML file, Spring will do everything necessary. We start.

 Mar 24, 2011 5:14:24 PM org.springframework.remoting.rmi.RmiServiceExporter getRegistry INFO: Looking for RMI registry at port '1199' Mar 24, 2011 5:14:24 PM org.springframework.remoting.rmi.RmiServiceExporter sourcepare INFO: Binding service 'Calculator' to RMI registry: RegistryImpl_Stub[UnicastRef [liveRef: [endpoint:[127.0.1.1:1199](remote),objID:[0:0:0, 0]]]] 


The server is ready.

Now you need to make a client. Create a class ClientCalculator, which is a wrapper for the calculator:

 public class ClientCalculator { private ICalculator calculator; public void setCalculator(ICalculator calculator) { this.calculator = calculator; } public ICalculator getCalculator() { return calculator; } } 


Now for the client application we create our IoC XML container:

 <beans> <bean id="clientCalculator" class="springrmi.client.ClientCalculator"> <property name="calculator" ref="remoteCalculator"/> </bean> <bean id="remoteCalculator" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:1199/Calculator"/> <property name="serviceInterface" value="springtest.calculator.ICalculator"/> </bean> </beans> 


To create and use remote objects, use RmiProxeFactoryBean, which returns the required interface. And passes it to the newly created clientCalculator wrapper.

Create a client class and check:

 public class Client { public static void main(String[] args) { ApplicationContext factory = new ClassPathXmlApplicationContext("springrmi/client/client-beans.xml"); ClientCalculator calc = (ClientCalculator) factory.getBean("clientCalculator"); System.out.print(calc.getCalculator().calc(new String[] {"30", "60"})); } } 


 30 + 60 = 90 


All is ready. We got a distributed application.

Source:
http://narod.ru/disk/8319633001/springrmi.tar.gz.html

Sources used:
http://static.springsource.org/spring/docs/2.0.x/reference/remoting.html

In addition to RMI, Spring Remoting also has support for other services. For further reading, I recommend referring to the link above.

Source: https://habr.com/ru/post/117802/


All Articles