spring整合ActiveMQ
2016-12-27 16:48
288 查看
一、准备工作
1、去官方网站下载:http://activemq.apache.org/;
2、在该目录下apache-activemq-5.14.2\bin\win64,运行activemq.bat;
3、本地启动后访问http://localhost:8161/,默认端口是8161,会有登录信息,需要输入帐号密码,新建一个queue,命名为FirstQueue;
二、Spring配置
好啦,准备工作已经完成,接下来是xml文件中的配置
1、pom文件中引入依赖的包 <!-- activemq -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
2、新建一个spring的配置文件applicationContext-jms.xml,同时在applicationContext.xml中引入该文件
<import resource="applicationContext-jm
4000
s.xml"></import>
下面是applicationContext-jms.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName">
<!-- 配置connectionFactory -->
<bean id="jmsFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://127.0.0.1:61616</value>
</property>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
<property name="defaultDestinationName" value="FirstQueue" />
<!-- 区别:它采用的模式为false是p2p,为true是订阅 -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="FirstQueue" />
</bean>
</beans>
三、Java代码
1、发送消息的生产者
@Component
public class HelloSender {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
public Destination destination;
public void sendMessage(){
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
}
});
System.out.println("成功发送了一条JMS消息");
}
}
2、接收消息的消费者
@Component
public class ProxyJMSConsumer {
public ProxyJMSConsumer() {
}
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
public Destination destination;
public void recive() {
try {
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);
if (null != txtmsg) {
System.out.println("[DB Proxy] " + txtmsg);
System.out.println("[DB Proxy] 收到消息内容为: "
+ txtmsg.getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、Controller
四、测试
URL中加/send就会调用发送消息的方法,加/receive就会调用接收消息的方法。
初次接触,如有错误还请各位大神指出,谢谢>_<
1、去官方网站下载:http://activemq.apache.org/;
2、在该目录下apache-activemq-5.14.2\bin\win64,运行activemq.bat;
3、本地启动后访问http://localhost:8161/,默认端口是8161,会有登录信息,需要输入帐号密码,新建一个queue,命名为FirstQueue;
二、Spring配置
好啦,准备工作已经完成,接下来是xml文件中的配置
1、pom文件中引入依赖的包 <!-- activemq -->
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<properties> <org.springframework-version>4.2.6.RELEASE</org.springframework-version> </properties>
2、新建一个spring的配置文件applicationContext-jms.xml,同时在applicationContext.xml中引入该文件
<import resource="applicationContext-jm
4000
s.xml"></import>
下面是applicationContext-jms.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName">
<!-- 配置connectionFactory -->
<bean id="jmsFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://127.0.0.1:61616</value>
</property>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
<property name="defaultDestinationName" value="FirstQueue" />
<!-- 区别:它采用的模式为false是p2p,为true是订阅 -->
<property name="pubSubDomain" value="true" />
</bean>
<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="FirstQueue" />
</bean>
</beans>
三、Java代码
1、发送消息的生产者
@Component
public class HelloSender {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
public Destination destination;
public void sendMessage(){
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
}
});
System.out.println("成功发送了一条JMS消息");
}
}
2、接收消息的消费者
@Component
public class ProxyJMSConsumer {
public ProxyJMSConsumer() {
}
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
public Destination destination;
public void recive() {
try {
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);
if (null != txtmsg) {
System.out.println("[DB Proxy] " + txtmsg);
System.out.println("[DB Proxy] 收到消息内容为: "
+ txtmsg.getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、Controller
@Controller public class TestController { @Autowired HelloSender helloSender; @Autowired ProxyJMSConsumer proxyJMSConsumer; @RequestMapping(value = "/send", method = RequestMethod.GET) @ResponseBody public String send(Model model){ helloSender.sendMessage(); return "send"; } @RequestMapping(value = "/receive", method = RequestMethod.GET) @ResponseBody public String receive(Model model){ proxyJMSConsumer.recive(); return "receive"; } }
四、测试
URL中加/send就会调用发送消息的方法,加/receive就会调用接收消息的方法。
初次接触,如有错误还请各位大神指出,谢谢>_<
相关文章推荐
- ActiveMQ整合spring
- Spring整合JMS(一)——基于ActiveMQ实现
- 快速上手MQ之activeMQ(2)--spring整合
- ActiveMQ与Spring的整合
- 深入浅出JMS--Spring和ActiveMQ整合的完整实例
- 深入浅出ActiveMQ(四)--Spring和ActiveMQ整合的完整实例
- Spring整合ActiveMQ
- Spring和Apache CXF,ActiveMQ整合记录
- Spring ActiveMQ 整合(四): JMS 事务管理
- activemq与spring整合
- Spring和ActiveMQ整合的完整实例
- Spring整合JMS——基于ActiveMQ实现(附源码)
- spring整合JMS - 基于ActiveMQ实现
- 利用Spring与ActiveMQ整合发送、接收消息实例(Queue与Topic模式)
- Spring2.5整合ActiveMQ 5.2(P2P文本消息)
- springboot整合activemq,应答模式,消息重发机制,消息持久化
- 详解Java消息队列-Spring整合ActiveMq
- Spring整合Jms学习(一)_基于ActiveMQ实现
- ActiveMQ与Spring整合
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例