java实现消息队列的两种方式
阅读原文时间:2021年04月20日阅读:1

实现消息队列的两种方式

Apache ActiveMQ官方实例发送消息

直接在Apache官网http://activemq.apache.org/download-archives.html下载ActiveMQ源码
下载解压后拿到java代码实例

然后倒入IDE
如下:

请认真阅读readme.md文件,大致意思就是把项目打成两个jar包,然后启动服务,然后同时运行打的两个jar包,然后就能看到具体的调用信息。打jar包时直接利用maven打就行了,不用修改代码。
启动服务:

利用Spring消息模板发送消息

Spirng对Apache ActiveMQ提供了很好的支持

生成者代码:

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;

/**
 * 发送消息
 */
@Service
public class ProducerServiceImpl implements ProducerService {

    @Resource
    private JmsTemplate jmsTemplate;

    public void sendMessage(Destination destination, String msg) {
        System.out.println("向队列"+destination+"发送消息");
        jmsTemplate.convertAndSend(destination,msg);
    }

    public void sendMessage(String msg) {
        System.out.println("向队列"+jmsTemplate.getDefaultDestination().toString()+"发送消息");
        jmsTemplate.convertAndSend(msg);
    }
}

消费者代码:

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;
    /**
     * 接收消息
     * @param destination
     */
    public void receive(Destination destination) {

        TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
        try {
            System.out.println("从队列》"+destination.toString()+"成功获取消息》"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

Spring配置代码

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

    <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
    <context:component-scan base-package="com.jms.service"> </context:component-scan>

    <!-- 配置根视图 -->
    <!--<mvc:view-controller path="/" view-name="index"/>-->

    <!--启用注解-->
    <mvc:annotation-driven />

    <!-- 视图层配置 -->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
        <!--<property name="prefix" value="/WEB-INF/view/"/>-->
        <!--<property name="suffix" value=".jsp"/>-->
    <!--</bean>-->


    <!-- 配置JMS连接工厂 -->
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <!-- 定义消息队列(Queue) -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>queue1</value>
        </constructor-arg>
    </bean>

    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <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>

测试代码

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;

/**
 * 消息队列测试类
 */
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("测试消息队列");
        customerService.receive(destination);
    }
}

测试结果


代码地址
https://github.com/wahnn/SpringJMS
https://gitee.com/wahnn/SpringJMS