Spring and RMI Integration with Example by R4R Team

Exposing services using RMI:-

Using Spring’s support for RMI, you can transparently expose your services through the RMI infrastructure. After having this set up, you basically have a configuration similar to remote EJBs, except for the fact that there is no standard support for security context propagation or remote transaction propagation.

Spring provides an easy way to run RMI application by the help of org.springframework.remoting.rmi.RmiProxyFactoryBean and org.springframework.remoting.rmi.RmiServiceExporter classes.

RmiServiceExporter

That provides the exportation service for the rmi object. This service can be accessed via plain RMI or RmiProxyFactoryBean.

RmiProxyFactoryBean

This is the factory bean for Rmi Proxies. It exposes the proxied service that can be used as a bean reference.


Example of Spring and RMI Integration:-
There are some following step to make an Application using RMI with Spring as :

The following example show how to write a simple Java SpringRMI application using RMI. To start with it, let us have working Eclipse IDE in place and follow the following steps to developed a Java Application:

1 Create a 
Java Project with a name SpringRMI and create a package r4r.in under the src folder in the created project.

2 Add Spring and other libraries into the folder lib to make it.

3 Create a interface Square.java and Java class SquareImpl.java, Host.java, Client.java under the r4r.in package.

4 Create configuration files applicationcontext.xml, client-beans.xml  under src folder .

5 The final step is to create the content of all the source and configuration files and export the application as explained below.

Following are the list of JAR files required for this application:

1 Spring Core jar files
2 Spring Remoting jar files
3 Spring AOP jar files

1 Following is the content of  Interface Square.java containing one sq(square) metod.

package r4r.in;

public interface Square {

int sq(int number);

}

 

2 Following is the content of  java class SquareIml.java .This class provides the implementation of Square interface.

package r4r.in;

public class SquareImpl implements Square{

@Override

public int sq(int number) {

return number*number;

} }

 

3 Following is the content of  xml file applicationcontext.xml .In this xml file we define the bean for SquareImpl class and RmiServiceExporter class. We need to provide values for the following properties of RmiServiceExporter class.

1 service
2 serviceInterface
3 serviceName
4 replaceExistingBinding
5 registryPort

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="squareBean" class="r4r.in.SquareImpl"></bean>

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">

<property name="service" ref="squareBean"></property>

<property name="serviceInterface" value="r4r.in.Square"></property>

<property name="serviceName" value="SquareService"></property>

<property name="replaceExistingBinding" value="true"></property>

<property name="registryPort" value="1099"></property>

</bean>

</beans>

4 Following is the content of  xml file client-beans.xml. In this xml file, we  define bean for RmiProxyFactoryBean. now we  need to define two properties of this class.

1 serviceUrl
2 serviceInterface

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="squareBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

<property name="serviceUrl" value="rmi://localhost:1099/SquareService"></property>

<property name="serviceInterface" value="r4r.in.Square"></property>

</bean>

</beans>

 

5 Following is the content of  java class host.java. It is simply getting the instance of ApplicationContext.

package r4r.in;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Host{

public static void main(String[] args){

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

System.out.println("Waiting for requests from client");

} }

 

6 Following is the content of  java class client.java. It is simply getting the instance of Square and call the method.

package r4r.in;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");

Square square = (Square)context.getBean("squareBean");

System.out.println(square.sq(6));

} }

 

7 Run this Application :

1 First we Run the Host.java

2 Then Run Client.java.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!