⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trappublisher.java.svn-base

📁 this is example use EJB with jboss.
💻 SVN-BASE
字号:
/*
 * 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.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.naming.NamingException;

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.vo.TrapVO;

/**
 * Class name 	: TrapPublisher <BR>
 * 
 * Package 		: jp.co.ntt_at.awgview.server.jms <BR>
 * 
 * Description	: This class provides public trap to client using JMS Topic<BR>
 * 
 * @author 		: AI&T
 * @version 	: 1.0
 */
public class TrapPublisher {

	private static TopicConnection connection = null;
	private static MessageProducer msgProducer = null;
	private static Session session = null;

	private boolean connected = false;

	private static final String trapTopicName = "topic/awgview.TrapTopic";
	
	/**
	 * 
	 * @return boolean
	 * @throws NamingException
	 * @throws JMSException
	 */
	public boolean init() throws NamingException, JMSException {
		
		if (isConnectedToSession()) {
			return true;
		}

		setConnectedToSession(false);

		try {
			connection = JndiUtils.getTopicConnection();
			
			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!");                                       
                    System.exit(Constants.SYSTEM_EXIT);
                }			    
			});
			
			if (connection != null) {
				session = connection.createSession(false,
						Session.AUTO_ACKNOWLEDGE);

				Topic trapTopic = null;
				trapTopic = JndiUtils.getTopic(trapTopicName);

				if (trapTopic != null) {
					msgProducer = session.createProducer(trapTopic);
					setConnectedToSession(true);
					return true;
				}
			} else {
				LogWriter.getSNMPLogger().error("Cannot init JMS to public Trap. Ability to JBoss server has not run.");
			}
		} catch (NamingException e) {
			if (LogWriter.getSNMPLogger().isDebugEnabled()) {
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
			LogWriter.getSNMPLogger().error("Cannot init JMS to public Trap. Ability to JBoss server has not run.");
			LogWriter.getSNMPLogger().error("Exception occurred: " + e.toString());
			throw e;
		} catch (JMSException e) {
			if (LogWriter.getSNMPLogger().isDebugEnabled()) {
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
			LogWriter.getSNMPLogger().error("Cannot init JMS to public Trap. Ability to JBoss server has not run.");
			LogWriter.getSNMPLogger().error("Exception occurred: " + e.toString());
			throw e;
		}
		return false;
	}

	/**
	 * Publish given message to the JMS topic
	 * 
	 * @param trapVO
	 *            : Object message to the JMS Topic
	 * @return boolean
	 */
	public boolean publish(TrapVO trapVO) throws NamingException, JMSException {
		LogWriter.getSNMPLogger().info("Starting publish trapvo ...");
		if (!isConnectedToSession()) {
			try {
				init();
				if (!isConnectedToSession()) {					
					LogWriter.getSNMPLogger().error("Error when publish TrapVO.");	
					LogWriter.getSNMPLogger().error("Ability to JBoss server has not run or configuration is invalid. Pls contact with admin");
					return false;
				}
			} catch (NamingException e) {
				LogWriter.getSNMPLogger().error(e.toString());
				if (LogWriter.getSNMPLogger().isDebugEnabled()) {
					LogWriter.getSNMPLogger().debug(Utils.parseException(e));
				}
				return false;
			} catch (JMSException e) {
				LogWriter.getSNMPLogger().error(e.toString());
				if (LogWriter.getSNMPLogger().isDebugEnabled()) {
					LogWriter.getSNMPLogger().debug(Utils.parseException(e));
				}
				return false;
			}
		}

		try {
			// Send a Trap Object
			ObjectMessageCreator msgCreator = new ObjectMessageCreator();
			Message objMessage = msgCreator.createObjectMessage(session, trapVO);
			if (objMessage != null) {
				msgProducer.send(objMessage);
				LogWriter.getSNMPLogger().info("Publish Alarm is success");
				return true;
			}else{
				LogWriter.getSNMPLogger().info("Publish Alarm failed");
				LogWriter.getSNMPLogger().error("Error when create Message Object to public");				
			}
		} catch (Exception e) {
			if (LogWriter.getSNMPLogger().isDebugEnabled()) {
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
			LogWriter.getSNMPLogger().error("Error when publish TrapVO: " + e.toString());
			LogWriter.getSNMPLogger().error("Pls contact with admin");			
		}
		return false;
	}

	/**
	 * 
	 * These methods set/return a flag that indicates whether the application is
	 * currently involved in a session.
	 *
	 * @param isConnected
	 */
	public void setConnectedToSession(boolean isConnected) {
		connected = isConnected;
	}

	public boolean isConnectedToSession() {
		return (this.connected);
	}

	/*
	 * Perform the JMS cleanup.
	 */
	public void doDisconnect() {
		if (!isConnectedToSession()) {
			return;
		}
		destroySession();
		setConnectedToSession(false);
	}

	/*
	 * Destroy/close session.
	 */
	private void destroySession() {
		try {
			// Close Producer
			if (msgProducer != null) {
				msgProducer.close();
			}

			// Close session
			if (session != null) {
				session.close();
			}

			// Close Connection
			if (connection != null) {
				connection.close();
			}

			msgProducer = null;
			session = null;
			connection = null;
			System.gc();
		} catch (Exception e) {
			LogWriter.getSNMPLogger().error(e.toString());
			if (LogWriter.getSNMPLogger().isDebugEnabled()) {
				LogWriter.getSNMPLogger().debug(Utils.parseException(e));
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -