您的位置:首页 > 编程语言 > Java开发

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>
<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就会调用接收消息的方法。

初次接触,如有错误还请各位大神指出,谢谢>_<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: