无法从设备接收UDP响应

问题描述 投票:1回答:1

我正在尝试发送UDP请求并接收响应。 Spring Integration具有执行此类任务的适当工具:UnicastSendingMessageHandlerUnicastReceivingChannelAdapter。我以以下方式配置它

@Bean
public MessageChannel requestChannel() {
    return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel = "requestChannel")
public UnicastSendingMessageHandler unicastSendingMessageHandler() {
    UnicastSendingMessageHandler unicastSendingMessageHandler = new UnicastSendingMessageHandler("239.255.255.250", 1982);
    return unicastSendingMessageHandler;
}

@Bean
public UnicastReceivingChannelAdapter unicastReceivingChannelAdapter() {
    UnicastReceivingChannelAdapter unicastReceivingChannelAdapter = new UnicastReceivingChannelAdapter(8080);
    unicastReceivingChannelAdapter.setOutputChannelName("nullChannel");
    return unicastReceivingChannelAdapter;
}

我如何发送消息(我想在任何地方使用sendDiscoveryMessage()):

@Service
public class DiscoveryService {

    private static final String DISCOVERY_MESSAGE = "M-SEARCH * HTTP/1.1\r\n"
            + "HOST: 239.255.255.250:1982\r\n"
            + "MAN: \"ssdp:discover\"\r\n"
            + "ST: wifi_bulb";

    private final MessageChannel requestChannel;

    public DiscoveryService(final MessageChannel requestChannel) {
        this.requestChannel = requestChannel;
    }

    public void sendDiscoveryMessage() {
        requestChannel.send(new GenericMessage<>(DISCOVERY_MESSAGE));
    }
}

[这时,我可以通过WireShark和ensure检查数据包是否已发送且适当的响应也已发送。唯一的问题是如何收到此回复。据我了解的阅读文档,我需要用@ServiceActivator注释的方法。但是我不知道应该在哪里(哪个通道)接收响应(以便正确指定@ServiceActivator(inputChannel=""))。另外,我不确定为@ServiceActivator(inputChannel = "requestChannel") bean放置的UnicastSendingMessageHandler。我尝试创建以下方法(假设响应将到达同一通道):

@ServiceActivator(inputChannel = "requestChannel")
public void receiveResponse(Message<String> response) {
    System.out.println(response);
}

但是它实际上拦截了我自己的请求消息(这对我来说似乎很合理,因为我已将请求发送到requestChannel)。所以我不知道我需要多少个通道(也许我需要1个请求和1个响应)以及如何创建@ServiceActivator来捕获响应。

java spring udp spring-integration
1个回答
0
投票
unicastReceivingChannelAdapter.setOutputChannelName("nullChannel");

您正在将结果发送到nullChannel,类似于Unix上的/dev/null;您正在丢弃它。

使用@ServiceActivator(inputChannel = "replyChannel")

unicastReceivingChannelAdapter.setOutputChannelName("replyChannel");
© www.soinside.com 2019 - 2024. All rights reserved.