我已经实现了一个带有
spring boot(版本1.5.3)框架的Web应用程序.现在我需要一个从客户端接收传入消息的udp服务器.如何将此功能添加到基于
Spring Boot的项目中?
我按照How to implement UDP in Spring framework链接参考,但无法获得w.r.t spring boot
任何人都可以帮助我理解这一点
谢谢
Maruthy
最佳答案 依赖于spring boot集成和spring-ip.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
然后用“UnicastReceivingChannelAdapter”创建简单的“IntegrationFlow”并关联它,创建一个用于接收UDP消息的bean
@Bean
public IntegrationFlow processUniCastUdpMessage() {
return IntegrationFlows
.from(new UnicastReceivingChannelAdapter(11111))
.handle("UDPServer", "handleMessage")
.get();
}
@Service
public class UDPServer
{
public void handleMessage(Message message)
{
String data = new String((byte[]) message.getPayload());
System.out.print(data);
}
}
使用“UnicastSendingMessageHandler”的简单客户端也可以将消息发送到UDP服务器.
UnicastSendingMessageHandler handler =
new UnicastSendingMessageHandler("localhost", 11111);
String payload = "Hello world";
handler.handleMessage(MessageBuilder.withPayload(payload).build());