RMI with Spring
RMI with Spring
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.
Example:
Let's see the simple steps to integration spring application
with RMI:
1. Calculation.java
2. CalculationImpl.java
3. myBean.xml
4. client-beans.xml
5. Host.java
6.
Client.java
Calculation.java
package com.smgc;
public interface
Calculation {
int
cube(int number);
int
square(int number);
int
fact(int number);
}
CalculationImpl.java
package
com.smgc;
public class
CalculationImpl implements Calculation {
@Override
public int
cube(int number) {
//
TODO Auto-generated method stub
return number*number*number;
}
@Override
public int
square(int number) {
//
TODO Auto-generated method stub
return number*number;
}
@Override
public int
fact(int number) {
//
TODO Auto-generated method stub
int f=1;
for(int i=1; i<=number; i++)
f = f * i;
return f;
}
}
Client.java
package
com.smgc;
import
java.util.Scanner;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class Client {
public static void main(String[] args)
{
// TODO Auto-generated method
stub
ApplicationContext context =
new ClassPathXmlApplicationContext(
"client-beans.xml");
Calculation c = (Calculation)
context.getBean("calculationBean");
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter
your Name: ");
String nm =
scanner.nextLine();
System.out.print("Enter
Number: ");
int n = scanner.nextInt();
System.out.println("Welcome
" + nm);
System.out.println("Square
of " + n + " is " + c.square(n));
System.out.println("Cube
of " + n + " is " + c.cube(n));
System.out.println("Factorial
of " + n + " is " + c.fact(n));
}
}
Host.java
package
com.smgc;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class Host {
public static void main(String[] args)
{
// TODO Auto-generated method
stub
ApplicationContext context =
new ClassPathXmlApplicationContext("myBean.xml");
System.out.println("Waiting
for requests");
}
}
client-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.smgc.Calculation"></property>
</bean>
</beans>
myBean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="calculationBean" class="com.smgc.CalculationImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="calculationBean"></property>
<property name="serviceInterface" value="com.smgc.Calculation"></property>
<property name="serviceName" value="CalculationService"></property>
<property name="replaceExistingBinding" value="true"></property>
<property name="registryPort" value="1099"></property>
</bean>
</beans>
How to run this example
First Compile and Run the Host.java
Waiting
for requests
Then,
Compile and Run the Client.java
Enter your Name: Piyush
Enter Number: 5
Welcome Piyush
Square of 5 is 25
Cube of 5 is 125
Factorial of 5 is 120
Comments
Post a Comment