让已有的系统开放RMI服务(2)

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) {

            }

      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值