赞
踩
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.2.8.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!--Jackson 核心库--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.9</version> </dependency> <!--Jackson 序列化库--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.9</version> </dependency> <!--Jackson 注解支持--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.9</version> </dependency>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <bean id="ProducerController" class="com.hy.hy114.util.rabbitmq.ProducerController"/> <!-- 配置连接工厂 --> <rabbit:connection-factory id="connectionFactory" host="192.168.103.10" port="5672" username="guest" password="guest" /> <!-- 定义mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 声明队列 --> <rabbit:queue name="que_producerOne" auto-declare="true" durable="true" /> <!-- 定义交换机绑定队列(路由模式) --> <rabbit:direct-exchange name="IExchange" id="IExchange"> <rabbit:bindings> <rabbit:binding queue="que_producerOne" key="que_producerOne_key" /> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息对象json转换类 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- 定义模版 --> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" exchange="IExchange" message-converter="jsonMessageConverter" /> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> <!-- 配置连接工厂 --> <rabbit:connection-factory id="connectionFactory" host="192.168.103.10" port="5672" username="guest" password="guest" /> <!-- 定义mq管理 --> <rabbit:admin connection-factory="connectionFactory" /> <!-- 声明队列 --> <rabbit:queue name="que_producerOne" auto-declare="true" durable="true" /> <!-- 定义消费者 --> <bean name="ConsumerOne" class="com.hy.hy114.util.rabbitmq.ConsumerOne" /> <!-- 定义消费者监听队列 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="ConsumerOne" queues="que_producerOne" /> </rabbit:listener-container> </beans>
在applicationContext.xml配置文件中引入以上两个xml文件即可。
public class ConsumerOne implements MessageListener { private static final ObjectMapper MAPPER = new ObjectMapper(); public void onMessage(Message msg) { System.out.println("===============start==============="); JsonNode jsonNode = MAPPER.readTree(msg.getBody()); String date = jsonNode.get("date").asText(); String date = jsonNode.get("data").asText(); System.out.println(msg); System.out.println("===============已消费==============="); } }
注入RabbitTemplate来操作RabbitMQ
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class RabbitTest { @Autowired RabbitTemplate rabbitTemplate; @Test public void test1(){ HashMap<String, String> map = new HashMap<String, String>(); LocalDateTime now = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); map.put("date",dateTimeFormatter.format(now) ); map.put("data", "test"); //根据key发送到对应的队列 rabbitTemplate.convertAndSend("que_producerOne_key", map); System.out.println("已生产"); } }
搞定!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。