以下は、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 は終了しました。私の質問は、クライアント プログラムがサーバーにアクセスしない理由です。