spring整合ActiveMQ

一、准备工作

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


初次接触,如有错误还请各位大神指出,谢谢>_<


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值