在外部接口上弹出“@MessagingGateway”注释

时间:2016-12-15 10:43:22

标签: spring spring-integration spring-4

我准备将使用旧版Spring(使用XML配置)创建的项目迁移到Spring Boot(使用Java配置)。 该项目使用Spring Integration通过JMS和AMQP进行通信。据我了解,我必须更换

<int:gateway id="someID" service-interface="MyMessageGateway" 
 default-request-channel="myRequestChannel" 
 default-reply-channel="myResponseChannel" 
 default-reply-timeout="20000" />

@MessagingGateway(name="someID", defaultRequestChannel = "myRequestChannel",
defaultReplyChannel = "myResponseChannel", defaultReplyTimeout = "20000")
public interface MyMessageGateway{ ...... }

我的问题是,现在正在使用的界面放在我无法访问的库中。

如何将此界面定义为我的MessagingGateway?

提前致谢!

2 个答案:

答案 0 :(得分:0)

使用GatewayProxyFactoryBean;这是一个简单的例子:

@SpringBootApplication
public class So41162166Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(So41162166Application.class, args);
        context.getBean(NoAnnotationsAllowed.class).foo("foo");
        context.close();
    }

    @Bean
    public GatewayProxyFactoryBean gateway() {
        GatewayProxyFactoryBean gateway = new GatewayProxyFactoryBean(NoAnnotationsAllowed.class);
        gateway.setDefaultRequestChannel(channel());
        return gateway;
    }

    @Bean
    public MessageChannel channel() {
        return new DirectChannel();
    }

    @ServiceActivator(inputChannel = "channel")
    public void out(String foo) {
        System.out.println(foo);
    }

    public static interface NoAnnotationsAllowed {

        public void foo(String out);

    }

}

答案 1 :(得分:0)

我刚试过这个伎俩:

interface IControlBusGateway {

    void send(String command);
}

@MessagingGateway(defaultRequestChannel = "controlBus")
interface ControlBusGateway extends IControlBusGateway {

}

...


@Autowired
private IControlBusGateway controlBus;

...

try {
        this.bridgeFlow2Input.send(message);
        fail("Expected MessageDispatchingException");
    }
    catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(MessageDispatchingException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers"));
    }
    this.controlBus.send("@bridge.start()");
    this.bridgeFlow2Input.send(message);
    reply = this.bridgeFlow2Output.receive(5000);
    assertNotNull(reply);

换句话说,你可以只extends到你本地的外部接口。 GatewayProxyFactoryBean会在下面为你做代理魔术。

我们也有类似用例的JIRA:https://jira.spring.io/browse/INT-4134