springboot 结合使用 activemq

22 篇文章 0 订阅

1、activemq

版本:apache-activemq-5.14.5

具体的下载到官网下载即可

2、springboot

搭建和使用自行上网查阅

3、工程依赖

pom的依赖

<!-- activeMq -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency>

配置文件

#activeMq
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=false
#true表示使用连接池
spring.activemq.pool.enabled=false

模拟的定义消息主题

package com.zh.service.activemq;

/**
 * @author zhangH
 * @date 2018/10/26
 */
public class MQQueueType {
    public static final String MQ_DEFAULT = "MQ_DEFAULT";
    // 学习
    public static final String MQ_STUDY = "MQ_STUDY";
    public static final String MQ_GOODS = "MQ_GOODS";
    // 订单
    public static final String ORDER_STATUS = "ORDER_STATUS";
    public static final String ORDER_PLACE = "ORDER_PLACE";
    public static final String ORDER_RETURN = "ORDER_RETURN";
    // 用户
    public static final String USER_INFO = "USER_INFO";
    public static final String USER_OPERATION = "USER_OPERATION";


}

 

消息的生产者

package com.zh.service.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangH
 * @date 2018/10/26
 */
@Service
public class MessageProducerServiceImpl {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 向默认队列发送消息
     *
     * @param msg
     */
    public void sendMsgD(String msg) {
        jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(MQQueueType.MQ_DEFAULT), msg);
    }

    /**
     * 向指定队列发送消息
     *
     * @param queueName 指定队列名称
     * @param msg       消息内容
     */
    public void sendMsgO(String queueName, String msg) {
        jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queueName), msg);
    }

}

 

消息的消费者

 

package com.zh.service.activemq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * @author zhangH
 * @date 2018/10/26
 */
@Service
public class MessageConsumerService {
    private static final Logger log = LoggerFactory.getLogger("active");

    @JmsListener(destination = MQQueueType.MQ_DEFAULT)
    public void defaultMq(String text) {
        log.info("【*** " + MQQueueType.MQ_DEFAULT + " 接收消息 ***】" + text);
    }

    @JmsListener(destination = MQQueueType.MQ_STUDY)
    public void receiveMessage(String text) {
        log.info("【*** " + MQQueueType.MQ_STUDY + " 接收消息 ***】" + text);
    }

    @JmsListener(destination = MQQueueType.MQ_GOODS)
    public void receiveGoodsMessage(String text) {
        log.info("【*** " + MQQueueType.MQ_GOODS + " 接收消息 ***】" + text);
    }

}

 

springboot测试类

package com.zh.activemq;

import com.zh.SpringBootDemoApplication;
import com.zh.service.activemq.MQQueueType;
import com.zh.service.activemq.MessageProducerServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;

/**
 * @author zhangH
 * @date 2018/10/26
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootDemoApplication.class)
@WebAppConfiguration
public class TestActiveMQ {

    @Resource
    private MessageProducerServiceImpl messageProducer;

    @Test
    public void testSend() throws Exception {
        for (int x = 0; x < 10; x++) {
            this.messageProducer.sendMsgD("study-" + x);
            this.messageProducer.sendMsgO(MQQueueType.MQ_GOODS, MQQueueType.MQ_GOODS + x);
            this.messageProducer.sendMsgO(MQQueueType.MQ_STUDY, MQQueueType.MQ_STUDY + x);
        }
        LoggerFactory.getLogger("active").info("activemq 消息发送完成");
    }
}

 

总结:

单机版的activemq的高可用性比较低,集群服务的高可用性较高。

由于是学习,没有看到实际的项目使用。

 

一天一点积累,最终就是想要的。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值