使用RMI时抛出 error during JRMP connection establishment; nested exception is:java.io.EOFException]

Jo_Cai 2009-03-12 08:08:19
刚学了RMI,编译教材上的代码运行很正常,教材上是使用rmiregistry注册远程对象的。然后我想用weblogic来代替行不行,但修改代码后就报错了。代码如下:
---------HelloService.java------------------------------------------------------------------------------------------
package chapter11;
import java.util.Date;
import java.rmi.*;
public interface HelloService extends Remote{
public String echo(String msg)throws RemoteException;
public Date getTime()throws RemoteException;
}
-------------HelloServiceImpl.java----------------------------------------------------------------------
package chapter11;
import java.rmi.*;
import java.util.Date;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService{
private String name;
public HelloServiceImpl(String name)throws RemoteException{
this.name = name;
}
public String echo(String msg)throws RemoteException{
System.out.println(name+":use the echo() method");
return "echo:"+msg+"from"+name;
}
public Date getTime()throws RemoteException{
System.out.println(name+":use the getTime() method");
return new Date();
}
}
----------SimpleServer.java----------------------------------------------------------------------------
package chapter11;
import java.rmi.*;
import javax.naming.*;

import java.util.*;
public class SimpleServer {
static Context namingContext = null;
public static void bind(String name,HelloService object){
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try{
namingContext = new InitialContext(ht);
namingContext.rebind(name,object);
}catch(NamingException e){
e.printStackTrace();
}finally{
try{
namingContext.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
try{
HelloService service1 = new HelloServiceImpl("service1");
HelloService service2 = new HelloServiceImpl("service2");

bind("rmi://localhost:7001/HelloService1",service1);
bind("rmi://localhost:7001/HelloService2",service2);

System.out.println("the server has registered two HelloService Object");
}catch(Exception e){
e.printStackTrace();
}
}
}
--------------------------------------
在项目里引入weblogic.jar后,运行weblogic server,再运行服务器类即SimpleServer 就报错
javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException]
at com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:142)
at com.sun.jndi.toolkit.url.GenericURLContext.rebind(GenericURLContext.java:231)
at javax.naming.InitialContext.rebind(InitialContext.java:408)
at chapter11.SimpleServer.bind(SimpleServer.java:15)
at chapter11.SimpleServer.main(SimpleServer.java:31)
。。。

...全文
3070 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Williams_bzd 2010-01-08
  • 打赏
  • 举报
回复
有魄力啊,两个月,顶顶你
Jo_Cai 2009-04-29
  • 打赏
  • 举报
回复

package chapter11;
import java.rmi.*;
import javax.naming.*;

import java.util.*;
public class SimpleServer {
static Context namingContext = null;
public static void bind(String name,HelloService object){
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); try{
namingContext = new InitialContext(ht);
namingContext.rebind(name,object);
}catch(NamingException e){
e.printStackTrace();
}finally{
try{
namingContext.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
try{
HelloService service1 = new HelloServiceImpl("service1");
HelloService service2 = new HelloServiceImpl("service2");

bind("HelloService1",service1);
bind("HelloService2",service2);

System.out.println("the server has registered two HelloService Object");
}catch(Exception e){
e.printStackTrace();
}
}
}


问题出在以下地方
bind("rmi://localhost:7001/HelloService1",service1);
bind("rmi://localhost:7001/HelloService2",service2);
当不使用rmiregistry,使用weblogic或Jboss时候不需要指定rmi、地址和端口,因为之前的用InitialContext已指定定好了。
-------
没想到两个月的复杂中自己想到了原因,哈哈
Jo_Cai 2009-04-29
  • 打赏
  • 举报
回复
我自己知道是什么原因了~~~哈哈
Jo_Cai 2009-03-16
  • 打赏
  • 举报
回复
自己顶一下~~~~~~~~~~

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧