【问题标题】:Client program does not hit the server program in RMI客户端程序没有命中 RMI 中的服务器程序
【发布时间】:2012-06-12 06:31:16
【问题描述】:

下面是实现RMI的sn-ps

以下是我的服务器类

package com.queryExecutor.actionclass;   
import java.rmi.Naming;   
import java.rmi.RemoteException;   
import java.rmi.registry.LocateRegistry;   
import java.rmi.server.UnicastRemoteObject;   
import java.util.concurrent.ExecutorService;   
import java.util.concurrent.Executors;   

public class ExecutorServer extends UnicastRemoteObject implements executorInterface   
{   
public ExecutorServer()throws RemoteException   
{   
System.out.println("Server is in listening mode");   
}   
public void executeJob(String req_id,String usrname,String pwd,String driver,String url)throws RemoteException     
{   
    System.out.println("Inside executeJob...");   
    acQueryExecutor a=new acQueryExecutor(req_id,usrname,pwd,driver,url);   

    ExecutorService threadExecutor = Executors.newCachedThreadPool();   

    threadExecutor.execute(a); // start task1   

    threadExecutor.shutdown(); // shutdown worker threads   

}      
public void killJob(String req_id)throws RemoteException{}   
public int getJobStatus(String req_id)throws RemoteException{return 1;}   
public void restart(String req_id)throws RemoteException{}   


public static void main(String arg[])   
{   
try{   
    //Registry registry = LocateRegistry.getRegistry("10.155.1.159",1099);    
    LocateRegistry.createRegistry(2005);   
ExecutorServer p=new ExecutorServer();   
Naming.rebind("//127.0.0.1:2005/exec",p);   
}catch(Exception e)   
{   
System.out.println("Exception occurred : "+e.getMessage());   
}   
}   
@Override  
public void executeJob(String req_id, String usrname, String pwd)   
        throws RemoteException {   
    // TODO Auto-generated method stub   
       System.out.println("Inside executeJob...");
acQueryExecutor a=new acQueryExecutor(req_id,usrname,pwd,"driver","url");

ExecutorService threadExecutor = Executors.newCachedThreadPool();
threadExecutor.execute(a); // start task1
threadExecutor.shutdown(); // shutdown worker threads
}   
}  

界面

package com.queryExecutor.actionclass;   
import java.rmi.Remote;   
import java.rmi.RemoteException;   

public interface executorInterface extends Remote   
{   
public void executeJob(String req_id,String usrname,String pwd)throws RemoteException;   
public void killJob(String req_id)throws RemoteException;   
public int getJobStatus(String req_id)throws RemoteException;   
public void restart(String req_id)throws RemoteException;   
}  
package com.queryExecutor.actionclass;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface executorInterface extends Remote
{
public void executeJob(String req_id,String usrname,String pwd)throws RemoteException;
public void killJob(String req_id)throws RemoteException;
public int getJobStatus(String req_id)throws RemoteException;
public void restart(String req_id)throws RemoteException;
} 

客户

package com.queryExecutor.actionclass;   
import java.rmi.Naming;   

    public class testClient {   
    public static void  main(String args[])   
    {   
    try{   

        executorInterface p=(executorInterface)Naming.lookup("//127.0.0.1:2005/exec");   

    p.executeJob("1", "abc", "abc");   


    }   
    catch(Exception e)   
    {   
    System.out.println("Exception occurred : "+e.getMessage());   
    }   
    }   
    }  

当我运行客户端代码时,我没有得到任何输出,而是在 Eclipse 控制台标题中它写入的 javaw.exe 终止。 我的问题是为什么我的客户端程序没有访问服务器。

【问题讨论】:

  • 您确定在运行客户端时服务器已启动并运行?
  • 是的,我在控制台中得到“服务器处于侦听模式”。我正在使用 eclipse 运行,所以我立即执行客户端但没有响应,javaw.exe 被终止
  • 能否在每个 threadExecutor.shutdown() 之后放置一条 System.out 消息? // 关闭工作线程行?我很好奇服务器是否在等待连接。
  • 我在 shutdown() 之后添加了 System.out 消息,但它没有打印出来。
  • 从来没有?这意味着甚至在它到达那里之前就出现了问题。使用 Eclipse,在调试模式下运行服务器并单步执行代码以查看哪里出了问题。 Eclipse中Java调试的快速介绍可以参考vogella.com/articles/EclipseDebugging/article.html。

标签: java jakarta-ee rmi


【解决方案1】:
  1. 您需要将LocateRegistry.createRegistry() 的结果存储在不会被垃圾回收的地方。否则将被垃圾回收 => 没有注册表 => Naming.lookup() 失败 => 客户端静默退出,因为您通过将异常打印到 javaw.exe 进程的 System.out 来处理异常没有这种东西。

  2. 当调用Naming.bind() 或Naming.rebind() 时,使用“localhost”以外的任何主机名都没有多大意义。如果主机名不是本地的,它无论如何都会失败,所以你只是在服务器代码中添加了一个额外的无意义的配置噩梦。将其设为“localhost”,它将在任何地方运行。

  3. 你的executeJob() 方法完全是疯了。它创建一个 Executor,调度一个作业,然后关闭 Executor:所以 Executor 永远不能做线程池;所以这基本上是浪费时间。您也可以只生成一个线程来执行任务;但是由于 RMI 已经是多线程的,您也可以自己执行任务。失去这一切。

【讨论】:

  • 它现在可以工作,但我遇到了其他问题。当我在动作类(struts 框架)中使用上面的客户端代码时,我得到 java.rmi.UnmarshalException: error unmarshalling return;嵌套异常是:java.lang.ClassNotFoundException: com.mindcraft.queryExecutor.actionclass.ExecutorInterface(无安全管理器:RMI 类加载器已禁用)
  • @happy 该类需要部署到客户端。
  • ExecutorInterface 部署到客户端。
  • @happy 异常说com.mindcraft.queryExecutor.actionclass.ExecutorInterface没有部署到客户端。
  • 在同一个目录中,当我在新的 java 文件中编写相同的代码时,它正在工作。但是当我在动作类中尝试时,它会抛出异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
相关资源
最近更新 更多