代码之家  ›  专栏  ›  技术社区  ›  sarah

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

  •  1
  • sarah  · 技术社区  · 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
    

    编辑:NarayanSambireddy询问了我如何向频道发送消息。我通过网关发送:

    @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);
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Artem Bilan    8 年前

    您与第三个订阅者的问题是您错过了 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 并创建一个- DirectChannel 。

    您可以使用 方法参考 尽管:

    IntegrationFlows.from(publishAction())
    

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

     IntegrationFlows.from(publishCha.input)
    

    这样,DSL将重用现有bean,只需向该发布子频道添加一个订阅者。