从servlet调用RMI方法

时间:2013-08-13 20:07:20

标签: java servlets rmi

我正在编写一个可以进行rmi调用的servlet。我已经编写了RMI服务器并使用java swing客户端进行了测试,一切顺利。但是我在使用servlet时遇到了问题。 当用户打开主网页时,servlet会自动调用rmi方法来检索数据库中的某些信息,然后构建要显示的网页。 当用户单击一个触发另一个调用另一个rmi方法的servlet的链接时,我得到一个错误:

java.rmi.ConnectException: Connection refused to host: 192.168.1.101; nested exception is: 
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at project.RemoteClass.getEngine(RemoteClass.java:33)
at project.MainServlet.processRequest(MainServlet.java:57)
at project.MainServlet.doGet(MainServlet.java:176)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)

我认为这可能是由于我如何查找注册表并将其传递给serlvets。 为了避免在每个servlet中写入每次查找RMI代码,我想做一个类(就像在JDBC clien上使用的ConnectionManager)一个查找注册表并将其返回给调用servlet的类:

public class RemoteClass {
    private RemoteClass(){}
    private static Registry registry = null;
    private static RemoteInterface interface_engine = null;
    private static final String rmiAddress = "192.168.1.101";
    private static final int rmiPort = 22222;
    private static final String skeleton = "ProjRMISkeleton";

    public static RemoteInterface getEngine() throws RemoteException {
        registry = LocateRegistry.getRegistry(rmiAddress, rmiPort);
        try {
            interface_engine = (RemoteInterface) registry.lookup(skeleton);
        } catch (RemoteException ex) {
            Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        return interface_engine;
    }
}

并且当此代码执行registry.lookup方法时会出现该错误。 我该如何解决?

1 个答案:

答案 0 :(得分:0)

  

连接拒绝主持人:192.168.1.101   ...   at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)

没有注册表运行在192.168.1.101,在本例中是在端口22222上调用LocateRegistry.getRegistry("192.168.1.101", 22222).

也许您的注册表在第一次通话后退出。如果您使用LocateRegistry.createRegistry()在服务器上创建它并且不将结果存储在静态字段中,则可能会被垃圾收集。