rabbitmq的配置类

dev.car.gateway
冷调 2024-10-06 21:41:25 +08:00
parent f5d6b5c00d
commit f38e42f72f
6 changed files with 101 additions and 9 deletions

View File

@ -2,12 +2,16 @@ package com.muyu.common.rabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import javax.annotation.Resource;
/**
* @author Lenovo
*/
@Configuration
public class RabbitListenerConfigurer implements org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer {

View File

@ -0,0 +1,47 @@
package com.muyu.common.rabbit.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author Lenovo
*/
@Component
@Log4j2
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* @PostContructspringspring
*/
@PostConstruct
public void init() {
rabbitTemplate.setConfirmCallback(this);
}
/**
*
*
* @param correlationData
* @param ack
* @param cause
*/
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if (!ack) {
String exchange = correlationData.getReturned().getExchange();
String message = correlationData.getReturned().getMessage().getBody().toString();
// 发送异常
log.error("消息:{},发送到交换机:{}失败,原因是:{}", message, exchange, cause);
}
}
}

View File

@ -0,0 +1,42 @@
package com.muyu.common.rabbit.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
*
* @author Lenovo
*/
@Component
@Log4j2
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* @PostContructspringspring
*/
@PostConstruct
public void init() {
rabbitTemplate.setReturnsCallback(this);
}
/**
*
*
* @param returnedMessage the returned message and metadata.
*/
@Override
public void returnedMessage(ReturnedMessage returnedMessage) {
log.error("消息:{},被交换机:{} 回退!退回原因为:{}",
returnedMessage.getMessage().toString(), returnedMessage.getExchange(), returnedMessage.getReplyText());
// TODO 回退了所有的信息,可做补偿机制
}
}

View File

@ -72,6 +72,10 @@
<groupId>com.muyu</groupId>
<artifactId>cloud-common-api-doc</artifactId>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-rabbit</artifactId>
</dependency>
<!-- http协议 -->
<dependency>
@ -84,11 +88,7 @@
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- mq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>

View File

@ -28,6 +28,5 @@ public class RedisConfig {
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}

View File

@ -85,7 +85,7 @@ public class Sample implements ApplicationRunner , DisposableBean {
// 设置密码
.setPassword("10160810@a")
// 设置要创建的实例数量
.setAmount(2)
.setAmount(1)
.setInternetChargeType("PayByTraffic")
.setInternetMaxBandwidthOut(1);
@ -219,7 +219,7 @@ public class Sample implements ApplicationRunner , DisposableBean {
public void destroy(){
try {
log.info("删除实例方法");
Thread.sleep(100000);
// Thread.sleep(100000);
deleteSample();
} catch (Exception e) {
log.info("删除实例失败");