队列配置
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@Component
public class xxxxxRabbitMqConfig {
@Value("${xxxxxxxxxx}")
private String 交换机名;
@Value("${xxxxxxxxxx}")
private String 队列名;
@Value("${xxxxxxxx}")
private String Routingkey;
@Bean("exchange")(可以自定义名字)
public DirectExchange exchange() {
return new DirectExchange(channelOrgChangeExchange);
}
@Bean("queue") (可以自定义名字)
public Queue queue() {
return new Queue(队列名字, true);
}
@Bean(绑定需要和前面定义名字关联)
public Binding queuebBindingX(@Qualifier("queue") Queue queue,
@Qualifier("exchange") DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingKey);
}
}
消息发送
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
--------------------------------------------------------------
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private RedisTemplate redisTemplate;
---------------------------------------------------------------
String sendMsg = JSON.toJSONString("发送消息实体");
CorrelationData correlationData = new CorrelationData();
rabbitTemplate.setConfirmCallback(confirmCallback);
archiveMsg(correlationData.getId(), sendMsg);
rabbitTemplate.convertAndSend(交换机名字, routingkey, sendMsg, correlationData);
log.info("mq发送完成:消息[{}]", sendMsg);
private final RabbitTemplate.ConfirmCallback confirmCallback = (CorrelationData correlationData, boolean ack, String cause) -> {
String id = correlationData != null ? correlationData.getId() : "";
if (ack) {
if (!StringUtils.isEmpty(id)) {
redisTemplate.delete(id);
}
} else {
repeatMsg(id);
}
};
private void archiveMsg(String id, String msg) {
if (com.alibaba.druid.util.StringUtils.isEmpty(id)) {
log.error("rabbitmq收到未知的空消息!");
return;
}
log.info("消息存档,消息Id[{}]", id);
redisTemplate.opsForSet().add(id, msg);
}
private void repeatMsg(String id) {
if (com.alibaba.druid.util.StringUtils.isEmpty(id)) {
log.error("rabbitmq收到未知的空消息!");
return;
}
String msg = redisTemplate.opsForValue().get(id).toString();
CorrelationData correlationData = new CorrelationData();
correlationData.setId(id);
rabbitTemplate.setConfirmCallback(confirmCallback);
rabbitTemplate.convertAndSend(交换机, routingkey, msg);
}
消息接收
```java
@RabbitHandler
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "队列名字", durable = "true"), exchange =
@Exchange(value = "交换机名字"), key = "key名字"))
public void 方法名(@Payload String msg, @Headers Map<String, Object> headers, Channel channel)
throws Exception {
String msgId = String.valueOf(headers.get("id"));
long deliveryTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
log.info("接mq通知消息,msgId:{},消息:{},headers:{}", msgId, msg, headers);
try {
自定义接收实体 = JSON.parseObject(msg, 转换类型.class);
log.info("实体对象:{}", 自定义接收实体);
} catch (Exception e) {
channel.basicNack(deliveryTag, false, false);
return;
}
if (null != 接收实体) {
} catch (Exception e) {
log.info("处理异常");
}
}
}