使用Bean名称与方法名称的Spring集成通道
我在一个频道发表过这样的文章:使用Bean名称与方法名称的Spring集成通道,spring,spring-integration,spring-integration-dsl,spring-dsl,Spring,Spring Integration,Spring Integration Dsl,Spring Dsl,我在一个频道发表过这样的文章: @Bean(name = {"publishCha.input", "publishCha2.input"}) //2 subscribers public MessageChannel publishAction() { PublishSubscribeChannel ps = MessageChannels.publishSubscribe().get(); ps.setMaxSubscribers(8);
@Bean(name = {"publishCha.input", "publishCha2.input"}) //2 subscribers
public MessageChannel publishAction() {
PublishSubscribeChannel ps = MessageChannels.publishSubscribe().get();
ps.setMaxSubscribers(8);
return ps;
}
我还有订户频道:
@Bean
public IntegrationFlow publishCha() {
return f -> f
.handle(m -> System.out.println("In publishCha channel..."));
}
@Bean
public IntegrationFlow publishCha2() {
return f -> f
.handle(m -> System.out.println("In publishCha2 channel..."));
}
最后是另一个订户:
@Bean
public IntegrationFlow anotherChannel() {
return IntegrationFlows.from("publishAction")
.handle(m -> System.out.println("ANOTHER CHANNEL IS HERE!"))
.get();
}
问题是,当我从另一个流调用方法名为“publishAction”的通道时,它只打印“此处的另一个通道”,而忽略其他订阅者。但是如果我打电话给你
.channel(“publishCha.input”)
,这次它进入publishCha和publishCha2订户,但忽略第三个订户
@Bean
public IntegrationFlow flow() {
return f -> f
.channel("publishAction");
}
我的问题是,为什么这两种不同的通灵方法会产生不同的结果
.channel("publishAction") // channeling with method name executes third subscriber
.channel("publishCha.input") // channelling with bean name, executes first and second subscribers
编辑:narayan sambireddy请求如何向频道发送消息。我通过网关发送:
@MessagingGateway
public interface ExampleGateway {
@Gateway(requestChannel = "flow.input")
void flow(Order orders);
}
大体上:
Order order = new Order();
order.addItem("PC", "TTEL", 2000, 1)
ConfigurableApplicationContext ctx = SpringApplication.run(Start.class, args);
ctx.getBean(ExampleGateway.class).flow(order);
您与第三个订阅者之间的问题是您错过了
@Bean
中名称的用途:
/**
* The name of this bean, or if several names, a primary bean name plus aliases.
* <p>If left unspecified, the name of the bean is the name of the annotated method.
* If specified, the method name is ignored.
* <p>The bean name and aliases may also be configured via the {@link #value}
* attribute if no other attributes are declared.
* @see #value
*/
@AliasFor("value")
String[] name() default {};
或者,如果它位于不同的配置类中,则可以重复使用预定义名称之一“
通过这种方式,DSL将重用现有的bean,只需向发布子频道添加一个订阅者。您是否也可以发布您是如何向这些频道发送消息的?要检查您是如何引用这些频道的(beanName或methodName)发送消息时。@narayan sambireddy我补充道。谢谢您花时间解释。
IntegrationFlows.from(publishAction())
IntegrationFlows.from(publishCha.input)