2016-12-15 1248 views
1

我正在将使用旧版本的Spring(使用XML配置)创建的项目迁移到Spring Boot(使用Java配置)。 该项目使用Spring Integration通过JMS和AMQP进行通信。据我了解,我有在外部接口弹簧“@MessagingGateway”注释

<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?

在此先感谢!

+0

你不必做任何事情......你仍然可以使用XML配置,你不必迁移一切基于Java配置。 –

+0

这是正确的,对于Spring Boot它不是必需的。但部门想切换到基于Java的配置。 :) – NagelAufnKopp

回答

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

+0

这个伎俩!如此小而简单。 :) 谢谢! – NagelAufnKopp

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); 

    } 

}