Spring Integration

org.springframework.integration.ip.udp
Class UnicastSendingMessageHandler

java.lang.Object
  extended by org.springframework.integration.ip.AbstractInternetProtocolSendingMessageHandler
      extended by org.springframework.integration.ip.udp.UnicastSendingMessageHandler
All Implemented Interfaces:
Runnable, InitializingBean, MessageHandler, CommonSocketOptions
Direct Known Subclasses:
MulticastSendingMessageHandler

public class UnicastSendingMessageHandler
extends AbstractInternetProtocolSendingMessageHandler
implements Runnable, InitializingBean

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

Field Summary
protected  Map<String,CountDownLatch> ackControl
           
protected  int ackCounter
           
protected  boolean acknowledge
           
protected  int ackPort
           
protected  int ackTimeout
           
protected  Exception fatalException
           
protected  String localAddress
           
protected  DatagramPacketMessageMapper mapper
           
protected  DatagramSocket socket
           
protected  int soReceiveBufferSize
           
protected  Executor taskExecutor
           
protected  boolean waitForAck
          If true adds headers to instruct receiving adapter to return an ack.
 
Fields inherited from class org.springframework.integration.ip.AbstractInternetProtocolSendingMessageHandler
destinationAddress, host, logger, port, soSendBufferSize, soTimeout
 
Constructor Summary
UnicastSendingMessageHandler(String host, int port)
          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.
 
Method Summary
 void afterPropertiesSet()
           
protected  DatagramSocket getSocket()
           
 void handleMessage(Message<?> message)
          Handles the message if possible.
 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.
 void run()
          Process acknowledgments, if requested.
protected  void send(DatagramPacket packet)
           
 void setLocalAddress(String localAddress)
          On a multi-homed system, specifies the ip address of the network interface used to communicate.
protected  void setReliabilityAttributes(boolean lengthCheck, boolean acknowledge, String ackHost, int ackPort, int ackTimeout)
           
protected  void setSocketAttributes(DatagramSocket socket)
           
 void setSoReceiveBufferSize(int size)
           
 void setTaskExecutor(Executor taskExecutor)
           
 void shutDown()
           
 
Methods inherited from class org.springframework.integration.ip.AbstractInternetProtocolSendingMessageHandler
getPort, setSoSendBufferSize, setSoTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mapper

protected final DatagramPacketMessageMapper mapper

socket

protected volatile DatagramSocket socket

waitForAck

protected volatile boolean waitForAck
If true adds headers to instruct receiving adapter to return an ack.


acknowledge

protected volatile boolean acknowledge

ackPort

protected volatile int ackPort

ackTimeout

protected volatile int ackTimeout

ackCounter

protected volatile int ackCounter

ackControl

protected volatile Map<String,CountDownLatch> ackControl

fatalException

protected volatile Exception fatalException

soReceiveBufferSize

protected int soReceiveBufferSize

localAddress

protected String localAddress

taskExecutor

protected volatile Executor taskExecutor
Constructor Detail

UnicastSendingMessageHandler

public UnicastSendingMessageHandler(String host,
                                    int port)
Basic constructor; no reliability; no acknowledgment.

Parameters:
host - Destination host.
port - Destination port.

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 Detail

setReliabilityAttributes

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

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean

handleMessage

public void handleMessage(Message<?> message)
                   throws MessageRejectedException,
                          MessageHandlingException,
                          MessageDeliveryException
Description copied from interface: MessageHandler
Handles the message if possible. If the handler cannot deal with the message this will result in a MessageRejectedException e.g. in case of a Selective Consumer. When a consumer tries to handle a message, but fails to do so, a MessageHandlingException is thrown. In the last case it is recommended to treat the message as tainted and go into an error scenario.

When the handling results in a message being sent failure to send that message will result in a MessageDeliveryException.

Specified by:
handleMessage in interface MessageHandler
Parameters:
message - the message to be handled
Throws:
MessageRejectedException - if the handler doesn't select these types of messages
MessageHandlingException - when something went wrong during the handling
MessageDeliveryException - when this handler failed to deliver the reply related to the handling of the message

send

protected void send(DatagramPacket packet)
             throws Exception
Throws:
Exception

getSocket

protected DatagramSocket getSocket()
                            throws IOException
Throws:
IOException

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.


shutDown

public void shutDown()

setSoReceiveBufferSize

public void setSoReceiveBufferSize(int size)
Specified by:
setSoReceiveBufferSize in interface CommonSocketOptions
Overrides:
setSoReceiveBufferSize in class AbstractInternetProtocolSendingMessageHandler
See Also:
Socket.setReceiveBufferSize(int), DatagramSocket.setReceiveBufferSize(int)

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

setTaskExecutor

public void setTaskExecutor(Executor taskExecutor)

Spring Integration

Copyright © 2010. All Rights Reserved.