ActiveMQ的发送消息和监听消息

本文介绍了一种基于ActiveMQ的消息队列实现方案,包括工具类的创建、消息的发送与接收、监听器的设计,以及在Spring Boot项目中的集成配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.MQ工具类

package com.xxx.common.activemq;

import com.alibaba.fastjson.JSONObject;
import com.yxb.base.util.CommonUtil;
import com.yxb.base.util.DateUtil;
import com.yxb.base.util.IDGenerator;
import com.yxb.base.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static org.apache.commons.httpclient.util.DateUtil.formatDate;

@Repository
public class ActiveMQDao {
    /**  创建新的Execl下载任务  队列名字*/
    public static final String DownExcel_Task = "downExcel_task";
    
    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * 发送消息给指定队列
     * @param destinationName
     * @param message
     * @return
     */
    public Boolean sendJmsMsg(String destinationName,Object message){
        if(message == null){
            return false;
        }
        String msg = CommonUtil.beanToString(message);
        return this.sendJmsMsg(destinationName,msg);
    }
    
    /**
     * 发送指定目标mq
     * @param destinationName 目标名
     * @param message 消息内容
     * @return
     */
    public Boolean sendJmsMsg(String destinationName, String message) {
        try{
            jmsTemplate.convertAndSend(destinationName, message);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

2.发送MQ消息

activeMQDao.sendJmsMsg(ActiveMQDao.DownExcel_Task, downExeclId);

3.监听MQ消息

package com.xxx.posadmin.common.listner;

import com.yxb.base.util.DateUtil;
import com.pospay.common.base.constant.CommonConstant;
import com.pospay.common.base.entity.SysConfig;
import com.pospay.posadmin.common.config.AccessAbleInterceptor;
import com.pospay.posadmin.dao.DownExeclMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;

@Service
public class ActiveMQReceiver {

    private static Logger logger = LoggerFactory.getLogger(ActiveMQReceiver.class);

    @Autowired
    private DownExeclMapper downExeclMapper;

   /**
     * 监听导出Execl任务
     * @param downExeclId
     * @throws Exception
     */
    @JmsListener(destination = ActiveMQDao.DownExcel_Task)
    public void downExecl(Integer downExeclId) throws Exception {
       //根据mq中的id,查询导出任务信息
       DownExeclTask downExeclTask = downExeclMapper.getById(downExeclId);
       //导出execl的逻辑省略...
       
    }

}

4.MQ的pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.pospay</groupId>
		<artifactId>pos-common</artifactId>
		<version>1.0.0</version>
	</parent>
	<groupId>com.pospay</groupId>
	<artifactId>pos-activeMQ</artifactId>
	<name>pos-activemq</name>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
	</dependencies>

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值