标签:成功 ace rri support temp work factor maven构建项目 缓存
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zxy</groupId>
<artifactId>spring-activemq</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-activemq</name>
<dependencies>
<!-- spring-jms 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- activemq依赖 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zxy</groupId>
<artifactId>spring-activemq</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-activemq</name>
<dependencies>
<!-- spring-jms 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- activemq依赖 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启包扫描 (减少在xml中注册bean)-->
<context:component-scan base-package="com.zxy.mq" />
<!-- #### ActiveMq配置 start ####-->
<!-- 1. ActiveMq连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!--2. Spring Caching 连接工厂(类似数据库线程池的东西,减少连接的创建。) -->
<!-- 由于jmsTemplate每次发送消息都需要创建连接和创建session了,所以引入这个类似连接池的连接工厂,优化Mq的性能 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标连接工厂 指向 ActiveMq工厂 -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory" />
<!-- session缓存的最大个数-->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 3.配置jmsTemplate,用于发送发送mq消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 设置 jmsTemplate 不支持订阅模式,即:只支持queue模式。
如果项目需要同时支持queue和topic,那么就需要另外注册一个jmsTemplate(把pubSubDomain设为true)-->
<property name="pubSubDomain" value="false"></property>
</bean>
<!-- 4.定义Queue监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactory">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
</jms:listener-container>
<!-- #### ActiveMq配置 end ####-->
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启包扫描 (减少在xml中注册bean)-->
<context:component-scan base-package="com.zxy.mq" />
<!-- #### ActiveMq配置 start ####-->
<!-- 1. ActiveMq连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!--2. Spring Caching 连接工厂(类似数据库线程池的东西,减少连接的创建。) -->
<!-- 由于jmsTemplate每次发送消息都需要创建连接和创建session了,所以引入这个类似连接池的连接工厂,优化Mq的性能 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标连接工厂 指向 ActiveMq工厂 -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory" />
<!-- session缓存的最大个数-->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 3.配置jmsTemplate,用于发送发送mq消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 设置 jmsTemplate 不支持订阅模式,即:只支持queue模式。
如果项目需要同时支持queue和topic,那么就需要另外注册一个jmsTemplate(把pubSubDomain设为true)-->
<property name="pubSubDomain" value="false"></property>
</bean>
<!-- 4.定义Queue监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactory">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
</jms:listener-container>
<!-- #### ActiveMq配置 end ####-->
</beans>
package com.zxy.mq.producer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* 通用的ActiveMQ queue消息生产者
* @author ZENG.XIAO.YAN
* @time 2018-11-13 10:48:20
* @version v1.0
*/
@Component
public class CommonMqQueueProducer {
@Autowired
private JmsTemplate jmsTemplate;
/**
* 发送点对点的文本类型的Mq消息
* @param queue 队列的名字
* @param message 文本消息(一般直接传输json字符串,所以可以认为文本消息是最通用的)
*/
public void send(String queue, String message) {
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
package com.zxy.mq.producer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* 通用的ActiveMQ queue消息生产者
* @author ZENG.XIAO.YAN
* @time 2018-11-13 10:48:20
* @version v1.0
*/
public class CommonMqQueueProducer {
private JmsTemplate jmsTemplate;
/**
* 发送点对点的文本类型的Mq消息
* @param queue 队列的名字
* @param message 文本消息(一般直接传输json字符串,所以可以认为文本消息是最通用的)
*/
public void send(String queue, String message) {
jmsTemplate.send(queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestAConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueA的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
public class TestAConsumer implements MessageListener {
public void onMessage(Message message) {
// myQueueA的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestBConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueB的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
public class TestBConsumer implements MessageListener {
public void onMessage(Message message) {
// myQueueB的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxy.mq.producer.CommonMqQueueProducer;
/**
* MQ消息测试类
* @author ZENG.XIAO.YAN
* @time 2018-11-15 14:04:35
* @version v1.0
*/
public class MqTestDemo {
private static ApplicationContext applicationContext;
// 静态代码块加载Spring容器
static {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("spring 容器已启动。。。");
}
public static void main(String[] args) {
CommonMqQueueProducer mqQueueProducer = applicationContext.getBean(CommonMqQueueProducer.class);
for (int i = 1; i < 11; i++) {
// 奇数给myQueueA发,偶数给myQueueB发
if (i % 2 == 1) {
mqQueueProducer.send("myQueueA", "Mq消息A" + i);
} else {
mqQueueProducer.send("myQueueB", "Mq消息B" + i);
}
}
}
}
package com.zxy.mq.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxy.mq.producer.CommonMqQueueProducer;
/**
* MQ消息测试类
* @author ZENG.XIAO.YAN
* @time 2018-11-15 14:04:35
* @version v1.0
*/
public class MqTestDemo {
private static ApplicationContext applicationContext;
// 静态代码块加载Spring容器
static {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("spring 容器已启动。。。");
}
public static void main(String[] args) {
CommonMqQueueProducer mqQueueProducer = applicationContext.getBean(CommonMqQueueProducer.class);
for (int i = 1; i < 11; i++) {
// 奇数给myQueueA发,偶数给myQueueB发
if (i % 2 == 1) {
mqQueueProducer.send("myQueueA", "Mq消息A" + i);
} else {
mqQueueProducer.send("myQueueB", "Mq消息B" + i);
}
}
}
}
Spring整合ActiveMQ及多个Queue消息监听的配置
标签:成功 ace rri support temp work factor maven构建项目 缓存
原文地址:https://www.cnblogs.com/zeng1994/p/5f9cd67f9ca155e8329263a2e5467aa4.html