📄 jmssnmpreplier.java
字号:
/*
* Copyright(C) 2008, NTT AT Co., Ltd.
* Project: AWGView
*
* Notes:
* N/A
*
* Record of change:
* Date Version Name Content
* 2008/12/15 1.0 TriNT First create
*/
package jp.co.ntt.awgview.server.jms;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.naming.NamingException;
import jp.co.ntt.awgview.server.AppMainServer;
import jp.co.ntt.awgview.server.common.LogWriter;
import jp.co.ntt.awgview.server.common.Utils;
import jp.co.ntt.awgview.server.constant.Constants;
import jp.co.ntt.awgview.server.thread.AgentDispatcher;
import jp.co.ntt.awgview.server.vo.SnmpVO;
/**
* Class name : JmsSnmpReplier <BR>
*
* Package : jp.co.ntt_at.awgview.server.jms <BR>
*
* Description : A Message Endpoint that waits to receive the request message;
* when it does, it responds by sending the reply message.<BR>
* The Queue the Replier uses to send the reply message to the Requestor. <BR>
* Implement the javax.jms.MessageListener interface, which includes an
* onMessage() method. <BR>
*
* @author : AI&T
* @version : 1.0
*/
public class JmsSnmpReplier implements MessageListener {
private Session session;
private QueueConnection connection;
private boolean transacted = false;
private static String queueName = "Unkown";
protected JmsSnmpReplier() {
super();
}
/**
* New Replier Object
*
* @param requestQueueName
* : The Queue uses to send the reply message to the Requestor.
* @return JmsSnmpReplier
* @throws JMSException
* @throws NamingException
*/
public static JmsSnmpReplier newReplier(String requestQueueName)
throws JMSException, NamingException {
try {
queueName = requestQueueName;
JmsSnmpReplier replier = new JmsSnmpReplier();
replier.initialize(requestQueueName);
return replier;
} catch (JMSException e) {
LogWriter.getSNMPLogger().error("Can not initialize Jms");
LogWriter.getSNMPLogger().error("Ability to JBoss server has not run " +
"\n\tor Queue [" + requestQueueName +"] does not exist on the JBoss server" +
"\n\tor other awgview server is running. " +
"\n\tPls check again or contact with admin");
LogWriter.getSNMPLogger().error("Exception occurred: " + e.toString());
throw e;
} catch (NamingException e) {
LogWriter.getSNMPLogger().error("Can not initialize Jms");
LogWriter.getSNMPLogger().error("Ability to JBoss server has not run " +
"\n\tor Queue [" + requestQueueName +"] does not exist on the JBoss server" +
"\n\tor other awgview server is running. " +
"\n\tPls check again or contact with admin");
LogWriter.getSNMPLogger().error("Exception occurred: " + e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
throw e;
}
}
/**
* JmsSnmpReplier initialize: - Create all the necessary objects for sending
* messages to a JMS queue. - Set the message listener using the following
* MessageConsumer method, passing the listener information
*
* @param requestQueueName
* : The Queue uses to send the reply message to the Requestor.
* @throws NamingException
* @throws JMSException
*/
protected void initialize(String requestQueueName) throws NamingException,
JMSException {
try {
// Look up a connection factory in JNDI.
connection = JndiUtils.getQueueConnection();
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
connection.setExceptionListener(new ExceptionListener(){
public void onException(JMSException arg0) {
LogWriter.getSNMPLogger().error("Exception code " + arg0.getErrorCode() +
" message = " + arg0.getMessage());
if(LogWriter.getSNMPLogger().isDebugEnabled()){
LogWriter.getSNMPLogger().debug(Utils.parseException(arg0));
}
LogWriter.getSNMPLogger().fatal("JBoss Server interrupted!");
// AppMainServer.shutDown();
System.exit(Constants.SYSTEM_EXIT);
}
});
Destination requestQueue = JndiUtils
.getDestination(requestQueueName);
MessageConsumer requestConsumer = session
.createConsumer(requestQueue);
MessageListener listener = this;
requestConsumer.setMessageListener(listener);
connection.start();
} catch (JMSException e) {
LogWriter.getSNMPLogger().error("Can not Snmp Replier initialize");
LogWriter.getSNMPLogger().error("Exception occurred: " + e.toString());
doDisconnect();
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
throw e;
}
}
/**
* JmsSnmpReplier implement the javax.jms.MessageListener interface, which
* includes an onMessage() method. <BR>
*
*/
public synchronized void onMessage(Message message) {
try {
if (message instanceof ObjectMessage) {
ObjectMessage requestOjbMessage = (ObjectMessage) message;
if (requestOjbMessage.getObject() instanceof SnmpVO) {
try {
SnmpVO snmpVO = (SnmpVO) requestOjbMessage.getObject();
if (snmpVO != null) {
if (AppMainServer.agentDispatcher == null){
AppMainServer.agentDispatcher = new AgentDispatcher();
}
AppMainServer.agentDispatcher.handleOnSnmpCommand(this, message, snmpVO);
}
} catch (Exception e) {
e.printStackTrace();
LogWriter.getSNMPLogger().debug(e.toString());
}
}
}
} catch (JMSException e) {
LogWriter.getSNMPLogger().error(e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
e.printStackTrace();
}
}
/***
* Receive the request message; when it does, it responds by sending the reply message.
*
* @param message
* @param updateSnmpVO
* @return boolean
*/
public boolean replyToClient(Message message, SnmpVO updateSnmpVO) throws NamingException, JMSException {
try {
if (message instanceof ObjectMessage) {
ObjectMessage requestOjbMessage = (ObjectMessage) message;
Destination replyDestination = message.getJMSReplyTo();
if (replyDestination != null) {
if (connection == null){
initialize(queueName);
}
MessageProducer replyProducer = session.createProducer(replyDestination);
// Create object to send
ObjectMessageCreator msgCreator = new ObjectMessageCreator();
Message objMessage = msgCreator.createObjectMessage(session,
updateSnmpVO, requestOjbMessage.getJMSMessageID());
if (objMessage != null) {
replyProducer.send(objMessage);
//LogWriter.getSNMPLogger().info("Data send to client: " + updateSnmpVO.toString());
if (transacted) {
session.commit();
LogWriter.getSNMPLogger().debug("Finished commiting the session.");
}
return true;
} else {
LogWriter.getSNMPLogger().info("Data send to client: " + updateSnmpVO.toString());
LogWriter.getSNMPLogger().error("Cannot create Object Message. Server send to client: Failed.");
return false;
}
} else {
LogWriter.getSNMPLogger().error("replyDestination is null || message.getJMSReplyTo() is null. " +
"Server send to client: False");
return false;
}
}
return false;
} catch (Exception e) {
LogWriter.getSNMPLogger().error(e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
return false;
}
}
/*
* Destroy/close session and connection.
*/
public void doDisconnect() throws JMSException{
try {
// Close session
if (session != null) {
session.close();
}
// Close Connection
if (connection != null) {
connection.close();
}
session = null;
connection = null;
super.finalize();
} catch (JMSException e) {
LogWriter.getSNMPLogger().error(e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
} catch (Exception e) {
LogWriter.getSNMPLogger().error(e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
} catch (Throwable e) {
LogWriter.getSNMPLogger().error(e.toString());
if(LogWriter.getSNMPLogger().isTraceEnabled()){
LogWriter.getSNMPLogger().trace(Utils.parseException(e));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -