Java Reference
In-Depth Information
3. public interface CallbackServer extends Remote{
4. public void getProject(String projectID, String callbackMachine,
5. String callbackObjectName) throws RemoteException;
6. }
Example 4.16 CallbackServerImpl.java
1. import java.rmi.Naming;
2. import java.rmi.server.UnicastRemoteObject;
3. public class CallbackServerImpl implements CallbackServer{
4. private static final String CALLBACK_SERVER_SERVICE_NAME = "callbackServer";
5. public CallbackServerImpl(){
6. try {
7. UnicastRemoteObject.exportObject(this);
8. Naming.rebind(CALLBACK_SERVER_SERVICE_NAME, this);
9. }
10. catch (Exception exc){
11. System.err.println("Error using RMI to register the CallbackServerImpl " + exc);
12. }
13. }
14.
15. public void getProject(String projectID, String callbackMachine,
16. String callbackObjectName){
17. new CallbackServerWorkThread(projectID, callbackMachine, callbackObject-
Name);
18. }
19.
20. }
In the getProject method, CallbackServerImpl delegates the task of retrieving the project to a worker object,
CallbackServerDelegate . This object runs on its own thread and does the work of retrieving a project and
sending it to a client.
Example 4.17 CallbackServerDelegate.java
1. import java.net.MalformedURLException;
2. import java.rmi.Naming;
3. import java.rmi.NotBoundException;
4. import java.rmi.RemoteException;
5. public class CallbackServerDelegate implements Runnable{
6. private Thread processingThread;
7. private String projectID;
8. private String callbackMachine;
9. private String callbackObjectName;
10.
11. public CallbackServerDelegate(String newProjectID, String newCallbackMachine,
12. String newCallbackObjectName){
13. projectID = newProjectID;
14. callbackMachine = newCallbackMachine;
15. callbackObjectName = newCallbackObjectName;
16. processingThread = new Thread(this);
17. processingThread.start();
18. }
19.
20. public void run(){
21. Project result = getProject();
22. sendProjectToClient(result);
23. }
24.
25. private Project getProject(){
26. return new Project(projectID, "Test project");
27. }
28.
29. private void sendProjectToClient(Project project){
30. try{
31. String url = "//" + callbackMachine + "/" + callbackObjectName;
32. Object remoteClient = Naming.lookup(url);
33. if (remoteClient instanceof CallbackClient){
34. ((CallbackClient)remoteClient).receiveProject(project);
35. }
36. }
37. catch (RemoteException exc){}
38. catch (NotBoundException exc){}
39. catch (MalformedURLException exc){}
40. }
41. }
Search WWH ::




Custom Search