MessageReceiverFactory: 生产MessageReceiver的简单工厂:
public class MessageReceiverFactory { private static MessageReceiver messageReceiver; private static RemotableMessageReceiver remotableMessageReceiver;
private static void init() throws ServerReceiverException { SystemInfo sysInfo = ContextFactory.getSystemConfigContext() .getSystemInfo(); if (sysInfo.isServer()) { MessageReceiver mifMessageReceiver = new ServerMessageReceiver(); if (sysInfo.isRemoteable()) { try { remotableMessageReceiver = new RMIMessageReceiver(mifMessageReceiver); } catch (RemoteException e) { throw new ServerReceiverException( "bind Message Receiver to port: " + sysInfo.getRmiport() + " with service name: " + sysInfo.getReceiverRmiBindName(), e); } }
messageReceiver = new StdMessageReceiver(mifMessageReceiver);
} else { String rmiHost = sysInfo.getRmiHost(); int rmiPort = sysInfo.getRmiport(); String serviceName = sysInfo.getReceiverRmiBindName(); messageReceiver = createRemoteMesageReceiver(rmiHost, rmiPort, serviceName); } }
public static MessageReceiver createRemoteMesageReceiver(String rmiHost, int rmiPort, String serviceName) throws ServerReceiverException { return new RemoteMessageReceiver(rmiHost, rmiPort, serviceName); }
public synchronized static MessageReceiver getMessageReceiver() throws ServerReceiverException { if(messageReceiver == null) { init(); } return messageReceiver; }
public static RemotableMessageReceiver getRemoteableMesageReceiver(){ return remotableMessageReceiver; }
public synchronized static void initMessageReceiver() throws ServerReceiverException { init(); } } |
StartUpServlet:
publicclass StartupServlet extends HttpServlet { publicvoid init(ServletConfig config) throws ServletException { try { //parse configuration
//init rmi service MessageReceiverFactory.initMessageReceiver();
} catch (Exception ex) { ex.printStackTrace(); thrownew ServletException(ex.getMessage(), ex); } } publicvoid destroy() { try {
RemotableMessageReceiver remotableMessageReceiver = MessageReceiverFactory .getRemoteableMesageReceiver(); if (remotableMessageReceiver != null) { SystemInfo sysInfo = ContextFactory.getSystemConfigContext() .getSystemInfo(); RMIUtils.unBind(sysInfo.getRmiport(), sysInfo .getReceiverRmiBindName(), remotableMessageReceiver); } } catch (Exception e) { e.printStackTrace(); } } } |
RMIUtils: 工具类提供rmi注册和撤销服务的功能:
public class RMIUtils { public static void bind(int rmiPort, String serviceName, Remote remoteObject) throws RemoteException { SystemInfo sysInfo = ContextFactory.getSystemConfigContext() .getSystemInfo(); Remote exportable = (Remote) UnicastRemoteObject .exportObject(remoteObject);
Registry registry = null;
try { registry = LocateRegistry.getRegistry(sysInfo.getRmiport()); registry.list(); } catch (Exception e) { registry = LocateRegistry.createRegistry(sysInfo.getRmiport()); } registry.list(); System.out.println("bind the service: " + serviceName); String bindName = serviceName;
registry.rebind(bindName, exportable);
}
public static void unBind(int rmiPort, String serviceName, Remote remoteObject) throws RemoteException { SystemInfo sysInfo = ContextFactory.getSystemConfigContext() .getSystemInfo();
Registry registry = LocateRegistry.getRegistry(sysInfo.getRmiport());
String bindName = serviceName;
try { registry.unbind(bindName); UnicastRemoteObject.unexportObject(remoteObject, true); } catch (java.rmi.NotBoundException nbe) { } } } |