Spring Integration 2.2.3.RELEASE @ServiceActivator未调用

时间:2018-11-13 16:14:21

标签: java spring spring-integration integration spring-annotations

我有这些版本的遗留代码(Spring版本:3.2.1.RELEASE和Spring Integration版本2.2.3.RELEASE)。我想将spring集成的所有xml配置迁移到java配置。 在开始迁移之前,我正在做一些测试,并且在@ServiceActivator中苦苦挣扎。 我的示例是:

@Configuration
@ImportResource("classpath*:integration-config.xml")
public static class IntegrationConfig {
    @Bean
    public MessageChannel queueChannel() {
        return new QueueChannel(5);
    }

    @Bean
    public MessageChannel pubSubChannel() {
        PublishSubscribeChannel publishSubscribeChannel = new PublishSubscribeChannel();
        publishSubscribeChannel.subscribe(new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                System.out.println("First handler :" + message.getPayload());
            }
        });

        publishSubscribeChannel.subscribe(new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                System.out.println("Second handler :" + message.getPayload());
            }
        });
     return publishSubscribeChannel;
    }
}

然后:

@MessageEndpoint
public static class MessageEndpointDefintion {

    @ServiceActivator(inputChannel = "queueChannel")
    public void queueActivator(String payload) {
        System.out.println("Payload from queue : " + payload);
    }

    @ServiceActivator(inputChannel = "pubSubChannel")
    public void pubSubActivator(Message<String> message) {
        System.out.println("Payload from pub-sub :" + message.getPayload());
    }
}

当我在频道中发送一些消息时,任何@ServiceActivator都会被调用。

here:所示,添加@EnableIntegration(自Spring Integration版本4起可用)可以解决该问题,但是在我的情况下,我使用的是不提供此注释的版本2.2.3。如果他们在此版本中提供此注释,我认为我们可以使用它,但是如何使用?

其他问题,我可以将Spring Integration版本升级到与Spring(3.2.1)相同的版本吗? 谢谢

编辑

我这样称呼我的配置:      AbstractApplicationContext context = new AnnotationConfigApplicationContext(IntegrationConfig.class);和测试示例:

PublishSubscribeChannel publishSubscribeChannel;
publishSubscribeChannel = (PublishSubscribeChannel) context.getBean("pubSubChannel", MessageChannel.class);
publishSubscribeChannel.send(MessageBuilder.withPayload("==PUB-SUB-CHANNEL== ").build());

还有integration-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.springframework.org/schema/c"
   xmlns:int="http://www.springframework.org/schema/integration"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">

<int:annotation-config/>

<xsd:element name="annotation-config">
    <xsd:annotation>
        <xsd:documentation>
            Enables annotation support for Message Endpoints.
        </xsd:documentation>
    </xsd:annotation>
</xsd:element>

1 个答案:

答案 0 :(得分:1)

您缺少将其添加到XML配置中的方法:

<int:annotation-config/>

<xsd:element name="annotation-config">
    <xsd:annotation>
        <xsd:documentation>
            Enables annotation support for Message Endpoints.
        </xsd:documentation>
    </xsd:annotation>

不幸的是,旧版本不能仅使用纯注释。整个Spring Integration引擎是从XML触发的。

而且不行:如果不升级Spring Framework,就无法升级Spring Integration。