@Configuration
public class RabbitMQConfig{
/** gis图形交换机 */
public static final String GIS_GRAPHICS_EXCHANGE = “3d_gis_exchange”;
/** gis图形数据接收序列 */
public static final String GIS_DATA_RECEIVE_REQUEUE = “gis_data_queue”;
/** gis图形数据返回(发送)序列 */
public static final String GIS_DATA_SEND_QUEUE = “gis_result_queue”;
@Bean
public Queue gisDataReceiveQueue () {
return new Queue(GIS_DATA_RECEIVE_REQUEUE);
}
@Bean
public Queue gisDataSendQueue () {
return new Queue(GIS_DATA_SEND_QUEUE);
}
需要文中资料的朋友,可以加我\/信获取:vip1024b 备注Java
@Bean
public DirectExchange directExchange() {
return new DirectExchange(GIS_GRAPHICS_EXCHANGE);
}
@Bean
public Binding receiveBinding () {
return BindingBuilder.bind(gisDataReceiveQueue()).to(directExchange()).withQueueName();
}
@Bean
public Binding sendBinding () {
return BindingBuilder.bind(gisDataSendQueue()).to(directExchange()).withQueueName();
}
}
消息生产者
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springblade.gis.rabbitmq.confirm.RabbitReturnCallback;
import org.springblade.gis.rabbitmq.confirm.RabbitConfirmCallback;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class RabbitSender implements InitializingBean {
@Resource
private RabbitTemplate rabbitTemplate;
/**
-
异步 对外发送消息的方法
-
@param msgId &msg 具体的消息内容
-
@throws Exception
*/
public void syncSend(String msgId,String msg) {
MessageProperties messageProperties=new MessageProperties();
messageProperties.setMessageId(msgId);
Message message=new Message(msg.getBytes(StandardCharsets.UTF_8),messageProperties);
CorrelationData data = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(RabbitMQConfig.GIS_GRAPHICS_EXCHANGE, RabbitMQConfig.GIS_DATA_SEND_QUEUE, message,data);
}
/**
-
同步 对外发送消息的方法
-
@param msgId &msg 具体的消息内容
-
@throws Exception
*/
public Object asyncSend(String msgId,String msg) {
MessageProperties messageProperties=new MessageProperties();
messageProperties.setMessageId(msgId);
Message message=new Message(msg.getBytes(StandardCharsets.UTF_8),messageProperties);
CorrelationData data = new CorrelationData(UUID.randomUUID().toString());
log.info(“【发送的消息-社会信用代码】:” + msg);
Object mm = rabbitTemplate.convertSendAndReceive(RabbitMQConfig.GIS_GRAPHICS_EXCHANGE,RabbitMQConfig.GIS_DATA_SEND_QUEUE, message,data);
log.info(“【同步消息返回结果-msgResult】:”+mm.toString());
return mm;
}
@Override
public void afterPropertiesSet () {
rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
rabbitTemplate.setReturnCallback(new RabbitReturnCallback());
rabbitTemplate.setMandatory(true);
}
}
消息消费者
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.core.Message;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
@AllArgsConstructor
public class RabbitReceiver {
/**
-
组合使用监听
-
@param message
-
@param channel
*/
@RabbitListener(queues =RabbitMQConfig.GIS_DATA_RECEIVE_QUEUE)
@SendTo(RabbitMQConfig.GIS_DATA_RECEIVE_QUEUE)
public String receiveQueueMsg(Message message) {
// 1. 收到消息以后进行业务端消费处理
log.info(“消息id:” + message.getMessageProperties().getMessageId() );
String messageId= message.getMessageProperties().getMessageId();
Long userId=0l;
JSONObject json;
try {
userId= Func.toLong(messageId);
String body=new String(message.getBody());
log.info(“消费消息:” + body );
json=JSONObject.parseObject(body);
} catch (Exception e) {
return “error”;
}
return “6666”;
}
线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识
一线互联网P7面试集锦+各种大厂面试集锦
学习笔记以及面试真题解析
arseObject(body);
} catch (Exception e) {
return “error”;
}
return “6666”;
}
线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识
[外链图片转存中…(img-FgmVBf9z-1716305590251)]
一线互联网P7面试集锦+各种大厂面试集锦
[外链图片转存中…(img-xVTXhTpg-1716305590252)]
学习笔记以及面试真题解析
[外链图片转存中…(img-Zv8cOImG-1716305590252)]