rmi

RmiMonitorService.java
package nbpt.ts.manager.message.service;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
* Description: 实时显示RMI服务接口.
*
* RMI接口必须扩展接口java.rmi.Remote
*
* @author Peter Wei
* @version 1.0 Feb 25, 2009
*/
public interface RmiMonitorService extends Remote {
/**
* 实时显示对外接口
*
* @param funindex
* 功能号
* @param param
* 键名列表,也就是实际传输的内容
* @return
* @throws RemoteException
* 远程接口方法必须抛出java.rmi.RemoteException
*/
public int interactive(int funindex, String param) throws RemoteException;
}
RmiMonitorServiceImpl.java
package nbpt.ts.manager.message.service.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
// import nbpt.ts.manager.base.util.AppContext;
import nbpt.ts.manager.message.service.RmiMonitorService;
import nbpt.ts.manager.message.service.WarnService;
/**
* Description: 实时显示RMI接口实现.
*
* 实现RMI接口及远程方法(继承UnicastRemoteObject)
*
* @author Peter Wei
* @version 1.0 Feb 25, 2009
*/
public class RmiMonitorServiceImpl extends UnicastRemoteObject implements
RmiMonitorService {

private static final long serialVersionUID = -3771656108378649574L;

public static final int SUCCSS = 1;

public static final int FAIL = 0;

public WarnService warnService;

/**
* 必须定义构造方法,因为要抛出RemoteException异常
*
* @throws RemoteException
*/
public RmiMonitorServiceImpl() throws RemoteException {
super();
}

public int interactive(int funindex, String param) throws RemoteException {

int result = FAIL;
switch (funindex) {
// 告警
case (1): {

// warnService = (WarnService) AppContext.getAppContext().getBean(
// "warn.warnService");
// 实际应用是从Spring应用中获取告警Service,如上代码
warnService = new WarnServiceImpl();
// 网络告警的业务操作
warnService.dealWarn(param);
result = SUCCSS;
}
break;
case (2):
// do other biz
break;
}
// ......

return result;
}

public WarnService getWarnService() {
return warnService;
}

public void setWarnService(WarnService warnService) {
this.warnService = warnService;
}

}

RmiServer.java
package nbpt.ts.manager.message.service;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import nbpt.ts.manager.message.service.impl.RmiMonitorServiceImpl;
/**
* Description: RMI服务端.
*
* @author Peter Wei
* @version 1.0 Feb 25, 2009
*/
public class RmiServer {

public String ip = "localhost";

public int port = 8889;

/**
* 启动RMI注册服务,并注册远程对象.实际应用中是在Spring初始化并启动
*/
public void init() {
try {
LocateRegistry.createRegistry(port);
// 创建一个远程对象
RmiMonitorService comm = new RmiMonitorServiceImpl();
Naming.bind("//" + ip + ":" + port + "/comm", comm);
} catch (RemoteException e) {
System.out.println("创建远程对象发生异常!" + e.toString());
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println("发生重复绑定对象异常!" + e.toString());
e.printStackTrace();
} catch (MalformedURLException e) {
System.out.println("发生URL畸形异常!" + e.toString());
e.printStackTrace();
}
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public static void main(String[] args) {
// 实际应用中是在Spring初始化并启动
RmiServer rmiServer = new RmiServer();
System.out.println("RMI服务初始化:");
rmiServer.init();

}
}
MonitorClient.java
package nbpt.ts.manager.message.service;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
/**
* Description: RMI客户端.
*
* @author Peter Wei
* @version 1.0 Feb 25, 2009
*/
public class MonitorClient {

public RmiMonitorService monitorService;

public String ip = "localhost";

public int port = 8889;

public int interactive(int funindex, String param) {
int result = 0;
try {
getMonitorService().interactive(funindex, param);
result = 1;
} catch (RemoteException e) {
e.printStackTrace();
}
return result;
}

public RmiMonitorService getMonitorService() {
try {
// 在RMI服务注册表中查找名称为RmiMonitorService的对象,并调用其上的方法
monitorService = (RmiMonitorService) Naming.lookup("rmi://" + ip
+ ":" + port + "/comm");

} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return monitorService;
}

public static void main(String args[]) throws RemoteException {
MonitorClient client = new MonitorClient();
System.out.println("发送告警信息:");
String msg = "tsid=1022&devid=10001027&warnid=102&warntype=01&warnlevel=1&warnmsg=设备出错,请检查.";
System.out.println(client.getValue(msg, "warnmsg"));
client.interactive(1, msg);

}

public String getValue(String content, String key) {
String value = "";

int begin = 0, end = 0;
begin = content.indexOf(key + "=");
end = content.indexOf("&", begin);

if (end == -1)
end = content.length();
value = content.substring(begin + key.length() + 1, end);
return value;

}
}

WarnService.java
package nbpt.ts.manager.message.service;

/**
* Description: 告警服务
*
* @author Peter Wei
* @version 1.0 2010-8-22
*/
public interface WarnService {

/**
* 处理告警:告警来时的业务操作,实际操作是解析消息存库,然后界面Ajax定时刷新数据,获取实时告警展示
*
* @param message
* @return
*/
public int dealWarn(String message);
}
WarnServiceImpl.java
package nbpt.ts.manager.message.service.impl;

import nbpt.ts.manager.message.service.WarnService;

/**
* Description: 告警服务
*
* @author Peter Wei
* @version 1.0 2010-8-22
*/
public class WarnServiceImpl implements WarnService {

public int dealWarn(String message) {
// 告警处理方法
System.out.println("已接收网络告警");
// …
return 1;
}

}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页