JMS

package com.zte.soa.util;

import java.io.Serializable;
import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
* JMS消息队列客户端
*
* @author zcf
*
*/
public class JMSClient {
/** 连接工厂JNDI */
public static String CONNECTION_FACTORY_JNDI_NAME = "connection.factory";
/** 队列JNDI */
public static String QUEUE_JNDI_NAME = "queue";
// JMS连接
private Queue queue;
private QueueConnection conn;
private QueueSession session;
private QueueSender sender;

// JMS环境
private Hashtable<String, String> environment;

/**
* 构造方法
*
* @param propertyFile
*/
public static JMSClient createJMSClient(String propertyFile) {
JMSClient client = new JMSClient(propertyFile);
if (null == client.sender) {
return null;
} else {
return client;
}
}

/**
* 发送系列化对象消息
*
* @param msg
* @throws JMSException
*/
public void sendObjectMessage(Serializable msg) throws JMSException {
sender.send(session.createObjectMessage(msg));
}

/**
* 发送文本消息
*
* @param msg
*/
public void sendTextMessage(String msg) {
try {
sender.send(session.createTextMessage(msg));
} catch (JMSException e) {
e.printStackTrace();
}
}

/**
* 关闭所有连接
*/
public void closeAll() {
try {
sender.close();
} catch (JMSException e) {
e.printStackTrace();
}

if (null != session) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (null != conn) {
try {
conn.close();
} catch (JMSException e) {
e.printStackTrace();
}
}

sender = null;
session = null;
conn = null;
}

/**
* 重新连接
*/
public void reconnection() {
this.closeAll();
newInstance();
}

/**
* 是否连接上
*
* @return
*/
public boolean isConnectiond() {

return sender != null;
}

// 初始化
private JMSClient(String propertyFile) {
// 读取配置文件
environment = PropertiesUtil.readToHashtable(propertyFile);
newInstance();
}

private JMSClient() {
newInstance();
}

private void newInstance() {
InitialContext ctx = null;
try {
ctx = new InitialContext(environment);
} catch (NamingException e) {
e.printStackTrace();
return;
}

// 队列
try {
queue = (Queue) ctx.lookup(environment.get(QUEUE_JNDI_NAME));
} catch (NamingException e) {
e.printStackTrace();
return;
}
// 连接工厂
QueueConnectionFactory factory = null;
try {
factory = (QueueConnectionFactory) ctx.lookup(environment
.get(CONNECTION_FACTORY_JNDI_NAME));
} catch (NamingException e) {
e.printStackTrace();
return;
}
// 连接
try {
conn = factory.createQueueConnection();
} catch (JMSException e) {
e.printStackTrace();
return;
}

// 会话
try {
session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
return;
}

// 发送器
try {
sender = session.createSender(queue);
} catch (JMSException e) {
e.printStackTrace();
}

if (null != ctx) {
try {
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}

public Hashtable<String, String> getEnvironment() {
return environment;
}
}

 

package com.zte.soa.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Properties;

public class PropertiesUtil {
/**
* 读取properties属性
*
* @param filePath
* @param key
* @return
*/
public static String readProperties(String filePath, String key) {
return readProperties(filePath, key, null);
}

/**
* 读取properties属性
*
* @param filePath
* @param key
* @param defaultValue
* @return
*/
public static String readProperties(String filePath, String key,
String defaultValue) {
Properties prop = new Properties();
InputStream fis;
try {
fis = new FileInputStream(filePath);
prop.load(fis);
return prop.getProperty(key, defaultValue);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 读取properties文件到Hashtable
*
* @param filePath
* @return
*/
public static Hashtable<String, String> readToHashtable(String filePath) {
Hashtable<String, String> table = null;
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
table = new Hashtable<String, String>();
for (Object key : prop.keySet()) {
table.put((String) key, (String) prop.get(key));
}
} catch (Exception e) {
e.printStackTrace();
}

return table;
}

/**
* 写入properties信息
*
* @param filePath绝对路径
* @param key
* @param value
*/
public static void writeProperties(String filePath, String key, String value) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(key, value);
prop.store(fos, null);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 写入properties信息
*
* @param filePath绝对路径
* @param table
*/
public static void writeProperties(String filePath,
Hashtable<String, String> table) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
for (String key : table.keySet()) {
prop.setProperty(key, table.get(key));
}
prop.store(fos, null);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

package com.zte.soa.systemmonitor;

import java.io.File;
import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SystemMonitorServlet extends HttpServlet {
private static final long serialVersionUID = 299471078752275038L;

// 重启定时器
private static String RESTART = "restart";
// 停止定时器
private static String STOP = "stop";

@EJB
private SystemMonitor systemMonitor; // 通过EJB注入系统监控Bean

public void init(ServletConfig config) throws ServletException {
super.init(config);
// 停止老的定时服务
systemMonitor.stop();
// JMS配置文件
String eventFile = config.getServletContext().getRealPath("/");

if (null == eventFile) {
// WebLogic方式
eventFile = this.getClass().getClassLoader().getResource("/")
.getPath();
} else {
// OC4J方式
eventFile = eventFile.replace(config.getServletContext()
.getServletContextName() + File.separator, "APP-INF"
+ File.separator + "classes" + File.separator);
}

// 启动系统监控
systemMonitor.start(eventFile);
}

// 控制定时器
private synchronized void runModel(HttpServletRequest req) {
String model = req.getParameter("model");
if (null != model) {
// 重新启动监控
if (RESTART.equalsIgnoreCase(model)) {
systemMonitor.restart();
}
// 停止监控
else if (STOP.equalsIgnoreCase(model)) {
systemMonitor.stop();
}
}
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
runModel(req);
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
runModel(req);
}

public void destroy() {
super.destroy();
// 停止系统监控
systemMonitor.stop();
}
}

posted @ 2015-03-15 13:15  采姑娘的蘑菇  阅读(230)  评论(0编辑  收藏  举报