我有一个Spring Boot / Integration应用程序,其集成流程以RabbitMQ队列开头。流本身和应用程序通常都运行良好,但是初始入站AMQP配置有很多冗余配置。
目前我有十DataType
个,每个都有略微变化的属性,其中一些需要在运行时定义。我为每个初始化一个入站流,设置几个标头,然后快速将它们转储到一个公共通道进行处理。
NetworkDiagnostic
DataType
的一个的Java配置如下所示:
@Bean
public IntegrationFlow inboundNetworkDiagnosticFlow(@Qualifier("connectionFactory") ConnectionFactory connectionFactory,
@Qualifier(BeanNames.INBOUND_EVENTS_CHANNEL) MessageChannel outbound,
@Qualifier(BeanNames.JSON_NODE_MESSAGE_CONVERTER) MessageConverter messageConverter,
@Qualifier("networkDiagnosticQueue") Queue queue,
@Value("${networkDiagnostic.numConsumers}") int numConsumers,
@Value("${networkDiagnostic.prefetchCount}") int prefetchCount) {
return makeEventIntegrationFlow(connectionFactory, outbound, messageConverter, queue, numConsumers, prefetchCount,
DataTypes.EVENT_NETWORK_DIAGNOSTIC);
}
@Bean
public Binding networkDiagnosticBinding(@Qualifier("networkDiagnosticQueue") Queue queue) {
return makeFanoutBinding(queue, NETWORK_DIAGNOSTIC_EXCHANGE_NAME);
}
@Bean
public Queue networkDiagnosticQueue() {
return makeQueue(NETWORK_DIAGNOSTIC_QUEUE_STRING);
}
@Bean
public FanoutExchange networkDiagnosticExchange() {
return new FanoutExchange(NETWORK_DIAGNOSTIC_EXCHANGE_NAME);
}
另外九个并行配置。我想更多地考虑这一点,以便a)重复被删除,以及b)可以从服务器上的配置文件中简单地配置更多输入。
我的一般想法是我会有一个yaml配置文件:
data_types:
- name: network-diagnostic
schema: event
window_type: hourly
exchange_name: blahblahblah
queue_name: blahblahblah
...
- name: log-diagnostic
...
,通过@ConfigurationProperties
我会映射到或多或少的类:
/**
* Organizes information and configuration for a DataType
*/
public class DataType {
private String name;
private Schema schema;
private WindowType windowType;
private long bucketLength;
private String exchange;
private String routingKey;
...
而且我需要一些方法 - registerAllBeans
- 期望所有的DataTypes,它们会创建所有必需的bean(及其相互关系),并在每个上调用SingletonBeanRegistry::registerSingleton
。
那就是说,我不确定该方法何时应该运行,以及如何让它运行呢。一方面,我需要在配置属性创建的bean可访问之后运行,但在生命周期管理开始之前(因此我的集成流程将被管理),最好在RabbitAdmin::afterPropertiesSet
之前,所以我也可以获得我的RabbitMQ对象的隐式声明。
我怎样才能做到这一点?
更新:我按照下面的@ ArtemBilan的建议编写了一个模拟示例代码,我将在此处进行编写。
主要类别:
@EnableAutoConfiguration
@Configuration
public class DemoApplication {
public static void main(String[] args) {
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(DemoApplication.class);
parentBuilder.child(ChildConfiguration.class).properties("name=bob", "message='hi how are you?'").run();
parentBuilder.child(ChildConfiguration.class).properties("name=jane", "message='hi how are you?'").run();
}
@Bean
public IntegrationFlow integrationFlow() {
Object object = new Object();
return IntegrationFlows.from("inputChannel")
.handle(m -> System.out.println(object + " " + m.getPayload() + " " + System.currentTimeMillis()))
.get();
}
}
子配置:
@EnableAutoConfiguration
@EnableConfigurationProperties(Sample.class)
@Configuration
public class ChildConfiguration {
@Bean
public IntegrationFlow anotherOutgoingFlow(Sample sample) {
return IntegrationFlows
.from(() -> new GenericMessage<>("hello " + sample.getName() + "; " + sample.getMessage()),
m -> m.poller(Pollers.fixedDelay(500)))
.channel("inputChannel")
.get();
}
}
模型类:
@ConfigurationProperties
public class Sample {
private String name;
private String message;
public Sample() { }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
打印,例如:
2016-03-25 17:12:04.109 INFO 24637 --- [ main] com.example.DemoApplication : Started DemoApplication in 0.169 seconds (JVM running for 3.878)
java.lang.Object@25c4da11 hello bob; 'hi how are you?' 1458940324438
java.lang.Object@25c4da11 hello jane; 'hi how are you?' 1458940324607
java.lang.Object@25c4da11 hello bob; 'hi how are you?' 1458940324938
java.lang.Object@25c4da11 hello jane; 'hi how are you?' 1458940325108
java.lang.Object@25c4da11 hello bob; 'hi how are you?' 1458940325439
答案 0 :(得分:1)
当您能够根据提供的环境重用模板配置时,请考虑为您的应用程序使用parent/child
架构。
有关详细信息,请参阅Spring Boot Reference Manual。