Activemq preliminary practice - producer

Recently, I learned the simple integration of activemq and spring, and simulated the message sending and receiving mechanism by connecting a web project. Here is just how to carry out simple communication between the two applications. As for the nomenclature, there are too many blogs on the Internet. It's the first time to write a blog, it's relatively low, I forgot to forgive me! This article refers to
http://blog.csdn.net/jiuqiyuliang/article/details/48758203
The project is built with maven, and the structure is as follows

 

Among them, lion-war is the message sender, and lion-consumer is the message receiver.

Step 1: Download activemq from the official website http://activemq.apache.org/

Step 2: Spring integrates activemq

One: message producer lion-war configuration

(1) Add core dependencies:

 

<!--消息机制-->
<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) Add the configuration file 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) Add (4) when the web.xml is initialized,

 because of the springmvc used, write a controller here, 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) The service layer writes two test services: QueueProduceService.java and 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);
            }
        });
    }
}

The configuration of the sender ends here
Consumer configuration: http://lucoo.iteye.com/blog/2331440

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326976671&siteId=291194637