| |
RMI的远程 |
|
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="rmserver" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/rmserver</value>
</property>
<property name="serviceInterface">
<value>rmserver</value>
</property>
</bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<value>rmservice</value>
</property>
<property name="serviceName">
<value>service1</value>
</property>
<property name="serviceInterface">
<value>rmservice</value>
</property>
</bean>
<bean id="rmservice" class="RMIserviceimpl">
</bean>
<bean id="rmserverimpl" class="RMIserverimpl">
<property name="rmserver"><ref bean="RMIserver"/></property>
</bean>
</beans>
File: Main.java
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
class rmserviceclient {
public static void main(String args[]) throws Exception {
Resource res = new ClassPathResource("context.xml");
BeanFactory factory = new XmlBeanFactory(res);
RMIServer bean1 = (RMIServer) factory.getBean("RMIServer");
String r = bean1.getresult("asdf");
System.out.println(r);
// ////////////client/////////////////
res = new ClassPathResource("context.xml");
factory = new XmlBeanFactory(res);
RMIservice bean2 = (RMIservice) factory.getBean("RMIservice");
String s = bean2.getresult("AAA");
System.out.println(s);
}
}
interface RMIservice {
String getresult(String s);
}
interface RMIServer extends Remote {
String getresult(String s) throws RemoteException;
}
class RMIserviceimpl implements RMIservice {
public static void main(String args[]) {
System.out.println("ready");
}
public RMIserviceimpl() {
System.out.println("constructor ok");
}
public String getresult(String a) {
String r = "Hi " + a;
return r;
}
}
class RMIserverimpl extends UnicastRemoteObject implements RMIServer {
public static void main(String args[]) {
try {
RMIserverimpl ob = new RMIserverimpl();
Naming.rebind("RMIServer", ob);
System.out.println("ready");
} catch (Exception e1) {
System.out.println("registered");
}
}
public RMIserverimpl() throws RemoteException {
System.out.println("constructor ok");
}
public String getresult(String a) throws RemoteException {
String r = "hi " + a;
return r;
}
}
|
|
Spring-RMIRemoting.zip( 2,894 k) |
Related examples in the same category |
|