消息队列解析Message
消息队列生产者
@Autowired private final AmqpTemplate amqpTemplate; public void test() { Map<String, Object> testMap = Maps.newHashMap(); testMap.put("userId", 101); testMap.put("archiveId",202); amqpTemplate.convertSendAndReceive("testQueue",testMap); }
消息队列消费者
import cn.hutool.core.bean.BeanUtil; import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.messaging.Message; import org.springframework.stereotype.Component; @Component @Slf4j public class RabbitQueueListener { /** * 监听 testQueue 队列的处理器 * @param message */ @RabbitListener(queues = "testQueue") @RabbitHandler public void onMessage(Message message) { Map<String, Object> map = BeanUtil.beanToMap(message.getPayload()); log.info("消费端Payload: " + map); } }
使用BeanUtil.beanToMap()方法将message.getPayload()转成Map
结果:消费端Payload: {userId=101, archiveId=202}
hutool依赖
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-core --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>5.8.10</version> </dependency>