当前位置:   article > 正文

RabbitMQ Spring(三)RabbitTemplate

public rabbittemplate rabbittemplate(

RabbitMQ 配置

  1. package com.bfxy.spring;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.Queue;
  5. import org.springframework.amqp.core.TopicExchange;
  6. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  7. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  8. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  9. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.ComponentScan;
  12. import org.springframework.context.annotation.Configuration;
  13. @Configuration
  14. @ComponentScan({"com.bfxy.spring.*"})
  15. public class RabbitMQConfig {
  16. public final static String HOST = "127.0.0.1";
  17. @Bean
  18. public ConnectionFactory connectionFactory(){
  19. CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  20. connectionFactory.setAddresses(HOST);
  21. connectionFactory.setUsername("guest");
  22. connectionFactory.setPassword("guest");
  23. connectionFactory.setVirtualHost("/");
  24. return connectionFactory;
  25. }
  26. @Bean
  27. public TopicExchange exchange001() {
  28. return new TopicExchange("topic001", true, false);
  29. }
  30. @Bean
  31. public Queue queue001() {
  32. return new Queue("queue001", true); //队列持久
  33. }
  34. @Bean
  35. public Binding binding001() {
  36. return BindingBuilder.bind(queue001()).to(exchange001()).with("spring.*");
  37. }
  38. @Bean
  39. public TopicExchange exchange002() {
  40. return new TopicExchange("topic002", true, false);
  41. }
  42. @Bean
  43. public Queue queue002() {
  44. return new Queue("queue002", true); //队列持久
  45. }
  46. @Bean
  47. public Binding binding002() {
  48. return BindingBuilder.bind(queue002()).to(exchange002()).with("rabbit.*");
  49. }
  50. @Bean
  51. public Queue queue003() {
  52. return new Queue("queue003", true); //队列持久
  53. }
  54. @Bean
  55. public Binding binding003() {
  56. return BindingBuilder.bind(queue003()).to(exchange001()).with("mq.*");
  57. }
  58. @Bean
  59. public Queue queue_image() {
  60. return new Queue("image_queue", true); //队列持久
  61. }
  62. @Bean
  63. public Queue queue_pdf() {
  64. return new Queue("pdf_queue", true); //队列持久
  65. }
  66. @Bean
  67. public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
  68. RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
  69. return rabbitTemplate;
  70. }
  71. }

RabbitTemplate 使用

  1. package com.bfxy.spring;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.amqp.AmqpException;
  5. import org.springframework.amqp.core.*;
  6. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. @RunWith(SpringRunner.class)
  11. @SpringBootTest
  12. public class ApplicationTests {
  13. @Autowired
  14. private RabbitTemplate rabbitTemplate;
  15. @Test
  16. public void testSendMessage() throws Exception {
  17. MessageProperties messageProperties = new MessageProperties();
  18. messageProperties.getHeaders().put("desc", "信息描述..");
  19. messageProperties.getHeaders().put("type", "自定义消息类型..");
  20. Message message = new Message("Hello RabbitMQ".getBytes(), messageProperties);
  21. rabbitTemplate.convertAndSend("topic001", "spring.amqp", message, new MessagePostProcessor() {
  22. @Override
  23. public Message postProcessMessage(Message message) throws AmqpException {
  24. System.err.println("------添加额外的设置---------");
  25. message.getMessageProperties().getHeaders().put("desc", "额外修改的信息描述");
  26. message.getMessageProperties().getHeaders().put("attr", "额外新加的属性");
  27. return message;
  28. }
  29. });
  30. }
  31. @Test
  32. public void testSendMessage2() throws Exception {
  33. MessageProperties messageProperties = new MessageProperties();
  34. messageProperties.setContentType("text/plain");
  35. Message message = new Message("mq 消息1234".getBytes(), messageProperties);
  36. rabbitTemplate.send("topic001", "spring.abc", message);
  37. rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object message send!");
  38. rabbitTemplate.convertAndSend("topic002", "rabbit.abc", "hello object message send!");
  39. }
  40. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/543517
推荐阅读
相关标签
  

闽ICP备14008679号