第一步:pom.xml的配置
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.2</version>
</dependency>
</dependencies>
第二步:applicationcontext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
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-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd ">
<!-- 指定Spring框架扫描的包 -->
<context:component-scan base-package="com.itma.activemq" />
<!--
################################# 指定ActiveMQ 连接工厂 ########################################
真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供
0 username 1 password 2 url
-->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="admin"></constructor-arg>
<constructor-arg index="1" value="admin"></constructor-arg>
<constructor-arg index="2" value="tcp://localhost:61616"></constructor-arg>
</bean>
<!--
################################# 指定Spring Caching连接工厂 #################################
-->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean>
<!--
################################# 指定消息的生产者 #################################
-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="connectionFactory" />
<!-- 定义JmsTemplate的Queue类型 -->
<!--true : 发布/订阅模式 也叫主题模式-->
<!--false : 队列模式 也叫P2P点对点模式 -->
<property name="pubSubDomain" value="false" />
</bean>
<!--
################################# 指定消息的消费者 #################################
-->
<!--
destination-type:监听消息的类型
queue:队列模式 也叫P2P点对点模式
topic:发布/订阅模式 也叫主题模式
container-type:容器的类型(可以使用默认值)
acknowledge:应答机制
-->
<jms:listener-container destination-type="queue"
container-type="default" connection-factory="connectionFactory"
acknowledge="auto">
<!--
注入消费者
destination:队列的名字
ref:引用对象,指定真正的消费者是谁
-->
<jms:listener destination="hello" ref="consumer" />
</jms:listener-container>
</beans>
创建一个生产者的类
//加上这个注解 让spring进行管理
@Component
public class Produce {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(final String msg){
//destinationName 队列的名字 send(destinationName, messageCreator)
//messageCreator 创建消息
jmsTemplate.send("hello",new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage(msg);
return message;
}
});
}
}
再创建一个消费者的类
@Component
public class Consumer implements MessageListener{
@Override
public void onMessage(Message msg) {
TextMessage message = (TextMessage) msg;
try {
String text = message.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
最后创建一个测试类 测试一下 大概就入门了
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationcontext.xml")
public class MqTest {
//注入生产者
@Autowired
private Produce produce;
@Test
public void test(){
produce.sendMessage("hello spring");
//生产者这里发出消息后 ,消费者那将会执行打印出 hello spring
}
}