@Autowired RabbitTemplate rabbitTemplate; @Autowired AmqpAdmin amqpAdmin; @Test void contextLoads() { rabbitTemplate.convertAndSend("amq.direct", "amq.direct", "消息内容"); } @Test void contextLoads1() { rabbitTemplate.receiveAndConvert("amq.direct"); } @Test void creatExchange() { amqpAdmin.declareExchange(new DirectExchange("交换机名称")); System.out.println("创建完成"); amqpAdmin.declareQueue(new Queue("队列名称",true)); System.out.println("创建完成"); amqpAdmin.declareBinding(new Binding("目的地", Binding.DestinationType.QUEUE,"交换机名称","路由键",null)); }
监听队列:
@SpringBootApplication @EnableRabbit public class AmqpApplication { public static void main(String[] args) { SpringApplication.run(AmqpApplication.class, args); } }
@Service public class BookService { @RabbitListener(queues = "队列名称") void Selectbook(Book book) { System.out.println("book = " + book); } @RabbitListener(queues = "队列名称") void SelectMessage(Message message) { System.out.println("message.getBody() = " + message.getBody()); System.out.println("message.getMessageProperties() = " + message.getMessageProperties()); } }
序列化:
@Configuration public class amqpConfig { @Bean public MessageConverter messageConverter() { return new Jackson2JsonMessageConverter(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)