Exporting RMI Service in Spring framework
First write Interface and Implementation Class you don’t need to implement 'java.rmi.Remote' interface as well no need to throw RemoteException.
Code:
public interface RmiSampleService
{
String getValue(String s);
}
public class RmiSampleServiceImpl implements RmiSampleService
{
public static void main(String args[])
{
System.out.println("hi from RMI Spring ");
}
publicRmiSampleServiceImpl()
{
System.out.println("constructor rmi");
}
public String getValue(String s)
{
return "Hi From RMI Service " + a;
}
}
RmiSampleService.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/ spring-beans.dtd">
<beans>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<value>rmiservice</value>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>RmiSampleService</value>
</property>
</bean>
<bean id="rmservice" class="RmiSampleServiceImpl">
</bean>
</beans>
Now Write Client Code
Code:
RmiSampleServiceImpl.java
import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
class RmiSampleServiceClient
{
public static void main(String args[])
{
try
{
System.out.println("Wait. for start uo.");
Resource res = new ClassPathResource("RmiSampleService.xml");
BeanFactory factory = new XmlBeanFactory(res);
System.out.println(" Bean factory created");
rmservice bean1 = (rmservice)factory.getBean("rmservice");
String s = bean1.getValue(args[0]);
System.out.println(s);
}
catch(Exception e1)
{System.out.println(""+e1);}
}
}
Now you need to compile and run client you will get the output.