Class UnicastSendingMessageHandler

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.handler.MessageHandlerSupport
org.springframework.integration.handler.AbstractMessageHandler
org.springframework.integration.ip.AbstractInternetProtocolSendingMessageHandler
org.springframework.integration.ip.udp.UnicastSendingMessageHandler
All Implemented Interfaces:
Runnable, org.reactivestreams.Subscriber<org.springframework.messaging.Message<?>>, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.context.Lifecycle, org.springframework.core.Ordered, org.springframework.integration.context.ExpressionCapable, org.springframework.integration.context.Orderable, org.springframework.integration.IntegrationPattern, CommonSocketOptions, org.springframework.integration.support.context.NamedComponent, org.springframework.integration.support.management.IntegrationManagement, org.springframework.integration.support.management.ManageableLifecycle, org.springframework.integration.support.management.TrackableComponent, org.springframework.messaging.MessageHandler, reactor.core.CoreSubscriber<org.springframework.messaging.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
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagement

    org.springframework.integration.support.management.IntegrationManagement.ManagementOverrides
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Lock
     

    Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport

    EXPRESSION_PARSER, logger

    Fields inherited from interface org.springframework.integration.support.management.IntegrationManagement

    METER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAME

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    UnicastSendingMessageHandler(String destinationExpression)
    Construct UnicastSendingMessageHandler based on the destination SpEL expression to determine the target destination at runtime against requestMessage.
    Basic constructor; no reliability; no acknowledgment.
    UnicastSendingMessageHandler(String host, int port, boolean lengthCheck)
    Can used to add a length to each packet which can be checked at the destination.
    UnicastSendingMessageHandler(String host, int port, boolean lengthCheck, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
    Add a length and/or acknowledgment request to packets.
    UnicastSendingMessageHandler(String host, int port, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
    Add an acknowledgment request to packets.
    UnicastSendingMessageHandler(org.springframework.expression.Expression destinationExpression)
    Construct UnicastSendingMessageHandler based on the destination SpEL expression to determine the target destination at runtime against requestMessage.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    convertAndSend(org.springframework.messaging.Message<?> message)
     
    void
     
    protected void
     
    int
     
     
    protected DatagramSocket
     
    int
     
    protected DatagramSocket
     
    void
    handleMessageInternal(org.springframework.messaging.Message<?> message)
     
    boolean
     
    protected void
     
    void
    If exposed as an MBean, can be used to restart the ack thread if a fatal (bind) error occurred, without bouncing the JVM.
    void
    run()
    Process acknowledgments, if requested.
    void
    setAckCounter(int ackCounter)
     
    void
    setLengthCheck(boolean lengthCheck)
     
    void
    setLocalAddress(String localAddress)
    On a multi-homed system, specifies the ip address of the network interface used to communicate.
    protected final void
    setReliabilityAttributes(boolean lengthCheck, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
     
    protected void
     
    protected void
     
    void
    Set a customizer to further configure the socket after creation.
    void
    setSocketExpression(org.springframework.expression.Expression socketExpression)
     
    void
     
    void
     
    void
    setTaskExecutor(Executor taskExecutor)
     
    void
     
    protected void
     

    Methods inherited from class org.springframework.integration.handler.AbstractMessageHandler

    handleMessage, onComplete, onError, onNext, onSubscribe, setObservationConvention

    Methods inherited from class org.springframework.integration.handler.MessageHandlerSupport

    buildSendTimer, destroy, getIntegrationPatternType, getManagedName, getManagedType, getMetricsCaptor, getObservationRegistry, getOrder, getOverrides, isLoggingEnabled, isObserved, registerMetricsCaptor, registerObservationRegistry, sendTimer, setLoggingEnabled, setManagedName, setManagedType, setOrder, setShouldTrack, shouldTrack

    Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport

    afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getConversionService, getExpression, getIntegrationProperties, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentName, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface reactor.core.CoreSubscriber

    currentContext

    Methods inherited from interface org.springframework.integration.support.management.IntegrationManagement

    getThisAs

    Methods inherited from interface org.springframework.integration.support.context.NamedComponent

    getBeanName, getComponentName
  • 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(org.springframework.expression.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(org.springframework.messaging.Message<?> message)
      Specified by:
      handleMessageInternal in class org.springframework.integration.handler.AbstractMessageHandler
    • startAckThread

      public void startAckThread()
    • convertAndSend

      protected void convertAndSend(org.springframework.messaging.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(org.springframework.expression.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()
      Specified by:
      getComponentType in interface org.springframework.integration.support.context.NamedComponent
      Overrides:
      getComponentType in class org.springframework.integration.handler.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()
      Overrides:
      onInit in class org.springframework.integration.context.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.