Use message queue ActiveMQ

Keywords: Spring Apache Java xml

1. Java way Apache ActiveMQ official instance send message
Download ActiveMQ source code directly from Apache website http://activemq.apache.org/download-archives.html
Download and unzip to get an example of java code

Then pour in the IDE
As follows:

Please read the readme.md file carefully, which roughly means to type the project into two jar packages, then start the service, then run the two jar packages at the same time, and then you can see the specific call information. maven is the only way to type jar packages without modifying the code.
Start up service:

2. Using Spring Message Template to Send Messages
Spirng provides good support for Apache ActiveMQ

Producer code:

package com.jms.service.impl;

import com.jms.service.ProducerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * send message
 */
@Service
public class ProducerServiceImpl implements ProducerService {

    @Resource
    private JmsTemplate jmsTemplate;

    public void sendMessage(Destination destination, String msg) {
        System.out.println("Queue oriented"+destination+"send message");
        jmsTemplate.convertAndSend(destination,msg);
    }

    public void sendMessage(String msg) {
        System.out.println("Queue oriented"+jmsTemplate.getDefaultDestination().toString()+"send message");
        jmsTemplate.convertAndSend(msg);
    }
}

Consumer code:

package com.jms.service.impl;

import com.jms.service.CustomerService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private JmsTemplate jmsTemplate;
    /**
     * receive messages
     * @param destination
     */
    public void receive(Destination destination) {

        TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
        try {
            System.out.println("From the queue."+destination.toString()+"Successful Access to Information"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

Spring configuration code

<?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"
       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
        ">

    <!-- Start package scanning to register with@Controller,@Service,@repository,@Component Classes with equal annotations become spring Of bean -->
    <context:component-scan base-package="com.jms.service"> </context:component-scan>

    <!-- Configure the root view -->
    <!--<mvc:view-controller path="/" view-name="index"/>-->

    <!--Enable annotations-->
    <mvc:annotation-driven />

    <!-- View Layer Configuration -->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
        <!--<property name="prefix" value="/WEB-INF/view/"/>-->
        <!--<property name="suffix" value=".jsp"/>-->
    <!--</bean>-->


    <!-- To configure JMS Connection factory -->
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <!-- Define message queues( Queue) -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- Set the name of the message queue -->
        <constructor-arg>
            <value>queue1</value>
        </constructor-arg>
    </bean>

    <!-- To configure JMS Template ( Queue),Spring Provided JMS Tool class, which sends and receives messages. -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="queueDestination" />
        <property name="receiveTimeout" value="10000" />
    </bean>
</beans>

Test code

package com.jsm.test;

import com.jms.service.CustomerService;
import com.jms.service.ProducerService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jms.Destination;

/**
 * Message Queue Test Class
 */
public class JmsTest {


    @Test
    public void producerTest(){

        ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:spring-core.xml"});
        ProducerService producerService = (ProducerService)springContext.getBean("producerServiceImpl");
        CustomerService customerService = (CustomerService)springContext.getBean("customerServiceImpl");

        Destination destination = (Destination)springContext.getBean("queueDestination");
        producerService.sendMessage("Test message queue");
        customerService.receive(destination);
    }
}

test result

Reference blog

https://blog.csdn.net/fenfenguai/article/details/79257928
https://github.com/wahnn/SpringJMS
https://gitee.com/wahnn/SpringJMS

Posted by mdj on Sun, 07 Apr 2019 11:24:30 -0700