From 06aa45e91f78a2a72e4d80b5534de0aa77d020fd Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 30 Oct 2017 13:39:19 -0400 Subject: [PATCH] INT-4279: Document UDP Java Configuration JIRA: https://jira.spring.io/browse/INT-4279 Add documentation for Java configuration of UDP channel adapters. Enhance DSL to support host/port for outbound adapters. --- ...AbstractUdpOutboundChannelAdapterSpec.java | 6 +- .../integration/ip/dsl/Udp.java | 28 +++- ...dpMulticastOutboundChannelAdapterSpec.java | 6 +- .../UdpUnicastOutboundChannelAdapterSpec.java | 6 +- src/reference/asciidoc/ip.adoc | 132 +++++++++++++----- 5 files changed, 139 insertions(+), 39 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/AbstractUdpOutboundChannelAdapterSpec.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/AbstractUdpOutboundChannelAdapterSpec.java index a2ee0b6ac8..d914d876eb 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/AbstractUdpOutboundChannelAdapterSpec.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/AbstractUdpOutboundChannelAdapterSpec.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,10 @@ public abstract class AbstractUdpOutboundChannelAdapterSpec { + UdpMulticastOutboundChannelAdapterSpec(String host, int port) { + this.target = new MulticastSendingMessageHandler(host, port); + } + UdpMulticastOutboundChannelAdapterSpec(String destinationExpression) { this.target = new MulticastSendingMessageHandler(destinationExpression); } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/UdpUnicastOutboundChannelAdapterSpec.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/UdpUnicastOutboundChannelAdapterSpec.java index 5988d7dad8..a009a6fb05 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/UdpUnicastOutboundChannelAdapterSpec.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/dsl/UdpUnicastOutboundChannelAdapterSpec.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,10 @@ import org.springframework.messaging.Message; public class UdpUnicastOutboundChannelAdapterSpec extends AbstractUdpOutboundChannelAdapterSpec { + UdpUnicastOutboundChannelAdapterSpec(String host, int port) { + super(host, port); + } + UdpUnicastOutboundChannelAdapterSpec(Function, ?> destinationFunction) { super(destinationFunction); } diff --git a/src/reference/asciidoc/ip.adoc b/src/reference/asciidoc/ip.adoc index ecb8a21ae7..c89a4ddd1b 100644 --- a/src/reference/asciidoc/ip.adoc +++ b/src/reference/asciidoc/ip.adoc @@ -33,6 +33,8 @@ This provides the same basic functionality as described in <>. [[udp-adapters]] === UDP Adapters +==== Outbound (XML Configuration) + [source,xml] ---- ` (`UnicastSendingMessageHandler`). +==== Outbound (Java Configuration) -The `destination-expression` can be used as a runtime alternative to the hardcoded `host`/`port` pair to determine -the destination address for the outgoing datagram packet against `requestMessage` as a root object for evaluation context. -The expression must evaluate to `URI`, or `String` in the URI style (see http://www.ietf.org/rfc/rfc2396.txt[RFC-2396]) -or `SocketAddress`. -The new `IpHeaders.PACKET_ADDRESS` header can be used for this expression as well. -In the Framework this header is populated by the `DatagramPacketMessageMapper`, when we receive datagrams in the -`UnicastReceivingChannelAdapter` and convert them to messages. -The header value is exactly the result of `DatagramPacket.getSocketAddress()` of incoming datagram. - -With the `socket-expression` help the Outbound Channel Adapter can use e.g. Inbound Channel Adapter socket -to send datagrams through same port which they were received. -It's useful in a scenario when our application works as a UDP server and clients operate behind the NAT. -This expression must evaluate to the `DatagramSocket`. -The `requestMessage` is used as a root object for evaluation context. -The `socket-expression` parameter cannot be used with parameters like `multicast` and `acknowledge`. - -[source,xml] +[source, java] ---- - - - - - - - - - +@Bean +@ServiceActivator(inputChannel = "udpOut") +public UnicastSendingMessageHandler handler() { + return new UnicastSendingMessageHandler("localhost", 11111); +} ---- +(or `MulticastSendingChannelAdapter` for multicast). + +==== Outbound (Java DSL Configuration) + +[source, java] +---- +@Bean +public IntegrationFlow udpOutFlow() { + return IntegrationFlows.from("udpOut") + .handle(Udp.outboundAdapter("localhost", 1234)) + .get(); +} +---- + +==== Inbound (XML Configuration) [source,xml] ---- @@ -165,6 +157,82 @@ By default, reverse DNS lookups are done on inbound packets to convert IP addres In environments where DNS is not configured, this can cause delays. This default behavior can be overridden by setting the `lookup-host` attribute to "false". +==== Inbound (Java Configuration) + +[source, java] +---- +@Bean +public UnicastReceivingChannelAdapter udpIn() { + UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(11111); + adapter.setOutputChannelName("udpChannel"); + return adapter; +} +---- + +==== Inbound (Java DSL Configuration) + +[source, java] +---- +@Bean +public IntegrationFlow udpIn() { + return IntegrationFlows.from(Udp.inboundAdapter(11111)) + .channel("udpChannel") + .get(); +} +---- + +==== Advanced Outbound Configuration + +The `destination-expression` and `socket-expression` options are available +for the `` (`UnicastSendingMessageHandler`). + +The `destination-expression` can be used as a runtime alternative to the hardcoded `host`/`port` pair to determine +the destination address for the outgoing datagram packet against a `requestMessage` as the root object for the evaluation context. +The expression must evaluate to an `URI`, or `String` in the URI style (see http://www.ietf.org/rfc/rfc2396.txt[RFC-2396]) +or `SocketAddress`. +The inbound `IpHeaders.PACKET_ADDRESS` header can be used for this expression as well. +In the Framework, this header is populated by the `DatagramPacketMessageMapper`, when we receive datagrams in the +`UnicastReceivingChannelAdapter` and convert them to messages. +The header value is exactly the result of `DatagramPacket.getSocketAddress()` of the incoming datagram. + +With the `socket-expression`, the Outbound Channel Adapter can use e.g. an Inbound Channel Adapter socket +to send datagrams through same port which they were received. +It's useful in a scenario when our application works as a UDP server and clients operate behind NAT. +This expression must evaluate to a `DatagramSocket`. +The `requestMessage` is used as the root object for the evaluation context. +The `socket-expression` parameter cannot be used with parameters `multicast` and `acknowledge`. + +[source,xml] +---- + + + + + + + + + +---- + +The equivalent configuration using Java DSL Configuration: + +[source, java] +---- +@Bean +public IntegrationFlow udpEchoUpcaseServer() { + return IntegrationFlows.from(Udp.inboundAdapter(11111).id("udpIn")) + .transform(p -> new String(p).toUpperCase()) + .handle(Udp.outboundAdapter("headers['ip_packetAddress']") + .socketExpression("@udpIn.socket")) + .get(); +} +---- + [[tcp-connection-factories]] === TCP Connection Factories