资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

成都创新互联专注于企业成都营销网站建设、网站重做改版、越秀网站定制设计、自适应品牌网站建设、HTML5商城建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为越秀等各大城市提供网站开发制作服务。

      前提: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:5672
rabbitmq.virtual-host=/
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.channel-cache-size=50
rabbitmq.concurrentConsumers=3
rabbitmq.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配置了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


分享标题:RabbitMQ中如何进行SSM框架整合xml配置
分享网址:http://cdkjz.cn/article/gopojc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220