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: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;
}

      ok!

转载于:https://my.oschina.net/mdxlcj/blog/3096142

你可能感兴趣的:(RabbitMQ之SSM框架整合xml配置)