activemq初步实践-生产者

最近学习activemq与spring的简单整合,通过连个web项目来模拟消息的发送以及接受机制,这里只是讲了如何在两个应用之间进行简单的通信,至于名词解释网上太多博客都有。第一次写博客,比较low,忘见谅哈!本文参考了
http://blog.csdn.net/jiuqiyuliang/article/details/48758203
工程采用maven构建,结构如下
activemq初步实践-生产者
 

其中lion-war为消息发送端,lion-consumer为消息接受端

第一步:官网下载activemqhttp://activemq.apache.org/

第二步:spring整合activemq

一:消息生产者lion-war配置

(1)添加核心依赖:

<!--消息机制-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>${activemq.version}</version>
</dependency>

(2)添加配置文件spring-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd">
    <!--connectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>
    <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!--生产者 start-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <!--队列模式-->
<property name="pubSubDomain" value="false"/>
    </bean>
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <!--主题模式-->
<property name="pubSubDomain" value="true"/>
    </bean>

</beans>

 (3)在web.xml初始化时加上
activemq初步实践-生产者
 (4)由于使用的springmvc,所以在此写个controller,JmsController.java


package com.lion.controller.mq;

import com.lion.service.mq.QueueProduceService;
import com.lion.service.mq.TopicProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by lucoo on 2016/10/18.
 */
@Controller
public class JmsController {
    @Autowired
private QueueProduceService queueProduceService;
    @Autowired
private TopicProduceService topicProduceService;

    /**
     * 队列测试
     * @return
*/
@RequestMapping(value = "/sendQueue")
    public String sendQueue() {
        queueProduceService.send("queueDestination", "hello world!i am from queue");
        return "/index";
    }

    /**
     * 发布订阅测试
     * @return
*/
@RequestMapping(value = "/sendTopic")
    public String publishTopic() {
        topicProduceService.publishTopic("topicDestination", "hello world!i am from topic");
        return "/index";
    }
}

 (5)service层写个两个测试service:QueueProduceService.java 和TopicProduceService.java

package com.lion.service.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * Created by lucoo on 2016/10/17.
 * 队列消息发送
 */
@Service
public class QueueProduceService {
    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    /**
     *
     * @param queueName(和监听器的保持一致)
     * @param message
*/
public void send(String queueName, String message) {
        jmsTemplate.send(queueName, new MessageCreator() {
            @Override
public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

package com.lion.service.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by lucoo on 2016/10/18.
 * 主题消息发送
 */
@Service
public class TopicProduceService {
    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate jmsTemplate;

    public void publishTopic(String topic, String message) {
        jmsTemplate.send(topic, new MessageCreator() {
            @Override
public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

到这里发送端的配置结束
消费者配置:http://lucoo.iteye.com/blog/2331440

相关推荐