我已经使用spring框架实现了一个Web应用程序。 现在我需要一个udp服务器来接收来自客户端(Android设备)的传入消息。 如何将此功能添加到基于Spring的项目中? 感谢。
答案 0 :(得分:6)
如果您想使用TCP and UDP Support的Spring Integration,请求您只接收UDP消息并对该消息执行某些操作,您应该按照以下步骤操作:
package com.example.udp;
import org.springframework.messaging.Message;
public class UDPConsumer {
@Autowire what you want, this will be a Spring Bean
@ServiceActivator
public void consume(Message message) {
... do something with message ...
}
}
udp-server.threads=10
udp-server.port=4000
udp-server.buffer-size=500
...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
<context:property-placeholder location="classpath:udp-server.properties" />
<bean id="udpConsumer" class="com.example.udp.UDPConsumer" />
<int:channel id="inputChannel">
<int:queue />
</int:channel>
<int-ip:udp-inbound-channel-adapter id="udpReceiver"
channel="inputChannel"
port="${udp-server.port}"
pool-size="${udp-server.threads}"
receive-buffer-size="${udp-server.buffer-size}"
multicast="false"
check-length="true"/>
<int:service-activator input-channel="inputChannel"
ref="udpConsumer" />
<int:poller default="true" fixed-rate="500" />
</beans>
注释:Spring Integration有许多有趣的功能,如消息路由和转换。我建议您准确查看官方文档。