2012-06-12 189 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