Class UnicastSendingMessageHandler

All Implemented Interfaces:
Runnable, org.reactivestreams.Subscriber<Message<?>>, Aware, BeanFactoryAware, BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, Lifecycle, Ordered, ExpressionCapable, Orderable, IntegrationPattern, CommonSocketOptions, NamedComponent, IntegrationManagement, ManageableLifecycle, TrackableComponent, MessageHandler, reactor.core.CoreSubscriber<Message<?>>
Direct Known Subclasses:
MulticastSendingMessageHandler

public class UnicastSendingMessageHandler extends AbstractInternetProtocolSendingMessageHandler implements Runnable
A MessageHandler implementation that maps a Message into a UDP datagram packet and sends that to the specified host and port.

Messages can be basic, with no support for reliability, can be prefixed by a length so the receiving end can detect truncation, and can require a UDP acknowledgment to confirm delivery.

Since:
2.0
Author:
Gary Russell, Marcin Pilaczynski, Artem Bilan, Christian Tzolov
  • Field Details

    • lock

      protected final Lock lock
  • Constructor Details

    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(String host, int port)
      Basic constructor; no reliability; no acknowledgment.
      Parameters:
      host - Destination host.
      port - Destination port.
    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(String destinationExpression)
      Construct UnicastSendingMessageHandler based on the destination SpEL expression to determine the target destination at runtime against requestMessage.
      Parameters:
      destinationExpression - the SpEL expression to evaluate the target destination at runtime. Must evaluate to String, URI or SocketAddress.
      Since:
      4.3
    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(Expression destinationExpression)
      Construct UnicastSendingMessageHandler based on the destination SpEL expression to determine the target destination at runtime against requestMessage.
      Parameters:
      destinationExpression - the SpEL expression to evaluate the target destination at runtime. Must evaluate to String, URI or SocketAddress.
      Since:
      4.3
    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(String host, int port, boolean lengthCheck)
      Can used to add a length to each packet which can be checked at the destination.
      Parameters:
      host - Destination Host.
      port - Destination Port.
      lengthCheck - If true, packets will contain a length.
    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(String host, int port, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
      Add an acknowledgment request to packets.
      Parameters:
      host - Destination Host.
      port - Destination Port.
      acknowledge - If true, packets will request acknowledgment.
      ackHost - The host to which acks should be sent. Required if ack true.
      ackPort - The port to which acks should be sent.
      ackTimeout - How long we will wait (milliseconds) for the ack.
    • UnicastSendingMessageHandler

      public UnicastSendingMessageHandler(String host, int port, boolean lengthCheck, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
      Add a length and/or acknowledgment request to packets.
      Parameters:
      host - Destination Host.
      port - Destination Port.
      lengthCheck - If true, packets will contain a length.
      acknowledge - If true, packets will request acknowledgment.
      ackHost - The host to which acks should be sent. Required if ack true.
      ackPort - The port to which acks should be sent.
      ackTimeout - How long we will wait (milliseconds) for the ack.
  • Method Details

    • setLengthCheck

      public void setLengthCheck(boolean lengthCheck)
      Parameters:
      lengthCheck - if true, a four byte binary length header is added to the packet, allowing the receiver to check for data truncation.
      Since:
      5.0
    • setSocketCustomizer

      public void setSocketCustomizer(SocketCustomizer socketCustomizer)
      Set a customizer to further configure the socket after creation.
      Parameters:
      socketCustomizer - the customizer.
      Since:
      5.3.3
    • setReliabilityAttributes

      protected final void setReliabilityAttributes(boolean lengthCheck, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
    • doStart

      public void doStart()
      Specified by:
      doStart in class AbstractInternetProtocolSendingMessageHandler
    • doStop

      protected void doStop()
      Specified by:
      doStop in class AbstractInternetProtocolSendingMessageHandler
    • handleMessageInternal

      public void handleMessageInternal(Message<?> message)
      Specified by:
      handleMessageInternal in class AbstractMessageHandler
    • startAckThread

      public void startAckThread()
    • convertAndSend

      protected void convertAndSend(Message<?> message) throws IOException, URISyntaxException
      Throws:
      IOException
      URISyntaxException
    • setSocket

      protected void setSocket(DatagramSocket socket)
    • getTheSocket

      @Nullable protected DatagramSocket getTheSocket()
    • getSocket

      protected DatagramSocket getSocket() throws IOException
      Throws:
      IOException
    • updateAckAddress

      protected void updateAckAddress()
    • setSoReceiveBufferSize

      public void setSoReceiveBufferSize(int size)
      Specified by:
      setSoReceiveBufferSize in interface CommonSocketOptions
      Overrides:
      setSoReceiveBufferSize in class AbstractInternetProtocolSendingMessageHandler
      Parameters:
      size - The receive buffer size.
      See Also:
    • setLocalAddress

      public void setLocalAddress(String localAddress)
      Description copied from interface: CommonSocketOptions
      On a multi-homed system, specifies the ip address of the network interface used to communicate. For inbound adapters and gateways, specifies the interface used to listed for incoming connections. If omitted, the endpoint will listen on all available adapters. For the UDP multicast outbound adapter specifies the interface to which multicast packets will be sent. For UDP unicast and multicast adapters, specifies which interface to which the acknowledgment socket will be bound. Does not apply to TCP outbound adapters and gateways.
      Specified by:
      setLocalAddress in interface CommonSocketOptions
      Parameters:
      localAddress - The local address.
    • setTaskExecutor

      public void setTaskExecutor(Executor taskExecutor)
    • setAckCounter

      public void setAckCounter(int ackCounter)
      Parameters:
      ackCounter - the ackCounter to set
    • setSocketExpression

      public void setSocketExpression(Expression socketExpression)
      Parameters:
      socketExpression - the socket expression to determine the target socket at runtime.
      Since:
      4.3
    • setSocketExpressionString

      public void setSocketExpressionString(String socketExpression)
      Parameters:
      socketExpression - the socket SpEL expression to determine the target socket at runtime.
      Since:
      4.3
    • getComponentType

      public String getComponentType()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this method to provide component type information.
      Specified by:
      getComponentType in interface NamedComponent
      Overrides:
      getComponentType in class MessageHandlerSupport
    • isAcknowledge

      public boolean isAcknowledge()
      Returns:
      the acknowledge
    • getAckPort

      public int getAckPort()
      Returns:
      the ackPort
    • getSoReceiveBufferSize

      public int getSoReceiveBufferSize()
      Returns:
      the soReceiveBufferSize
    • onInit

      protected void onInit()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this for initialization logic.
      Overrides:
      onInit in class IntegrationObjectSupport
    • setSocketAttributes

      protected void setSocketAttributes(DatagramSocket socket) throws SocketException
      Throws:
      SocketException
    • run

      public void run()
      Process acknowledgments, if requested.
      Specified by:
      run in interface Runnable
    • restartAckThread

      public void restartAckThread()
      If exposed as an MBean, can be used to restart the ack thread if a fatal (bind) error occurred, without bouncing the JVM.