1

我有这样的 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”的频道时,它只打印“ANOTHER CHANNEL HERE”并忽略其他订阅者。但是,如果我用 调用 .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);
4

1 回答 1

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 找不到带有 的 beanpublishAction并创建一个 - DirectChannel

您可以使用方法参考

IntegrationFlows.from(publishAction())

或者,如果它在不同的配置类中,您可以重新使用预定义名称之一”

 IntegrationFlows.from(publishCha.input)

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

于 2018-05-14T13:05:10.030 回答