欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

javaspringBoot配置外部rabbitMQDemo

时间:2023-06-17

org.springframework.boot spring-boot-starter-amqp

这个包含rabbitMq的所有依赖,导入之后,就不需要导入任何单独的依赖(都可兼容),可见springBoot之强

加入这段到配置文件中

@Bean(name = "decorationConnectionFactory")@Primarypublic ConnectionFactory pmsConnectionFactory() { return connectionFactory();}public ConnectionFactory connectionFactory(){ CachingConnectionFactory connectionFactory=new CachingConnectionFactory(); connectionFactory.setHost("192.168.31.29"); connectionFactory.setPort(5672); connectionFactory.setUsername("admin"); connectionFactory.setPassword("12345678"); return connectionFactory;}@Bean(name = "decorationRabbitListenerContainerFactory")@Primarypublic RabbitListenerContainerFactory pmsRabbitListenerContainerFactory(@Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); return factory;}@Bean(name = "BPMRabbitMQTemplate")@Primarypublic RabbitTemplate rabbitTemplate( @Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); return rabbitTemplate;}

package ljqc.conf;import org.springframework.amqp.core.*;import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;import org.springframework.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;@Configurationpublic class DirectRabbitConfig { @Bean(name = "decorationConnectionFactory") @Primary public ConnectionFactory pmsConnectionFactory() { return connectionFactory(); } public ConnectionFactory connectionFactory(){ CachingConnectionFactory connectionFactory=new CachingConnectionFactory(); connectionFactory.setHost("192.168.31.29"); connectionFactory.setPort(5672); connectionFactory.setUsername("admin"); connectionFactory.setPassword("12345678"); return connectionFactory; } @Bean(name = "decorationRabbitListenerContainerFactory") @Primary public RabbitListenerContainerFactory pmsRabbitListenerContainerFactory(@Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); return factory; } @Bean(name = "BPMRabbitMQTemplate") @Primary public RabbitTemplate rabbitTemplate( @Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory ) { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); return rabbitTemplate; } //队列 起名:TestDirectQueue @Bean public Queue TestDirectQueue() { // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效 // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。 // return new Queue("TestDirectQueue",true,true,false); //一般设置一下队列的持久化就好,其余两个就是默认false return new Queue("suyuanT3-local",true); //本地 } //Direct交换机 起名:TestDirectExchange @Bean DirectExchange TestDirectExchange() { // return new DirectExchange("TestDirectExchange",true,true); return new DirectExchange("TestDirectExchange",true,false); } //绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting @Bean Binding bindingDirect() { return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting"); } @Bean DirectExchange lonelyDirectExchange() { return new DirectExchange("lonelyDirectExchange"); }}

 注释掉依然可以用

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。