赞
踩
package com.java1234.producer.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { /** * direct交换机名称 */ public static final String DIRECT_EXCHANGE="directExchange"; /** * direct队列名称 */ public static final String DIRECT_QUEUE="directQueue"; /** * direct路由Key */ public static final String DIRECT_ROUTINGKEY="directRoutingKey"; /** * 定义一个direct交换机 * @return */ @Bean public DirectExchange directExchange(){ return new DirectExchange(DIRECT_EXCHANGE); } /** * 定义一个direct队列 * @return */ @Bean public Queue directQueue(){ return new Queue(DIRECT_QUEUE); } /** * 定义一个队列和交换机的绑定 * @return */ @Bean public Binding directBinding(){ return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTINGKEY); } }
package com.java1234.consumer.service.impl; import com.java1234.consumer.service.RabbitMqService; import com.java1234.producer.config.RabbitMQConfig; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("rabbitmqService") public class RabbitMqServiceImpl implements RabbitMqService { @Autowired private AmqpTemplate amqpTemplate; @Override public void receiveMessage() { String message=(String) amqpTemplate.receiveAndConvert(RabbitMQConfig.DIRECT_QUEUE); System.out.println("接受到的mq消息:"+message); } @Override @RabbitListener(queues = {RabbitMQConfig.DIRECT_QUEUE}) public void receiveMessage2(String message) { System.out.println("接收到的mq消息:"+message); } }
package com.java1234.consumer.service;
public interface RabbitMqService {
/**
* 接受消息
*/
public void receiveMessage();
/**
* 接受消息
*/
public void receiveMessage2(String message);
}
package com.java1234.consumer; import com.java1234.consumer.service.RabbitMqService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { ApplicationContext ac = SpringApplication.run(ConsumerApplication.class,args); // RabbitMqService rabbitMqService=(RabbitMqService) ac.getBean("rabbitmqService"); // rabbitMqService.receiveMessage(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。