2012-09-27 48 views
4

我想知道是否有可能創建一個能夠接受請求並返回響應的彈簧集成框架的UDP服務器。與彈簧集成的UDP服務器

對於TCP有TCP網關,它允許請求/響應處理,但我沒有看到類似的東西的UDP。

這很容易設置UDP監聽器和接收數據包,但我看不到如何返回響應,因爲我只能將它路由到預定義的輸出通道。

此外,我沒有看到發件人的IP和端口,因爲變壓器沒有收到DatagramPacket對象,但只收到數據。

這裏是我的配置:

<int:channel id="ChannelIn" /> 

<ip:udp-inbound-channel-adapter id="ChannelReceiver" 
    channel="ChannelIn" 
    port="5555" 
    multicast="false" 
    check-length="false" 
    pool-size="10" 
    /> 

<int:transformer 
    ref="datagramToPacketTransformer" 
    input-channel="ChannelIn" 
    output-channel="ChannelSA" 
    method="toPacket"/> 

<int:channel id="ChannelSA" /> 

<int:service-activator id="ChannelActivator" 
    input-channel="ChannelSA" 
    ref="PacketHandler" 
    method="process" 
/> 

回答

1

我兩年前

https://jira.springsource.org/browse/INT-1809

開闢了新的功能,JIRA,但沒有收到票,或觀察者,所以我關閉了它。

隨時添加評論,我們可以重新打開它。

發件人的IP是在郵件頭,而不是他的口...

  message = MessageBuilder.withPayload(payload) 
        .setHeader(IpHeaders.HOSTNAME, hostName) 
        .setHeader(IpHeaders.IP_ADDRESS, hostAddress) 
        .build(); 

如果你想打開一個新的JIRA爲,這是一個足夠小的改變就可以進入2.2。即將推出的版本(但網關不會讓2.2)。

https://jira.springsource.org/browse/INT

-1

謝謝你,加里!

我注意到遠程IP地址在標題中,但遠程端口不是這樣,所以我做了一些修改來添加它。

我已經擴展了UnicastReceivingChannelAdapter來覆蓋構建Message的asyncSendMessage。我也必須擴展DatagramPacketMessageMapper。如果我可以在UnicastReceivingChannelAdapter中注入我自己的DatagramPacketMessageMapper實現,那會更容易,但這是不可能的。

然後在春天的配置我已經刪除了

<ip:udp-inbound-channel-adapter id="ChannelReceiver"...> 

定義,並添加以下

<bean id="udpInboundChannelAdapterBean" 
      class="me.spring.integration.udp.UnicastReceivingChannelAdapterExt" init-method="run"> 
      <constructor-arg name="port" value="5555" /> 
      <property name="poolSize" value="10"/> 
      <property name="lookupHost" value="false"/> 
      <property name="outputChannel" ref="ChannelIn" /> 

這似乎是工作,雖然我沒有時間全面測試它。

我的想法是使用遠程IP和端口以及來自udpInboundChannelAdapterBean的DatagramSocket引用來創建一個新的激活服務,該服務僅將響應寫入套接字。

我會按照您的建議爲遠程端口打開一個新的jira。

+0

這裏的JIRA;應該把它變成2.2 ... https://jira.springsource.org/browse/INT-2776 –

2

我發現了一種處理UDP的方式。下面是我的配置

<int-ip:udp-inbound-channel-adapter id="ChannelReceiver" 
            channel="serverBytes2StringChannel" 
            port="9000" 
            multicast="false" 
            check-length="false" 
            pool-size="10" 
            lookup-host="false" 
/> 

<int:transformer id="serverBytes2String" 
       input-channel="serverBytes2StringChannel" 
       output-channel="udpSA" 
       expression="new String(payload)" 

/> 

<int:service-activator input-channel="udpSA" 
         ref="deviceService" 
         method="udpMessage"/> 

而且deviceService豆有這樣的代碼:

public Message udpMessage(Message message) { 
     String response = "KO"; 
     try { 
      response = process(message); 
     } catch (Throwable th) { 
      //do something 
     } 
     Message msg = MessageBuilder.withPayload(response.getBytes()).copyHeaders(message.getHeaders()).build(); 
     sendReply(msg); 
     return null; 
    } 
    private void sendReply(Message message) { 
     try { 
      int port = (Integer) message.getHeaders().get("ip_port"); 
      String ip = (String) message.getHeaders().get("ip_address"); 
      InetAddress IPAddress = InetAddress.getByName(ip); 

      byte[] sentence = (byte[]) message.getPayload(); 
      byte[] sendData = new byte[sentence.length]; 
      byte[] receiveData = new byte[1024]; 
      sendData = sentence; 
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 
      DatagramSocket clientSocket = new DatagramSocket(); 
      clientSocket.send(sendPacket); 
      clientSocket.close(); 
     } catch (Exception ex) { 
      throw new RuntimeException("KO"); 
     } 
    } 

當我部署在亞馬遜AWS這段代碼就無法正常工作,這是我的個人電腦上卻可以正常使用。我發現即使程序正在監聽UDP 9000端口,但在服務器端沒有收到通信。這可能是AWS設置的一個問題,儘管我已經允許該AWS實例的所有UDP端口。

如何使亞馬遜AWS這項工作任何提示?