使用Bean名称与方法名称进行Spring集成通道

时间:2018-05-14 03:22:40

标签: spring spring-integration spring-integration-dsl spring-dsl

我有这样的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请求我如何向频道发送消息。我通过Gateway发送它:

@MessagingGateway
public interface ExampleGateway {

@Gateway(requestChannel = "flow.input")
    void flow(Order orders);
}

在Main:

Order order = new Order();
      order.addItem("PC", "TTEL", 2000, 1)
    ConfigurableApplicationContext ctx = SpringApplication.run(Start.class, args);
    ctx.getBean(ExampleGateway.class).flow(order);

1 个答案:

答案 0 :(得分:2)

您与第三位订阅者的问题是,您错过了name@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 {};

因此,在这种情况下忽略作为bean名称的方法名称,因此Spring Integration Java DSL找不到具有publishAction的bean并创建一个 - DirectChannel

您可以使用方法参考

IntegrationFlows.from(publishAction())

或者,如果它位于不同的配置类中,则可以重复使用其中一个预定义名称“

 IntegrationFlows.from(publishCha.input)

这样DSL将重新使用现有的bean,并且只会向该pub-sub频道添加一个用户。