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

需要的依赖

<dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.6</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.14.5</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.14.5</version>
</dependency>
<dependency>  
    <groupId>org.apache.activemq</groupId>  
    <artifactId>activemq-pool</artifactId>  
    <version>5.7.0</version>  
    <exclusions>  
       <exclusion>  
           <groupId>org.apache.geronimo.specs</groupId>  
           <artifactId>geronimo-jms_1.1_spec</artifactId>  
       </exclusion>  
    </exclusions>  
</dependency>

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服务端查看队列消息

 

好的,你想了解如何使用Java连接ActiveMQ并查看topic消息,可以按照以下步骤操作: 1. 下载并安装ActiveMQ。可以从官网下载最新的ActiveMQ安装包,并按照安装向导进行安装。 2. 创建Java项目并导入ActiveMQ相关的jar包。可以使用Maven管理项目依赖,添加以下依赖: ```xml <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.16.2</version> </dependency> ``` 3. 创建连接和会话。可以使用以下代码创建连接和会话: ```java // 创建连接工厂 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创建连接 Connection connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ``` 4. 创建topic和消息消费者。可以使用以下代码创建topic和消息消费者: ```java // 创建topic Topic topic = session.createTopic("test.topic"); // 创建消息消费者 MessageConsumer consumer = session.createConsumer(topic); ``` 5. 接收消息。可以使用以下代码接收消息: ```java // 接收消息 Message message = consumer.receive(); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("Received message: " + textMessage.getText()); } ``` 这样就可以使用Java连接ActiveMQ并查看topic消息了。当然,还有其他更详细的操作可以参考ActiveMQ官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值