本文不介绍activemq的安装,Windows下安装很简单,下载安装包解压即可
pom.xml文件引入依赖
一定不要向下面这样引入activemq依赖,因为activemq-all依赖中也包括spring依赖,可能会与你搭建项目时引入的spring依赖冲突
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.16.2</version>
</dependency>
所以我们需要单独引入我们所需依赖
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.16.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.16.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.16.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.18</version>
</dependency>
新增spring-activemq.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.5.xsd
">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactoryJms"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 使用pool进行链接 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<!-- <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> -->
<!-- <property name="brokerURL" value="tcp://localhost:61616"/> -->
<!-- </bean> -->
<!-- <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> -->
<!-- <property name="connectionFactory" ref="targetConnectionFactory"/> -->
<!-- <property name="maxConnections" value="10"/> -->
<!-- </bean> -->
<!-- <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> -->
<!-- <property name="targetConnectionFactory" ref="pooledConnectionFactory"/> -->
<!-- </bean> -->
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactoryJms"/>
</bean>
<!-- 4.定义Queue监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactoryJms">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
<jms:listener destination="queue1" ref="InserArticleMQ"/>
<jms:listener destination="queue2" ref="UpdateArticleMQ"/>
</jms:listener-container>
<!--这个是队列目的地 -->
<!-- <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">-->
<!-- <constructor-arg value="queue1"/>-->
<!-- </bean>-->
<!--这个是主题目的地,一对多的-->
<!-- <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">-->
<!-- <constructor-arg value="topic"/>-->
<!-- </bean>-->
<!-- 消息监听器 -->
<bean id="InserArticleMQ"
class="com.fh.experience.service.InserArticleMQ"/>
<bean id="UpdateArticleMQ"
class="com.fh.experience.service.UpdateArticleMQ"/>
<!-- <!– 消息监听容器 –>-->
<!-- <bean id="jmsContainer"-->
<!-- class="org.springframework.jms.listener.DefaultMessageListenerContainer">-->
<!-- <property name="connectionFactory" ref="connectionFactoryJms"/>-->
<!-- <property name="destination" ref="queueDestination"/>-->
<!-- <property name="messageListener" ref="consumerMessageListener"/>-->
<!-- </bean>-->
</beans>
到这里配置就完成了
这里给出一个简单的点对点模型例子参考
生产者:
import com.alibaba.fastjson.JSON;
import com.fh.experience.entity.Article;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JmsProduce {
public static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
public static final String QUEUE_NAME = "queue1";
public static void main(String[] args) throws JMSException {
Article article = new Article();
article.setTitle("mq测试发送对象");
article.setRemark("测试");
article.setAuthor("YR");
//创建连接工厂,按照给定的url地址,采用默认用户名和密码(admin)
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//通过连接工厂,获得连接connection并启动
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
//创建session,两个参数,第一个是事务,第二个是签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建目的地(queue或topic)
Queue queue = session.createQueue(QUEUE_NAME);
//创建消息生产者
MessageProducer messageProducer = session.createProducer(queue);
//使用消息生产者生产消息放入队列
// for (int i = 0; i < 3; i++) {
// TextMessage textMessage = session.createTextMessage("msg" + i);
// messageProducer.send(textMessage);
// }
String jsonString = JSON.toJSONString(article);
TextMessage textMessage = session.createTextMessage(jsonString);
messageProducer.send(textMessage);
//关闭资源
messageProducer.close();
session.close();
connection.close();
System.out.println("发布消息成功");
}
}
消费者:
import com.fh.experience.entity.Article;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
public class JmsConsumer {
public static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
public static final String QUEUE_NAME = "queue1";
public static void main(String[] args) throws JMSException, IOException {
//创建连接工厂,按照给定的url地址,采用默认用户名和密码(admin)
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//通过连接工厂,获得连接connection并启动
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
//创建session,两个参数,第一个是事务,第二个是签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建目的地(queue或topic)
Queue queue = session.createQueue(QUEUE_NAME);
//创建消费者
MessageConsumer messageConsumer = session.createConsumer(queue);
//通过一直等待或等待一段时间的方式来接收生产者消息
// while (true){
// TextMessage textMessage = (TextMessage) messageConsumer.receive();
// if (textMessage != null){
// System.out.println("消费者接收到消息"+textMessage.getText());
// }else {
// break;
// }
// }
//通过监听方式来接收生产者消息
// messageConsumer.setMessageListener(new MessageListener() {
// @Override
// public void onMessage(Message message) {
// if (message != null && message instanceof TextMessage) {
// TextMessage textMessage = (TextMessage) message;
// try {
// System.out.println("消费者接收到消息" + textMessage.getText());
// } catch (JMSException e) {
// e.printStackTrace();
// }
// }
// }
// });
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if (message != null && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("消费者接收到消息" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
//防止运行过快接收不到,保证控制台不灭
System.in.read();
messageConsumer.close();
session.close();
connection.close();
}
}