java中的rmi怎么写_java项目中用到的rmi实例

package com.csii.rmi.server;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.rmi.server.RMIClientSocketFactory;

import java.rmi.server.RMIServerSocketFactory;

import java.util.List;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.csii.rmi.RemoteObjectDescriptor;

import com.csii.rmi.RemoteSecurityManager;

import com.csii.rmi.util.Utils;

import com.csii.service.comm.Server;

import com.csii.service.comm.rmi.RMINaming;

public class SimpleRMIServer implements Server

{

protected Log log = LogFactory.getLog(getClass());

private String registryHost = "127.0.0.1";

//注册端口

private int registryPort = 10099;

private String registHostIp;

private RMIServerSocketFactory rmiServerSocketFactory;

private RMIClientSocketFactory rmiClientSocketFactory;

//传输端口

private int port = 10098;

private boolean alive = false;

//远程对象集合

private RemoteObjectDescriptor[] remoteObjects;

public void setRegistHostIp(String registHostIp){

this.registHostIp = registHostIp;

}

// private boolean isListening(String registryHost, int registryPort)

// {

// try

// {

// Registry registry = LocateRegistry.getRegistry(registryHost, registryPort, this.rmiClientSocketFactory);

//

// Remote testService = new Remote()

// {

// };

// Remote remote = UnicastRemoteObject.exportObject(testService, this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);

// try

// {

// registry.rebind("InnerTest", remote);

// }

// catch (Exception e)

// {

// return false;

// }

// try

// {

// registry.unbind("InnerTest");

// }

// catch (Exception e) {

// this.log.error("unbind", e);

// return false;

// }

//

// return true;

// } catch (Exception e1) {

// }

// return false;

// }

public synchronized void start(){

if (isAlive()){

return;

}

try{

//公网访问ip

System.setProperty("java.rmi.server.hostname", this.registHostIp);

if (this.log.isDebugEnabled())

this.log.debug("cannot find rmiRegistry at " + this.registryHost + ":" + this.registryPort);

if (!Utils.isLocalAddress(this.registryHost))

throw new RuntimeException("Cannot located remote Registry Server.");

try{

if (this.log.isDebugEnabled())

this.log.debug("create rmiRegistry at " + this.registryHost + ":" + this.registryPort);

//在本地主机上创建并导出 Registry 实例,该本地主机使用的是与该实例通信的自定义套接字工厂

LocateRegistry.createRegistry(this.registryPort, this.rmiClientSocketFactory, this.rmiServerSocketFactory);

//安全设置

RemoteSecurityManager.setup();

} catch (Exception e) {

throw new RuntimeException("Cannot started Registry Server.", e);

}

//返回本地创建的指定 host 和 port 上对远程对象 Registry 的远程引用。

Registry registry = LocateRegistry.getRegistry(this.registryHost, this.registryPort, this.rmiClientSocketFactory);

if (this.log.isDebugEnabled()) {

this.log.debug("find rmiRegistry" + registry);

}

for (int i = 0; i < this.remoteObjects.length; i++){

RemoteObjectDescriptor desc = this.remoteObjects[i];

try{

RMINaming.rebind(desc.getName(), desc.getBean(), desc.getInterfaces(), this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);

}catch (Exception e){

this.log.error("rebind fail, try to rebind ", e);

RMINaming.unbind(desc.getName(), this.rmiClientSocketFactory);

RMINaming.bind(desc.getName(), desc.getBean(), desc.getInterfaces(), this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);

}

}

this.alive = true;

this.log.info("Started... " + this);

}catch (Exception e){

this.log.error("start", e);

throw new RuntimeException(e);

}

}

public boolean isAlive()

{

return this.alive;

}

public synchronized void shutdown()

{

if (!isAlive()) {

return;

}

this.alive = false;

try

{

Registry registry = LocateRegistry.getRegistry(this.registryHost, this.registryPort, this.rmiClientSocketFactory);

for (int i = 0; i < this.remoteObjects.length; i++)

{

RemoteObjectDescriptor desc = this.remoteObjects[i];

RMINaming.unbind(desc.getName(), this.rmiClientSocketFactory);

}

}

catch (Exception e)

{

this.log.error("shutdown", e);

}

}

public void setRmiServerSocketFactory(RMIServerSocketFactory rmiServerSocketFactory)

{

this.rmiServerSocketFactory = rmiServerSocketFactory;

}

public void setRmiClientSocketFactory(RMIClientSocketFactory rmiClientSocketFactory)

{

this.rmiClientSocketFactory = rmiClientSocketFactory;

}

public void setRemoteObjects(RemoteObjectDescriptor[] remoteObjects)

{

this.remoteObjects = remoteObjects;

}

public void setRegistryHost(String registryHost)

{

this.registryHost = registryHost;

}

public void setRegistryPort(int registryPort) {

this.registryPort = registryPort;

}

public void setPort(int port) {

this.port = port;

}

public void setRemoteObjectList(List list)

{

this.remoteObjects = ((RemoteObjectDescriptor[])list.toArray(new RemoteObjectDescriptor[0]));

}

public void restart()

{

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值