在 Spring 集成中,消息通道可以这样配置:
<int:channel id="get_send_channel" />
<int:channel id="get_receive_channel">
<int:queue capacity='10' />
</int:channel>
<int-http:outbound-gateway id="get.outbound.gateway"
request-channel="get_send_channel"
url="http://localhost:8080/greeting"
http-method="GET" reply-channel="get_receive_channel"
expected-response-type="java.lang.String">
</int-http:outbound-gateway>
并像这样使用:
@SpringBootApplication
@ImportResource("http-outbound-gateway.xml")
public class HttpApplication {
@Autowired
@Qualifier("get_send_channel")
MessageChannel getSendChannel;
@Autowired
@Qualifier("get_receive_channel")
PollableChannel getReceiveChannel;
public static void main(String[] args) {
SpringApplication.run(HttpApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
Message<?> message = MessageBuilder.withPayload("").build();
getSendChannel.send(message);
System.out.println(getReceiveChannel.receive().getPayload());
};
}
如何动态创建和注册 MessageChannel?
上面的代码来自这个例子
我现在已经试过了
return IntegrationFlows.from(MessageChannels.rendezvous("getSend1"))
.handle(Http.outboundGateway("http://localhost:8080/greeting").httpMethod(HttpMethod.GET))
.channel(MessageChannels.queue("getReceive1")).get();
使用默认轮询器,但有消息:
preReceive on channel 'getSend1'
postReceive on channel 'getSend1', message is null
Received no Message during the poll, returning 'false'
所以配置似乎不正确,消息不是从 URL 中获取的。