我的应用程序有两个兔子实例,它需要连接到。
Rabbit1是我的应用程序的入口点,我的侦听器在那里等待消息和
@RabbitListener(queues = "#{myAmqpProperties.getRequest().getQueue()}")它是通过常规的Springboot2属性配置的。
spring:
rabbitmq:
host: localhost
username: myUser
password: myPass
port: 5672
virtual-host: myVhost这个很好用。
现在,我需要在另一个rabbitMQ实例Rabbit2上发送兔子消息。因此,我创建了一个配置类,构建rabbitTemplete及其相关的connectionFactory。
package com.mycompany.socle.amqp.ocr;
import com.mycompany.doccontrol.messaging.app.AppMessageConverter;
import com.mycompany.socle.amqp.common.service.CpyAmqpRequestWithReplyToProp;
import com.mycompany.socle.amqp.common.service.CpyAmqpServerProperties;
import com.mycompany.socle.common.bean.SslProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.validation.Valid;
@Configuration
@EnableRabbit
public class OcrAmqpConfiguration {
@Autowired
private OcrAmqpProperties ocrAmqpProperties;
/**
* Template used to send analysis request.
*
* @return the template to use
* @throws Exception if there are issues while initialising the connection factory.
*/
@Bean
public RabbitTemplate appRequestTemplate() throws Exception {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(ocrConnectionFactory());
rabbitTemplate.setMessageConverter(new AppMessageConverter());
@Valid final CpyAmqpRequestWithReplyToProp appProperties = ocrAmqpProperties.getApp().getRequest();
// Settings to send the request
rabbitTemplate.setExchange(appProperties.getExchange());
if (appProperties.getRoutingKey() != null) {
rabbitTemplate.setRoutingKey(appProperties.getRoutingKey());
}
return rabbitTemplate;
}
/**
* Define the Connection factory used to contact the broker.
*
* @return the connection factory
* @throws Exception if there are issues while defining the factory context.
*/
@Bean
public CachingConnectionFactory ocrConnectionFactory() throws Exception {
final CachingConnectionFactory result = new CachingConnectionFactory(builConnectionFactory(ocrAmqpProperties.getRabbitmq()).getObject());
result.afterPropertiesSet();
return result;
}
private static RabbitConnectionFactoryBean builConnectionFactory(MyAmqpServerProperties pAmqpProperties) {
RabbitConnectionFactoryBean factory = new RabbitConnectionFactoryBean();
// Generic connection properties
factory.setHost(pAmqpProperties.getHost());
factory.setPort(pAmqpProperties.getPort());
factory.setUsername(pAmqpProperties.getUsername());
factory.setPassword(pAmqpProperties.getPassword());
factory.setVirtualHost(pAmqpProperties.getVirtualHost());
factory.afterPropertiesSet();
return factory;
}
}类MyAmqpServerProperties只是一个属性类,它与我在属性文件中添加的用于定义Rabbit2信息的额外属性相匹配。
但是没有生成默认的SpringBoot ConnectionFactory,我的侦听器最初听的是Rabbit1,现在也听Rabbit2了。
我看到RabbitAutoConfiguration java doc说:
*
在代码中有注释
@ConditionalOnMissingBean(ConnectionFactory.class)
=>如果我删除了ocrConnectionFactory方法的@Bean,它可以正常工作,但是我需要在spring上下文中注册ocrConnectionFactory来监视.那么,是否有一种方法可以在生成默认属性之后注册它,或者有其他一些属性强制默认属性按正常方式生成?
发布于 2019-09-06 15:28:53
不是的。如果没有启动,引导将只配置一个。如果配置一个,则必须同时配置两者。
https://stackoverflow.com/questions/57823157
复制相似问题