2016-12-15 1249 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); 

    } 

}