我有这样的PublishSubscribeChannel:
@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
中name
的目的:
/**
* 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 {};
因此,在这种情况下,作为 Bean 名称的方法名称将被忽略,因此 Spring Integration Java DSL 找不到具有publishAction
的 Bean 并创建一个 -DirectChannel
。
不过,您可以使用方法引用:
IntegrationFlows.from(publishAction())
或者,如果它在不同的配置类中,您可以重用预定义的名称之一">
IntegrationFlows.from(publishCha.input)
这样,DSL将重用现有的bean,并且只会向该发布-订阅通道添加一个订阅者。