从servlet运行客户端时无法连接到rmi服务器

tez616oj  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(427)

此问题已在此处找到答案

什么是nullpointerexception,如何修复它((12个答案)
6小时前关门了。
使用tomcat服务器运行Javaservlet。因此,当servlet启动时,我希望客户端连接到rmi注册表(端口33996)。客户机在测试期间初始化 init() servlet的一部分。但是,由于某些原因,它无法连接到服务器
服务器代码

@Override
    public void startRMIRegistry(CentralServer server){
        try {
            AsyncLogger.log("Setting up RMI Registry.....");
            Registry registry = LocateRegistry.createRegistry(
                Integer.parseInt(ServerConfigUtils.getProperty(ServerConfigUtils.PORT)));
            registry.rebind(ServerConfigUtils.getProperty(ServerConfigUtils.NAME), server);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

客户端代码

private ICentralServer getServerConnection() {
        String serverHost = ServerConfigUtils.getProperty(ServerConfigUtils.HOST);
        Integer serverPort = Integer.valueOf(ServerConfigUtils.getProperty(ServerConfigUtils.PORT));
        String serverName = ServerConfigUtils.getProperty(ServerConfigUtils.NAME);

        Integer failureCount = 0;

        while (failureCount < SERVER_ACTION_ATTEMPTS) {
            try {
                return (ICentralServer)
                    LocateRegistry.getRegistry(serverHost, serverPort).lookup(serverName);
            } catch (Exception e) {
                failureCount++;
                continue;
            }
        }

        AsyncLogger.log("Failed to connect to central server, giving up");
        throw new RuntimeException();
    }

servlet

@Override
    public void init() throws ServletException {

        try {
            client = new LabClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.init();
    }

错误

INFO: Starting ProtocolHandler ["http-nio-8080"]
sg.gov.moh.exceptions.CannotReachServerException: java.lang.NullPointerException
        at sg.gov.moh.client.LabClient.<init>(LabClient.java:366)
        at launcher.servlet.MainServlet.init(MainServlet.java:36)
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1152)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1097)
        at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:768)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:364)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:624)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1651)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
        at sg.gov.moh.centralServer.CentralServer.pingOrPrune(CentralServer.java:318)
        at sg.gov.moh.centralServer.CentralServer.registerLaboratory(CentralServer.java:297)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:163)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:235)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:180)
        at com.sun.proxy.$Proxy5.registerLaboratory(Unknown Source)
        at sg.gov.moh.client.LabClient.registerCallback(LabClient.java:69)
        at sg.gov.moh.client.LabClient.<init>(LabClient.java:364)
        ... 21 more

如果我的同事尝试了这个代码,他们都能够成功连接。已尝试关闭防火墙,为端口设置入站规则,但无效

8aqjt8rx

8aqjt8rx1#

找到了解决办法,我跑了 mvn clean package 在父maven目录中,之前我在子模块级别上以虚拟方式重建子模块,这破坏了字节码。

|- Parent 
|   |--Sub-module a 
|   |--Sub-module b
|
|

相关问题