千家信息网

RabbitMQ中如何进行SSM框架整合xml配置

发表于:2024-11-15 作者:千家信息网编辑
千家信息网最后更新 2024年11月15日,这期内容当中小编将会给大家带来有关RabbitMQ中如何进行SSM框架整合xml配置,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。前提:jdk1.8,本博客使用的是
千家信息网最后更新 2024年11月15日RabbitMQ中如何进行SSM框架整合xml配置

这期内容当中小编将会给大家带来有关RabbitMQ中如何进行SSM框架整合xml配置,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

前提:jdk1.8,本博客使用的是RabbitTemplate模版,用封装好的方法,不再使用

还有一个重点,自己一定要会使用rabbitmq服务器,自己创建exchange、queue等,不然使用该博客的话,会报错的。

两种方法:topic模式以及延迟队列的使用

1、pom
    cn.hutool    hutool-all    4.5.16    org.springframework.amqp    spring-rabbit    1.7.11.RELEASE
2、application.properties
# rabbitmq 消息配置rabbitmq.addresses=localhost:5672rabbitmq.virtual-host=/rabbitmq.username=guestrabbitmq.password=guestrabbitmq.channel-cache-size=50rabbitmq.concurrentConsumers=3rabbitmq.maxConcurrentConsumers=10# 确认方式 MANUAL 手动,AUTO 自动,NONE 自动确认rabbitmq.acknowledgeMode=MANUAL# 线程池数量 = 并发数 * 监听数rabbitmq.task-executor.pool-size=100
3、spring-rabbit.xml
                                                                                                                                                                                                                                                                                                                                                                                                    
4、监听器
import cn.hutool.core.date.DateUtil;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.amqp.core.Message;import org.springframework.amqp.core.MessageListener;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import java.io.IOException;import java.text.DateFormat;import java.util.Date;/** * @Author:MuJiuTian * @Description: RabbitMq延迟队列 https://blog.csdn.net/m912595719/article/details/83787486 * ChannelAwareMessageListener(Message memssage,Channel channel) MessageListener(Message message) * @Date: Created in 下午4:17 2019/8/12 */public class DelayListener implements MessageListener {    @Autowired    RabbitTemplate rabbitTemplate;    private static final ObjectMapper MAPPER = new ObjectMapper();    @Override    public void onMessage(Message message) {        try {            JsonNode jsonData = MAPPER.readTree(message.getBody());            System.out.println("延迟队列时间为:"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance()));        } catch (IOException e) {            e.printStackTrace();        }    }}
import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.platform.service.SeckillService;import org.springframework.amqp.core.Message;import org.springframework.amqp.core.MessageListener;import org.springframework.beans.factory.annotation.Autowired;/** * @Author:MuJiuTian * @Description: 秒杀消费者消费消息,监听执行业务逻辑处理 * @Date: Created in 下午5:01 2019/8/14 */public class SeckillHandler implements MessageListener {    @Autowired    SeckillService seckillService;    private static final ObjectMapper MAPPER = new ObjectMapper();    @Override    public void onMessage(Message message) {        try {            //队列中继续执行秒杀            JsonNode jsonData = MAPPER.readTree(message.getBody());            String goodsId = jsonData.get("goodsId").asText();            int productId = jsonData.get("productId").asInt();            int userId    = jsonData.get("userId").asInt();            int sellerNum = jsonData.get("sellerNum").asInt();            //开始秒杀            seckillService.seckillRedis(goodsId,productId,sellerNum,userId);        } catch (Exception e) {            e.printStackTrace();        }    }}
5、实体类
public class Mail implements Serializable {    private static final long serialVersionUID = -8140693840257585779L;    private String mailId;    private String country;    private Double weight;    public Mail() {    }    public Mail(String mailId, String country, double weight) {        this.mailId = mailId;        this.country = country;        this.weight = weight;    }    public String getMailId() {        return mailId;    }    public void setMailId(String mailId) {        this.mailId = mailId;    }    public String getCountry() {        return country;    }    public void setCountry(String country) {        this.country = country;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }    @Override    public String toString() {        return "Mail [mailId=" + mailId + ", country=" + country + ", weight="                + weight + "]";    }}
6、controller
/** * topic:通配符模式 */@GetMapping(value = "/test7")public void test11(){    Mail mail = new Mail("21","China",27.2);    System.out.println("topic模式发送数据到消息队列"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance()));    rabbitTemplate.convertAndSend("IExchange","lazy.dtb",mail);}/** * 死信队列 long等待时间,目前测试为:自动消费 */@GetMapping(value = "/test8")public void test13(long time) throws IOException {    Mail mail = randomMail();    System.out.println("延迟队列:dlx方式"+DateUtil.format(new Date(), DateFormat.getDateTimeInstance()));    rabbitTemplate.convertAndSend("dlx_delay_exchange","dlx_delay_road", mail, message -> {        message.getMessageProperties().setExpiration(time + "");        return message;    });}/** * 随机创建一个Mail实体对象,供接口测试 */public static Mail randomMail() {    Mail mail = new Mail();    mail.setMailId(new Random().nextInt(100)+"");    mail.setCountry("China");    mail.setWeight(new Random().nextDouble());    return mail;}

上述就是小编为大家分享的RabbitMQ中如何进行SSM框架整合xml配置了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

0