1. 配置
@Configuration
public static class YwtDataQueryConfig {
public static final String QUEUE_YWT_DATA_QUERY = "QUEUE_YWT_DATA_QUERY";
public static final String DIRECT_EXCHANGE_YWT_DATA_QUERY = "DIRECT_EXCHANGE_YWT_DATA_QUERY";
public static final String ROUTING_KEY_YWT_DATA_QUERY = "ROUTING_KEY_YWT_DATA_QUERY";
@Bean
public Queue queueYwtDataQuery() {
return new Queue(QUEUE_YWT_DATA_QUERY, true);
}
@Bean
public DirectExchange directExchangeYwtDataQuery() {
return new DirectExchange(DIRECT_EXCHANGE_YWT_DATA_QUERY, true, false, null);
}
@Bean
public Binding bindingYwtDataQuery() {
return BindingBuilder.bind(queueYwtDataQuery()).to(directExchangeYwtDataQuery()).with(ROUTING_KEY_YWT_DATA_QUERY);
}
}
2. 监听消费
package com.xxx.service.mq;
import com.alibaba.fastjson.JSON;
import com.xxx.config.RabbitMqConfig;
import com.xxx.service.mq.vo.YwtQueryParam;
import com.xxx.service.view.YctGongaEBaseinfoService;
import com.xxx.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Slf4j
@Service
public class YwtDataQueryListener {
@Resource
private YctGongaEBaseinfoService enterService;
@RabbitListener(queues = RabbitMqConfig.YwtDataQueryConfig.QUEUE_YWT_DATA_QUERY)
@SendTo
public String onMessage(Message message) {
try {
byte[] body = message.getBody();
String param = new String(body, StandardCharsets.UTF_8);
YwtQueryParam queryParam = JSON.parseObject(param, YwtQueryParam.class);
String queryType = queryParam.getQueryType();
if ((YwtQueryParam.QueryType.FEEDBACK_RATIO.equals(queryType))) {
ResultVO resultVO = enterService.feedbackRatio(queryParam);
return JSON.toJSONString(resultVO);
}
if ((YwtQueryParam.QueryType.NOT_FEEDBACK_ENTERPRISE.equals(queryType))) {
ResultVO resultVO = enterService.notFeedbackEnterprise(queryParam);
return JSON.toJSONString(resultVO);
}
} catch (Exception exception) {
log.error(exception.getMessage());
}
return null;
}
}
3. 消息生产发送
public static final long YWT_REPLY_TIMEOUT = 15000L;
public static final String APPLICATION_JSON = "application/json";
public String sendToQueueYwtDataQuery(YwtQueryParam param) {
try {
String msg = JSON.toJSONString(param);
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(APPLICATION_JSON);
messageProperties.setExpiration(String.valueOf(YWT_REPLY_TIMEOUT));
rabbitTemplate.setReplyTimeout(YWT_REPLY_TIMEOUT);
Message message = new Message(msg.getBytes(StandardCharsets.UTF_8), messageProperties);
Message mm = rabbitTemplate.sendAndReceive(RabbitConfig.YwtDataQueryConfig.QUEUE_YWT_DATA_QUERY, message);
if (null == mm) {
return null;
}
String msgResult = new String(mm.getBody(), StandardCharsets.UTF_8);
return msgResult;
} catch (Exception e) {
return e.getMessage();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix