2012-06-12 148 views
0
点击服务器程序

下面是它实现RMI客户端程序不RMI

继片段是我的服务器类

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终止。 我的问题是为什么我的客户端程序没有打到服务器。

+1

您确定运行客户端时服务器已启动并正在运行吗? – Ewald

+0

是的,我得到“服务器处于监听模式”在console.I使用eclipse运行,所以立即执行客户端,但没有响应,javaw.exe终止 – happy

+0

你可以在每个threadExecutor.shutdown()后放置一个System.out消息; //关闭工作线程线?我很好奇,看看服务器是否等待连接。 – Ewald

回答

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

  2. 当致电Naming.bind()Naming.rebind()时,使用除“localhost”以外的任何主机名称都没有多大意义。如果主机名不是本地的,它将会失败,所以你只是在te服务器代码中增加一个额外的毫无意义的配置恶梦。使它成为'本地主机',它将在任何地方运行。

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

+0

它现在工作,但我卡在其他problem.when我使用上面的客户端代码在行动类(struts框架)我得到java.rmi.UnmarshalException:错误解组返回;嵌套的异常是: java.lang.ClassNotFoundException:com.mindcraft.queryExecutor.actionclass.ExecutorInterface(没有安全管理器:禁用RMI类加载器) – happy

+0

@happy该类需要部署到客户端。 – EJP

+0

ExecutorInterface部署到客户端。 – happy