我一直在研究Spring集成ip模块,我想创建UDP channel 用于接收,但我发现我只能用XML来做到这一点。 我当时想,如果我查看一下实现代码,我可以弄清楚一些事情,但它会根据 xml 中提供的参数创建 bean 定义本身。 我无法在代码中使用 xml 定义,有没有办法让它在没有 xml 的情况下与 spring 一起工作?
或者,java中有没有更好的方法来使用udp?
最佳答案
从版本 5.0
开始,Java DSL 已经解决了这个问题,因此 UDP channel 适配器的代码可能如下所示:
@Bean
public IntegrationFlow inUdpAdapter() {
return IntegrationFlows.from(Udp.inboundAdapter(0))
.channel(udpIn())
.get();
}
@Bean
public QueueChannel udpIn() {
return new QueueChannel();
}
@Bean
public IntegrationFlow outUdpAdapter() {
return f -> f.handle(Udp.outboundAdapter(m -> m.getHeaders().get("udp_dest")));
}
但使用现有的 Spring Integration 版本,您可以简单地配置 UnicastReceivingChannelAdapter
bean:
@Bean
public UnicastReceivingChannelAdapter udpInboundAdapter() {
UnicastReceivingChannelAdapter unicastReceivingChannelAdapter = new UnicastReceivingChannelAdapter(1111);
unicastReceivingChannelAdapter.setOutputChannel(udpChannel());
return unicastReceivingChannelAdapter;
}
在引用手册中,您可以找到 Tips and Tricks章节了解如何使用原始 Java 和注释配置编写 Spring Integration 应用程序的一些信息。
我添加了JIRA解决引用手册中的 Java 示例。
关于java - Spring 集成 ip - 仅使用 java 代码的 udp channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181305/