This commit is contained in:
张良(004796)
2024-01-12 17:50:50 +08:00
parent 728192d4f0
commit 28623f17d1
28 changed files with 746 additions and 110 deletions

View File

@@ -1,5 +1,7 @@
package com.ruoyi.cai.mq;
import com.alibaba.fastjson.JSON;
import com.ruoyi.cai.mq.consumer.AmqpConsumer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -22,4 +24,17 @@ public class AmqpProducer {
return messagePostProcessor;
});
}
public void sendCommonDelayMq(Integer type,Long roomId,Integer timeout){
CommonDelayDto dto = new CommonDelayDto();
dto.setType(type);
dto.setRoomId(roomId);
rabbitTemplate.convertAndSend(CheckTimeOutMqConfig.EXCHANGE_NAME,
CheckTimeOutMqConfig.ROUTING_KEY,
JSON.toJSONString(dto),
messagePostProcessor -> {
messagePostProcessor.getMessageProperties().setDelay(timeout*1000); // 设置延迟时间,单位毫秒
return messagePostProcessor;
});
}
}

View File

@@ -0,0 +1,9 @@
package com.ruoyi.cai.mq;
import lombok.Data;
@Data
public class CommonDelayDto {
private Integer type;
private Long roomId;
}

View File

@@ -0,0 +1,40 @@
package com.ruoyi.cai.mq;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
@Configuration
public class CommonDelayMqConfig {
public static final String EXCHANGE_NAME = "commonDelayExchange";
public static final String QUEUE_NAME = "commonDelayQueue";
public static final String ROUTING_KEY = "commonDelayRouting";
@Bean
public CustomExchange delayedExchange() {
HashMap<String,Object> args = new HashMap<>();
args.put("x-delayed-type", "direct");
return new CustomExchange(EXCHANGE_NAME,
"x-delayed-message", // 消息类型
true, // 是否持久化
false,// 是否自动删除
args);
}
@Bean
public Queue delayedQueue() {
return QueueBuilder.durable(QUEUE_NAME)
.withArgument("x-delayed-type", "direct")
.build();
}
@Bean
public Binding delayedBinding(Queue delayedQueue,CustomExchange delayedExchange) {
return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(ROUTING_KEY).noargs();
}
}

View File

@@ -1,10 +1,13 @@
package com.ruoyi.cai.mq;
package com.ruoyi.cai.mq.consumer;
import com.ruoyi.cai.service.ConsumeLogService;
import com.ruoyi.cai.ws.service.SettleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@@ -25,19 +28,18 @@ public class AmqpConsumer {
System.out.println(msg);
}*/
@Autowired
private ConsumeLogService consumeLogService;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = CALCULATE_SALES_QUEUE, durable = "false", autoDelete = "false"),
exchange = @Exchange(value = CALCULATE_SALES_EXCHANGE),
key = CALCULATE_SALES_KEY)
,containerFactory = "customContainerFactory")
public void calculateSalesQueue(String message) {
log.info("calculateSalesQueue: " + message);
}
@RabbitListener(queues = CheckTimeOutMqConfig.QUEUE_NAME
,containerFactory = "customContainerFactory")
public void checkTimeOutMq(String message) {
log.info("checkTimeOutMq: " + message);
log.info("接受到到分销处理请求: message=" + message);
consumeLogService.dealFenxiao(Long.valueOf(message));
log.info("分销处理完成: message=" + message);
}

View File

@@ -0,0 +1,30 @@
package com.ruoyi.cai.mq.consumer;
import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.mq.CheckTimeOutMqConfig;
import com.ruoyi.cai.ws.service.SettleService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.index.qual.SameLen;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class CheckTimeOutMqConsumer {
@Autowired
private SettleService settleService;
@Autowired
private AmqpProducer amqpProducer;
@RabbitListener(queues = CheckTimeOutMqConfig.QUEUE_NAME
,containerFactory = "customContainerFactory")
public void checkTimeOutMq(String message) {
log.info("checkTimeOutMq: " + message);
boolean next = settleService.withholdingFee(Long.valueOf(message));
if(next){
// 1分钟后继续执行
amqpProducer.sendCheckTimeOut(message,60);
}
}
}

View File

@@ -0,0 +1,34 @@
package com.ruoyi.cai.mq.consumer;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.mq.CommonDelayDto;
import com.ruoyi.cai.mq.CommonDelayMqConfig;
import com.ruoyi.cai.ws.job.CheckTimeOutJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class CommonDelayMqConsumer {
@Autowired
private CheckTimeOutJob checkTimeOutJob;
@Autowired
private AmqpProducer amqpProducer;
@RabbitListener(queues = CommonDelayMqConfig.QUEUE_NAME
,containerFactory = "customContainerFactory")
public void checkTimeOutMq(String message) {
log.info("CommonDelayMqConsumer: " + message);
CommonDelayDto dto = JSON.parseObject(message, CommonDelayDto.class);
switch (dto.getType()){
case 1:
checkTimeOutJob.deal(dto.getRoomId());
break;
default:
break;
}
}
}