发送消息
-
以Message对象发送消息
import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; @Service public class MyRabbitMQService { final RabbitTemplate rabbitTemplate; public MyRabbitMQService(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; } public void sendDirectQueue(String sayMessage) { byte[] bytes = sayMessage.getBytes(); MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentEncoding("UTF-8"); messageProperties.setHeader("Message","这是一个测试消息"); Message message = new Message(bytes, messageProperties); rabbitTemplate.send("exchange.direct","exchange.direct.queue",message); } }
测试:
@Autowired MyRabbitMQService myRabbitMQService; @Test void rabbitService() { myRabbitMQService.sendDirectQueue("Say Hello Direct queue"); }
结果:
-
以自定义对象发送消息
service
public void sendFanout(Object object)</