mq 配置

This commit is contained in:
GYJ
2025-02-14 13:36:59 +08:00
parent c1b4f06670
commit 4c6d521118
11 changed files with 219 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.czg</groupId>
<artifactId>cash-common</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>cash-common-mq</artifactId>
<name>cash-common-mq</name>
<url>https://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.czg</groupId>
<artifactId>cash-common-tools</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,51 @@
package com.czg.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.HashMap;
import java.util.Map;
/**
* @author GYJoker
*/
@Configuration
public class RabbitConfig {
@Value("${spring.profiles.active}")
private String activeProfile;
@Bean
public Queue orderPrintQueue() {
// 创建一个用于存储队列参数的 Map
Map<String, Object> args = new HashMap<>();
// 设置消息过期时间为 180000 毫秒(即 180 秒)
args.put("x-message-ttl", 180000);
return new Queue(activeProfile + "-" + RabbitConstants.Queue.ORDER_PRINT_QUEUE, true, false, false, args);
}
@Bean
public Queue orderStockQueue() {
return new Queue(activeProfile + "-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE, true);
}
@Bean
@Primary
public DirectExchange directExchange() {
return new DirectExchange(activeProfile + "-" + RabbitConstants.Exchange.CASH_EXCHANGE);
}
@Bean
public Binding bindingOrderPrintExchange(Queue orderPrintQueue, DirectExchange exchange) {
return BindingBuilder.bind(orderPrintQueue).to(exchange).with(activeProfile + "-" + RabbitConstants.Queue.ORDER_PRINT_QUEUE);
}
@Bean
public Binding bindingOrderStockExchange(Queue orderStockQueue, DirectExchange exchange) {
return BindingBuilder.bind(orderStockQueue).to(exchange).with(activeProfile + "-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE);
}
}

View File

@@ -0,0 +1,16 @@
package com.czg.config;
/**
* @author GYJoker
*/
public interface RabbitConstants {
class Exchange {
public static final String CASH_EXCHANGE = "czg-cashier";
}
class Queue {
public static final String ORDER_STOCK_QUEUE = "order.stock.queue";
public static final String ORDER_PRINT_QUEUE = "order.print.queue";
}
}

View File

@@ -0,0 +1,32 @@
package com.czg.config;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author GYJoker
*/
@Service
public class RabbitPublisher {
@Value("${spring.profiles.active}")
private String activeProfile;
@Resource
private RabbitTemplate rabbitTemplate;
public void sendOrderStockMsg(String msg) {
sendMsg(RabbitConstants.Exchange.CASH_EXCHANGE, RabbitConstants.Queue.ORDER_STOCK_QUEUE, msg);
}
public void sendOrderPrintMsg(String msg) {
sendMsg(RabbitConstants.Exchange.CASH_EXCHANGE, RabbitConstants.Queue.ORDER_PRINT_QUEUE, msg);
}
private void sendMsg(String exchange, String queue, String msg) {
rabbitTemplate.convertAndSend(activeProfile + "-" + exchange, activeProfile + "-" + queue, msg);
}
}