1.使用枚举定义队列
import lombok.Getter;
@Getter
public enum MessageTaskSendEnum {
MESSAGE_TASK_SEND_ENUM("message.task.send.direct","message.task.send.cancel","message.task.send.cancel"),
MESSAGE_TASK_SEND_TTL_ENUM("message.task.send.ttl.direct","message.task.send.ttl.cancel","message.task.send.ttl.cancel"),
MESSAGE_TASK_SEND_DELAYED_ENUM("message.task.send.delayed.direct","message.task.send.delayed.cancel","message.task.send.delayed.cancel");
private final String exchange;
private final String name;
private final String routeKey;
MessageTaskSendEnum(String exchange, String name, String routeKey){
this.exchange = exchange;
this.name = name;
this.routeKey = routeKey;
}
}
2.绑定队列和交换机
import org.jeecg.modules.rabbitmq.enums.MessageTaskSendEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* @author wanglei
* @version 1.0
* @description: 消息推送
* @date 2021/11/29 11:34
*/
@Configuration
public class MessageTaskSendConfig {
@Bean
public CustomExchange delayedExchange(){
Map<String, Object> arguments = new HashMap<>();
arguments.put("x-delayed-type", "direct");
return new CustomExchange(MessageTaskSendEnum.MESSAGE_TASK_SEND_DELAYED_ENUM.getExchange(),"x-delayed-message",true,false,arguments);
}
@Bean
Queue delayedQueue() {
return new Queue(MessageTaskSendEnum.MESSAGE_TASK_SEND_DELAYED_ENUM.getName(), true);
}
@Bean
Binding delayedBinding(CustomExchange delayedExchange,Queue delayedQueue) {
return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(MessageTaskSendEnum.MESSAGE_TASK_SEND_DELAYED_ENUM.getRouteKey()).noargs();
}
}
3.创建生产者
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.rabbitmq.enums.MessageTaskSendEnum;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author wanglei
* @version 1.0
* @description: TODO
* @date 2021/7/8 13:09
*/
@Slf4j
@Component
public class MessageTaskDelayedSend {
@Resource
private AmqpTemplate amqpTemplate;
//延时队列
public void sendMessage(String json, Integer delayTimes){
amqpTemplate.convertAndSend(MessageTaskSendEnum.MESSAGE_TASK_SEND_DELAYED_ENUM.getExchange(), MessageTaskSendEnum.MESSAGE_TASK_SEND_DELAYED_ENUM.getRouteKey(), json, message -> {
message.getMessageProperties().setDelay(delayTimes);
return message;
});
log.info("推送模板消息:{}",json);
}
}
4.创建消费者
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.messagesend.entity.MessageTask;
import org.jeecg.modules.messagesend.service.IMessageTaskLogService;
import org.jeecg.modules.messagesend.service.IMessageTaskService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @author wanglei
* @version 1.0
* @description: 延时推送队列消费者
* @date 2021/3/26 10:50
*/
@Slf4j
@Component
@RabbitListener(queues = "message.task.send.delayed.cancel")
public class MessageTaskDelayedReceiver {
@Resource
private IMessageTaskService messageTaskService;
@Resource
private IMessageTaskLogService messageTaskLogService;
@RabbitHandler
public void handle(String json, Channel channel, Message message) {
log.info("延时推送队列消费者:"+json);
try {
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (
IOException e) {
e.printStackTrace();
}
}
}