Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring with RMI


Spring RMI lets you expose your services through the RMI infrastructure.

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

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


RmiProxyFactoryBean

It 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

Let’s see the simple steps to integration spring application with RMI:

  1. Calculation.java
  2. CalculationImpl.java
  3. applicationContext.xml
  4. client-beans.xml
  5. Host.java
  6. Client.java

Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Remoting jar files
  • Spring AOP jar files

download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.


1) Calculation.java

It is the simple interface containing one method cube.

  1. package com.Kreationnext;
  2. public interface Calculation {
  3. int cube(int number);
  4. }

2) CalculationImpl.java

This class provides the implementation of Calculation interface.

  1. package com.Kreationnext;
  2. public class CalculationImpl implements Calculation{
  3.     @Override
  4.     public int cube(int number) {
  5.         return number*number*number;
  6.     }
  7. }

3) applicationContext.xml

In this xml file we are defining the bean for CalculationImpl 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
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  5. http://www.springframework.org/schema/beans/spring-beans.xsd”>
  6. <bean id=“calculationBean” class=“com.Kreationnext.CalculationImpl”></bean>
  7. <bean class=“org.springframework.remoting.rmi.RmiServiceExporter”>
  8.     <property name=“service” ref=“calculationBean”></property>
  9.     <property name=“serviceInterface” value=“com.Kreationnext.Calculation”></property>
  10.     <property name=“serviceName” value=“CalculationService”></property>
  11.     <property name=“replaceExistingBinding” value=“true”></property>
  12.     <property name=“registryPort” value=“1099”></property>
  13. </bean>
  14. </beans>

4) client-beans.xml

In this xml file, we are defining bean for RmiProxyFactoryBean. You need to define two properties of this class.

  1. serviceUrl
  2. serviceInterface
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  5. http://www.springframework.org/schema/beans/spring-beans.xsd”>
  6. <bean id=“calculationBean” class=“org.springframework.remoting.rmi.RmiProxyFactoryBean”>
  7. <property name=“serviceUrl” value=“rmi://localhost:1099/CalculationService”></property>
  8. <property name=“serviceInterface” value=“com.Kreationnext.Calculation”></property>
  9. </bean>
  10. </beans>

5) Host.java

It is simply getting the instance of ApplicationContext. But you need to run this class first to run the example.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Host{
  5. public static void main(String[] args){
  6. ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7. System.out.println(“Waiting for requests”);
  8. }
  9. }

6) Client.java

This class gets the instance of Calculation and calls the method.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Client {
  5. public static void main(String[] args)  {
  6. ApplicationContext context = new ClassPathXmlApplicationContext(“client-beans.xml”);
  7. Calculation calculation = (Calculation)context.getBean(“calculationBean”);
  8. System.out.println(calculation.cube(7));
  9. }
  10. }

How to run this example

First Compile and Run the Host.java

Then, Compile and Run the Client.java

Comments

comments