记录spring rabbitmq简单的使用遇到的问题
写了个最简单的例子,用默认的交换机。
准备:pom.xm的配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.neo</groupId> <artifactId>spring-boot-rabbitmq</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>spring-boot-rabbitmq</name> <description>Demo project for Spring Boot and rabbitmq</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1.配置类
package com.neo.rabbit; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public ConnectionFactory getConn(){ CachingConnectionFactory factory = new CachingConnectionFactory("127.0.0.1"); factory.setUsername("guest"); factory.setPassword("guest"); return factory; } @Bean public Queue helloQueue() { return new Queue("hello"); } }
2.消息发送者:注意下划线的那栏
package com.neo.rabbit.hello; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; @Component public class HelloSender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hello " + new Date(); System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("msg", context); } }
3.消息接收者
package com.neo.rabbit.hello; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import java.util.Date; @Component @RabbitListener(queues = "hello") public class HelloReceiver { @RabbitHandler public void process(String msg) { System.out.println("Receiver : " + msg); } }
4.写个控制器测试
package com.neo.controller; import com.neo.rabbit.hello.HelloSender; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RabbitController { @Autowired private HelloSender helloSender; @RequestMapping("/hello") public String hello(String msg){ helloSender.send(); return "success"; } }
运行:localhost:8080/hello
结果发送消息可以,但是收不到消息,代码研究了半天,后面发现是消息发送者HelloSender
里面的下划线部分问题。
发送消息的时候,需要对应的已注入队列:queue名称。
代码修改为:
this.rabbitTemplate.convertAndSend("hello", context);
即可。