Top Description Inners Fields Constructors Methods
org.springframework.messaging.simp.broker

public Class OrderedMessageChannelDecorator

extends Object
implements MessageChannel
Class Inheritance
All Implemented Interfaces
org.springframework.messaging.MessageChannel
Imports
java.util.Queue, java.util.concurrent.ConcurrentLinkedQueue, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, org.apache.commons.logging.Log, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageHandler, org.springframework.messaging.simp.SimpMessageHeaderAccessor, org.springframework.messaging.support.ExecutorChannelInterceptor, .ExecutorSubscribableChannel, .MessageHeaderAccessor, org.springframework.util.Assert

Decorator for an ExecutorSubscribableChannel that ensures messages are processed in the order they were published to the channel. Messages are sent one at a time with the next one released when the previous has been processed. This decorator is intended to be applied per session.
Author
Rossen Stoyanchev
Since
5.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
OrderedMessageChannelDecorator.PostHandleTask

Remove handled message from queue, and send next message.

Field Summary

Modifier and TypeField and Description
private final MessageChannel
private final Log
private final Queue<Message<?>>
private static final String
private final AtomicBoolean
private final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
configureInterceptor(MessageChannel
the channel to configure
channel
,
boolean
whether preserve the order or publication; when "true" an interceptor is inserted, when "false" it removed.
preserveOrder
)

Install or remove an ExecutorChannelInterceptor that invokes a completion task, if found in the headers of the message.

public static Runnable
getNextMessageTask(Message<?> message)

Obtain the task to release the next message, if found.

private boolean
removeMessage(Message<?> message)

Remove the message from the top of the queue, but only if it matches, i.e. hasn't been removed already.

public boolean
send(Message<?>
the message to send
message
)

Overrides default org.springframework.messaging.MessageChannel.send.

Send a Message to this channel.

public boolean
send(Message<?>
the message to send
message
,
long
the timeout in milliseconds or INDEFINITE_TIMEOUT
timeout
)

Implements org.springframework.messaging.MessageChannel.send.

Send a message, blocking until either the message is accepted or the specified timeout period elapses.

private void
private static void
setTaskHeader(Message<?> message, Runnable task)

public static boolean
supportsOrderedMessages(MessageChannel channel)

Whether the channel has been configured with an interceptor for sequential handling.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

channelback to summary
private final MessageChannel channel
loggerback to summary
private final Log logger
messagesback to summary
private final Queue<Message<?>> messages
NEXT_MESSAGE_TASK_HEADERback to summary
private static final String NEXT_MESSAGE_TASK_HEADER
sendInProgressback to summary
private final AtomicBoolean sendInProgress
subscriberCountback to summary
private final int subscriberCount

Constructor Detail

OrderedMessageChannelDecoratorback to summary
public OrderedMessageChannelDecorator(MessageChannel channel, Log logger)

Method Detail

configureInterceptorback to summary
public static void configureInterceptor(MessageChannel channel, boolean preserveOrder)

Install or remove an ExecutorChannelInterceptor that invokes a completion task, if found in the headers of the message.

Parameters
channel:MessageChannel

the channel to configure

preserveOrder:boolean

whether preserve the order or publication; when "true" an interceptor is inserted, when "false" it removed.

getNextMessageTaskback to summary
public static Runnable getNextMessageTask(Message<?> message)

Obtain the task to release the next message, if found.

Annotations
@Nullable
removeMessageback to summary
private boolean removeMessage(Message<?> message)

Remove the message from the top of the queue, but only if it matches, i.e. hasn't been removed already.

sendback to summary
public boolean send(Message<?> message)

Overrides default org.springframework.messaging.MessageChannel.send.

Doc from org.springframework.messaging.MessageChannel.send.

Send a Message to this channel. If the message is sent successfully, the method returns true. If the message cannot be sent due to a non-fatal reason, the method returns false. The method may also throw a RuntimeException in case of non-recoverable errors.

This method may block indefinitely, depending on the implementation. To provide a maximum wait time, use send(Message, long).

Parameters
message:Message<?>

the message to send

Returns:boolean

whether the message was sent

Annotations
@Override
sendback to summary
public boolean send(Message<?> message, long timeout)

Implements org.springframework.messaging.MessageChannel.send.

Doc from org.springframework.messaging.MessageChannel.send.

Send a message, blocking until either the message is accepted or the specified timeout period elapses.

Parameters
message:Message<?>

the message to send

timeout:long

the timeout in milliseconds or INDEFINITE_TIMEOUT

Returns:boolean

true if the message is sent, false if not including a timeout of an interrupt of the send

Annotations
@Override
sendNextMessageback to summary
private void sendNextMessage()
setTaskHeaderback to summary
private static void setTaskHeader(Message<?> message, Runnable task)
supportsOrderedMessagesback to summary
public static boolean supportsOrderedMessages(MessageChannel channel)

Whether the channel has been configured with an interceptor for sequential handling.

Since
6.1
trySendback to summary
private void trySend()
org.springframework.messaging.simp.broker back to summary

private final Class OrderedMessageChannelDecorator.CallbackTaskInterceptor

extends Object
implements ExecutorChannelInterceptor
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.OrderedMessageChannelDecorator.CallbackTaskInterceptor
All Implemented Interfaces
org.springframework.messaging.support.ExecutorChannelInterceptor, org.springframework.messaging.support.ChannelInterceptor

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
afterMessageHandled(Message<?>
the message handled
message
,
MessageChannel
the channel on which the message was sent to
ch
,
MessageHandler
the target handler that handled the message
handler
,
Exception
any exception that may have been raised by the handler
ex
)

Overrides default org.springframework.messaging.support.ExecutorChannelInterceptor.afterMessageHandled.

Invoked inside the Runnable submitted to the Executor after calling the target MessageHandler regardless of the outcome (i.e. Exception raised or not) thus allowing for proper resource cleanup.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CallbackTaskInterceptorback to summary
private CallbackTaskInterceptor()

Method Detail

afterMessageHandledback to summary
public void afterMessageHandled(Message<?> message, MessageChannel ch, MessageHandler handler, Exception ex)

Overrides default org.springframework.messaging.support.ExecutorChannelInterceptor.afterMessageHandled.

Doc from org.springframework.messaging.support.ExecutorChannelInterceptor.afterMessageHandled.

Invoked inside the Runnable submitted to the Executor after calling the target MessageHandler regardless of the outcome (i.e. Exception raised or not) thus allowing for proper resource cleanup.

Note that this will be invoked only if beforeHandle successfully completed and returned a Message, i.e. it did not return null.

Parameters
message:Message<?>

the message handled

ch:MessageChannel

the channel on which the message was sent to

handler:MessageHandler

the target handler that handled the message

ex:Exception

any exception that may have been raised by the handler

Annotations
@Override
org.springframework.messaging.simp.broker back to summary

private final Class OrderedMessageChannelDecorator.PostHandleTask

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.OrderedMessageChannelDecorator.PostHandleTask
All Implemented Interfaces
java.lang.Runnable

Remove handled message from queue, and send next message.

Field Summary

Modifier and TypeField and Description
private final AtomicInteger
private final Message<?>

Constructor Summary

AccessConstructor and Description
private
PostHandleTask(Message<?> message)

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

handledCountback to summary
private final AtomicInteger handledCount
Annotations
@Nullable
messageback to summary
private final Message<?> message

Constructor Detail

PostHandleTaskback to summary
private PostHandleTask(Message<?> message)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override