07_ActiveMQ_连接池与发送工具类配置

需要的依赖


    commons-pool
    commons-pool
    1.6


    org.apache.activemq
    activemq-broker
    5.14.5


    org.apache.activemq
    activemq-client
    5.14.5

  
    org.apache.activemq  
    activemq-pool  
    5.7.0  
      
         
           org.apache.geronimo.specs  
           geronimo-jms_1.1_spec  
         
      

config.properties配置

passenger.flow.enabled=true
passenger.flow.username=admin
passenger.flow.password=admin
#ip地址和端口可以更改,其他按照格式来
passenger.flow.brokerurl=failover:(tcp://200.200.6.19:61616)?randomize=false
​​​​​​​passenger.flow.queuename=TDQUEUE

 

连接池配置

import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnection;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.apache.log4j.Logger;

public final class ActiveMQPoolsUtil {
	private static final Logger sysLogger = Logger.getLogger(ActiveMQPoolsUtil.class);

	private final static int MAXIMUMACTIVE 	= 200;  // session数
	private final static int IDLETIMEOUT 	= 1200; // 超时时间
	private final static int MAXCONNECTIONS = 50;   // 最大连接数
	/**
	 * 连接
	 */
	private static PooledConnection connection;

	private ActiveMQPoolsUtil() {
	}

	// 初始化连接池等工作
	static {

		String userName = PropertiesUtil.Intance.GetProperty("passenger.flow.username", "META-INF/props/config");
		String userPassowrd = PropertiesUtil.Intance.GetProperty("passenger.flow.password", "META-INF/props/config");
		String brokerUrl = PropertiesUtil.Intance.GetProperty("passenger.flow.brokerurl", "META-INF/props/config");

		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
		activeMQConnectionFactory.setUserName(userName);
		activeMQConnectionFactory.setPassword(userPassowrd);
		activeMQConnectionFactory.setBrokerURL(brokerUrl);

		try {
			PooledConnectionFactory poolFactory = new PooledConnectionFactory(activeMQConnectionFactory);

			poolFactory.setMaximumActiveSessionPerConnection(MAXIMUMACTIVE);
			poolFactory.setIdleTimeout(IDLETIMEOUT);
			poolFactory.setMaxConnections(MAXCONNECTIONS);
			poolFactory.setBlockIfSessionPoolIsFull(true);
			connection = (PooledConnection) poolFactory.createConnection();
			// 必须start,否则无法接收消息
			connection.start();
		} catch (JMSException e) {
			sysLogger.error("创建MQ连接池出错", e);
		}
	}

	/**
	 * 关闭连接
	 */
	public static void close() {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (JMSException e) {
			sysLogger.error("【error】connection close error", e);
		}
	}

	/**
	 * 关闭session
	 */
	public static void closeSession(Session session) {
		try {
			if (session != null) {
				session.close();
			}
		} catch (JMSException e) {
			sysLogger.error("【error】SESSION close error", e);
		}
	}

	/**
	 * 获取一个连接
	 */
	public static PooledConnection getConnection() {
		return connection;
	}

	/**
	 * 设置连接
	 */
	public static void setConnection(PooledConnection connection) {
		ActiveMQPoolsUtil.connection = connection;
	}
}


 

发送工具类配置

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.pool.PooledConnection;
import org.apache.log4j.Logger;

public class ActiveMQSingleUtils {

    private static final Logger sysLogger = Logger.getLogger(ActiveMQSingleUtils.class);
    //一个操作会话
    private static Session SESSION;
    //目的地,其实就是连接到哪个队列,如果是点对点,那么它的实现是Queue,如果是订阅模式,那它的实现是Topic
    private static Destination DESTINATION;
    //生产者,就是产生数据的对象
    private static MessageProducer PRODUCER;
    
    public static void sendMessage(String message){
        sysLogger.info("【info】begin sendMessage, activeMQValidationVO : " + message);
        try {
        	PooledConnection connection = ActiveMQPoolsUtil.getConnection();
        	try {
        	    //第一个参数false代表不支持事务,反之支持,开启支持的话此工具类无法发送消息
        		SESSION = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);				
			} catch (Exception e) {
				sysLogger.error("【error】sendTextMsg close error", e);
			}
			String queueName = PropertiesUtil.Intance.GetProperty("passenger.flow.queuename",
		                "META-INF/props/config");
	        DESTINATION = SESSION.createQueue(queueName);
	        PRODUCER = SESSION.createProducer(DESTINATION);
	        TextMessage textMsg = SESSION.createTextMessage(message);
            PRODUCER.send(textMsg);
		} catch (JMSException e) {
			sysLogger.error("【error】sendTextMsg close error", e);
		} finally{
			ActiveMQPoolsUtil.closeSession(SESSION);
		}
    }
}

 

 

 

发送

ActiveMQSingleUtils.sendMessage(messageJson.toJSONString());

验证

http://activemq.apache.org/activemq-5154-release.html 可以下载MQ服务端查看队列消息

 

你可能感兴趣的:(ActiveMQ)