##队列绑定
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;
@Configuration
public class RabbitMqConfig {
//延时
public static final String DELAYED_ROBOT_QUEUE_NAME = "delayed_robot_queue_name";
public static final String DELAYED_ROBOT_EXCHANGE_NAME = "delayed_robot_exchange_name";
public static final String DELAYED_ROBOT_ROUTING_KEY = "delayed_robot_routing_key";
//死信
public static final String DEAD_LETTER_ROBOT_QUEUE_NAME = "dead_letter_robot_queue_name";
public static final String DEAD_LETTER_ROBOT_EXCHANGE_NAME = "dead_letter_robot_exchange_name";
public static final String DEAD_LETTER_ROBOT_ROUTING_KEY = "dead_letter_robot_routing_key";
//延时队列绑定死信
@Bean
public Queue delayQueue() {
Map<String, Object> args = new HashMap<>();
// x-dead-letter-exchange 这里声明当前队列绑定的死信交换机
args.put("x-dead-letter-exchange", DEAD_LETTER_ROBOT_EXCHANGE_NAME);
// x-dead-letter-routing-key 这里声明当前队列的死信路由key
args.put("x-dead-letter-routing-key", DEAD_LETTER_ROBOT_ROUTING_KEY);
return QueueBuilder.durable(DELAYED_ROBOT_QUEUE_NAME).withArguments(args).build();
}
@Bean
public Exchange customExchange() {
return new DirectExchange(DELAYED_ROBOT_EXCHANGE_NAME);
}
@Bean
public Binding bindingDelay() {
return BindingBuilder.bind(delayQueue()).to(customExchange()).with(DELAYED_ROBOT_ROUTING_KEY).noargs();
}
//死信
@Bean
public Queue deadLetterQueue() {
return new Queue(DEAD_LETTER_ROBOT_QUEUE_NAME);
}
@Bean
public Exchange deadLetterExchange() {
return new DirectExchange(DEAD_LETTER_ROBOT_EXCHANGE_NAME);
}
@Bean
public Binding bindingDeadLetter() {
return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEAD_LETTER_ROBOT_ROUTING_KEY).noargs();
}
}
##发送消息
import com.alibaba.fastjson.JSONObject;
import com.fuyun.microservice.ResultObject;
import com.fuyun.microservice.config.RabbitMqConfig;
import com.fuyun.microservice.shop.model.req.RabbitMqTestMessageReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Slf4j
@RequestMapping("/test")
@RestController
public class RabbitMqTestController {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping("/testSendDelayMessage")
public ResultObject sendDelayMsg(@RequestBody RabbitMqTestMessageReq message) {
rabbitTemplate.convertAndSend(RabbitMqConfig.DELAYED_ROBOT_EXCHANGE_NAME, RabbitMqConfig.DELAYED_ROBOT_ROUTING_KEY,
JSONObject.toJSONString(message), a ->{
a.getMessageProperties().setExpiration(String.valueOf(message.getTime()));
return a;
});
log.info("消息发送成功:{}, message:{}", new Date().toString(), message);
return ResultObject.buildSuccess("消息发送成功");
}
}
消息监听
import com.alibaba.fastjson.JSON;
import com.fuyun.microservice.config.RabbitMqConfig;
import com.fuyun.microservice.shop.model.req.RabbitMqTestMessageReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Slf4j
public class Robotlisten {
@RabbitListener(queues = RabbitMqConfig.DEAD_LETTER_ROBOT_QUEUE_NAME)
public void receive(Message message) {
RabbitMqTestMessageReq messageReq = JSON.parseObject(new String(message.getBody()), RabbitMqTestMessageReq.class);
log.info("当前时间:{},延时队列收到消息:{}", new Date().toString(), messageReq.getBody());
}
}