最近应用spring框架写了个rmi项目,具体如下:
在MyEclipse中新建一个rmiServer的java project,然后导入下面几个包:spring.jar;spring-aspects.jar;spring-mock.jar;common-logging.jar;cglib-nodep-2.1_3.jar;
第一步:
建立test.spring.remote.rmi包,然后在此包下新建一接口MyService.java,代码如下: package test.spring.remote.rmi; public interface MyService extends java.rmi.Remote {
public void doSomething() throws java.rmi.RemoteException;
public String sayHello() throws java.rmi.RemoteException;
} 然后实现这个接口MyServiceImpl.java,代码如下: package test.spring.remote.rmi; public class MyServiceImpl implements MyService { public void doSomething(){
// TODO Auto-generated method stub } public String sayHello(){
// TODO Auto-generated method stub
System.out.println("rmi client call rmi server...");
return "hello world!";
}
} 第二步:
配置applicationContext.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-2.0.xsd"> <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyService</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
</beans> 第三步:
在test.spring.remote.rmi包下建启动rmi server的RmiServer.java,代码如下: package test.spring.remote.rmi; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class RmiServer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml"); }
} 第四步:
Run As--->Java Application,运行RmiServer,控制台打印出“Binding service 'myService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.8.109:1199](local),objID:[0:0:0, 0]]]]”信息 第五步:
新建一个rmiClient的java project,导入相同的jar包,然后建一个相同的test.spring.remote.rmi包 第六步:
将rmiServer中的MyService.java拷贝到rmiClient的test.spring.remote.rmi包下 第七步:
配置rmiClient项目中的applicationContext.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-2.0.xsd"> <bean id="rmiService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyService</value>
</property>
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
</bean>
</beans> 第八步:
在test.spring.remote.rmi包下新建rmi的客户端TestSpringRmi.java,代码如下: package test.spring.remote.rmi; import java.rmi.RemoteException; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpringRmi {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MyService service=(MyService)context.getBean("rmiService"); try {
System.out.println(service.sayHello());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 第九步:
Run As--->Java Application,运行TestSpringRmi,然后在rmiServer的控制台打印出:rmi client call rmi server...
在rmiClient的控制台打印出:hello world! 至此,rmi应该是完成了。但是将我把rmiClient搬到局域网中的另一台电脑上,把applicationContext.xml中的
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
改成
<property name="serviceUrl">
<value>rmi://192.168.8.109:1199/myService</value>
</property> 再次运行rmiClient的TestSpringRmi时,在控制台打印出:
信息: Using service interface [test.spring.remote.rmi.MyService] for RMI stub [rmi://192.168.8.109:1199/myService] - directly implemented
2008-1-7 15:28:04 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
信息: CGLIB2 available: proxyTargetClass feature enabled
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:336)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.sayHello(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.remoting.rmi.RmiClientInterceptorUtils.doInvoke(RmiClientInterceptorUtils.java:98)
at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:363)
at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:259)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy1.sayHello(Unknown Source)
at test.spring.remote.rmi.RmiClient.main(RmiClient.java:17)
Caused by: java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:276)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619) 请教各位能人,问题出在哪里了?
在MyEclipse中新建一个rmiServer的java project,然后导入下面几个包:spring.jar;spring-aspects.jar;spring-mock.jar;common-logging.jar;cglib-nodep-2.1_3.jar;
第一步:
建立test.spring.remote.rmi包,然后在此包下新建一接口MyService.java,代码如下: package test.spring.remote.rmi; public interface MyService extends java.rmi.Remote {
public void doSomething() throws java.rmi.RemoteException;
public String sayHello() throws java.rmi.RemoteException;
} 然后实现这个接口MyServiceImpl.java,代码如下: package test.spring.remote.rmi; public class MyServiceImpl implements MyService { public void doSomething(){
// TODO Auto-generated method stub } public String sayHello(){
// TODO Auto-generated method stub
System.out.println("rmi client call rmi server...");
return "hello world!";
}
} 第二步:
配置applicationContext.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-2.0.xsd"> <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyService</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
</beans> 第三步:
在test.spring.remote.rmi包下建启动rmi server的RmiServer.java,代码如下: package test.spring.remote.rmi; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class RmiServer {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml"); }
} 第四步:
Run As--->Java Application,运行RmiServer,控制台打印出“Binding service 'myService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.8.109:1199](local),objID:[0:0:0, 0]]]]”信息 第五步:
新建一个rmiClient的java project,导入相同的jar包,然后建一个相同的test.spring.remote.rmi包 第六步:
将rmiServer中的MyService.java拷贝到rmiClient的test.spring.remote.rmi包下 第七步:
配置rmiClient项目中的applicationContext.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-2.0.xsd"> <bean id="rmiService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyService</value>
</property>
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
</bean>
</beans> 第八步:
在test.spring.remote.rmi包下新建rmi的客户端TestSpringRmi.java,代码如下: package test.spring.remote.rmi; import java.rmi.RemoteException; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpringRmi {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MyService service=(MyService)context.getBean("rmiService"); try {
System.out.println(service.sayHello());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 第九步:
Run As--->Java Application,运行TestSpringRmi,然后在rmiServer的控制台打印出:rmi client call rmi server...
在rmiClient的控制台打印出:hello world! 至此,rmi应该是完成了。但是将我把rmiClient搬到局域网中的另一台电脑上,把applicationContext.xml中的
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
改成
<property name="serviceUrl">
<value>rmi://192.168.8.109:1199/myService</value>
</property> 再次运行rmiClient的TestSpringRmi时,在控制台打印出:
信息: Using service interface [test.spring.remote.rmi.MyService] for RMI stub [rmi://192.168.8.109:1199/myService] - directly implemented
2008-1-7 15:28:04 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
信息: CGLIB2 available: proxyTargetClass feature enabled
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:336)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.sayHello(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.remoting.rmi.RmiClientInterceptorUtils.doInvoke(RmiClientInterceptorUtils.java:98)
at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:363)
at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:259)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
at $Proxy1.sayHello(Unknown Source)
at test.spring.remote.rmi.RmiClient.main(RmiClient.java:17)
Caused by: java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:276)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619) 请教各位能人,问题出在哪里了?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货