Package com.swiftmq.swiftlet.queue
Class MessageProcessor
- java.lang.Object
-
- com.swiftmq.swiftlet.queue.MessageProcessor
-
- All Implemented Interfaces:
AsyncTask
,java.lang.Runnable
public abstract class MessageProcessor extends java.lang.Object implements AsyncTask
Abstract MessageProcessor to register at aQueuePullTransaction
. TheQueuePullTransaction
registers it at the queue within the transaction context. The MessageProcessor will be invoked when a message is available in the queue and if it does match the selector, if specified.
The MessageProcessor can be used in bulk mode. That is, thesetBulkMode(true)
must be called and the bulk buffer has to be created withceateBulkBuffer(size)
. In bulk mode, the methodprocessMessages(n)
is called instead ofprocessMessage(messageEntry)
. The size of the bulk can be limited by overwritin methodgetMaxBulkSize()
which returns either -1 (unlimited) or the max size in bytes. The Queue Manager willsetCurrentBulkSize(n)
zu the size of all messages in the current bulk.
MessageProcessor implementsAsyncTask
for convenience. It is highly recommended to implement the processing of the message inside therun()
method and to dispatch this task in a thread pool withinprocessMessage()
to avoid blocking of other threads.- Author:
- IIT GmbH, Bremen/Germany, Copyright (c) 2000-2002, All Rights Reserved
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MessageProcessor()
Creates a new MessageProcessor.protected
MessageProcessor(Selector selector)
Creates a new MessageProcessor with a selector.protected
MessageProcessor(Selector selector, long timeout)
Creates a new MessageProcessor with a selector and timeout.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
createBulkBuffer(int bulkSize)
Creates a new bulk buffer in the given sizeMessageEntry[]
getBulkBuffer()
Returns the bulk bufferlong
getCurrentBulkSize()
Returns the current bulkjava.lang.String
getDescription()
Returns a short description of this task.java.lang.String
getDispatchToken()
Returns the dispatch token of the task.long
getMaxBulkSize()
Returns the max bulk sizelong
getReceiverId()
int
getRegistrationId()
Returns the registration id.long
getRegistrationTime()
Returns the registration time.Selector
getSelector()
Returns the selector.long
getTimeout()
Returns the timeout.java.lang.Object
getTransactionId()
Returns the transaction id.int
getViewId()
Returns the viewId.boolean
isAutoCommit()
Returns whether the message should be auto committedboolean
isBulkMode()
Returns whether this message processor runs in bulk modeabstract void
processException(java.lang.Exception exception)
Process an exception.abstract void
processMessage(MessageEntry messageEntry)
Process a message.void
processMessages(int numberMessages)
Process messages in bulk mode.void
reset()
Reset the MessageProcessor to its initial state without the need to recreate it.void
run()
Implements the task logic.void
setAutoCommit(boolean autoCommit)
Sets whether the message should be auto committedprotected void
setBulkMode(boolean bulkMode)
Sets whether this message processor runs in bulk modevoid
setCurrentBulkSize(long currentBulkSize)
Sets the current bulk sizevoid
setReceiverId(long receiverId)
void
setRegistrationId(int registrationId)
Sets a registration id.void
setRegistrationTime(long registrationTime)
Sets a registration time.protected void
setTransactionId(java.lang.Object transactionId)
Sets the transaction id.void
setViewId(int viewId)
Sets the view id.void
stop()
Stops this task.
-
-
-
Constructor Detail
-
MessageProcessor
protected MessageProcessor()
Creates a new MessageProcessor.
-
MessageProcessor
protected MessageProcessor(Selector selector)
Creates a new MessageProcessor with a selector.- Parameters:
selector
- selector.
-
MessageProcessor
protected MessageProcessor(Selector selector, long timeout)
Creates a new MessageProcessor with a selector and timeout. If a timeout occurs,processException()
is called and the exception will be aQueueTimeoutException
- Parameters:
selector
- selector.timeout
- timeout (0 = no timeout).- See Also:
QueueTimeoutException
-
-
Method Detail
-
getReceiverId
public long getReceiverId()
-
setReceiverId
public void setReceiverId(long receiverId)
-
getTransactionId
public java.lang.Object getTransactionId()
Returns the transaction id.- Returns:
- transaction id.
-
setTransactionId
protected void setTransactionId(java.lang.Object transactionId)
Sets the transaction id. Called from theQueuePullTransaction
- Parameters:
transactionId
- transaction id.
-
getRegistrationTime
public long getRegistrationTime()
Returns the registration time.- Returns:
- registration time.
-
setRegistrationTime
public void setRegistrationTime(long registrationTime)
Sets a registration time. Internal use for timeout processing.- Parameters:
registrationTime
- registration time.
-
getRegistrationId
public int getRegistrationId()
Returns the registration id.- Returns:
- registration id.
-
setRegistrationId
public void setRegistrationId(int registrationId)
Sets a registration id. Internal use.- Parameters:
registrationId
- registration id.
-
getSelector
public Selector getSelector()
Returns the selector.- Returns:
- selector.
-
getViewId
public int getViewId()
Returns the viewId.- Returns:
- view Id.
-
setViewId
public void setViewId(int viewId)
Sets the view id. Internal use.- Parameters:
viewId
- registration time.
-
getTimeout
public long getTimeout()
Returns the timeout.- Returns:
- timeout.
-
isAutoCommit
public boolean isAutoCommit()
Returns whether the message should be auto committed- Returns:
- auto commit.
-
setAutoCommit
public void setAutoCommit(boolean autoCommit)
Sets whether the message should be auto committed- Parameters:
autoCommit
- auto commit.
-
isBulkMode
public boolean isBulkMode()
Returns whether this message processor runs in bulk mode- Returns:
- bulk mode
-
setBulkMode
protected void setBulkMode(boolean bulkMode)
Sets whether this message processor runs in bulk mode- Parameters:
bulkMode
- bulk mode
-
createBulkBuffer
protected void createBulkBuffer(int bulkSize)
Creates a new bulk buffer in the given size- Parameters:
bulkSize
- bulk size
-
getBulkBuffer
public MessageEntry[] getBulkBuffer()
Returns the bulk buffer- Returns:
- bulk buffer
-
getMaxBulkSize
public long getMaxBulkSize()
Returns the max bulk size- Returns:
- max bulk size
-
getCurrentBulkSize
public long getCurrentBulkSize()
Returns the current bulk- Returns:
- current bulk size
-
setCurrentBulkSize
public void setCurrentBulkSize(long currentBulkSize)
Sets the current bulk size- Parameters:
currentBulkSize
- current bulk size
-
processMessages
public void processMessages(int numberMessages)
Process messages in bulk mode. Called when messages are available and the selector matches, if specified. The queue first obtains the bulk buffer withgetBulkBuffer()
and fills it with message entries up to the buffer size. It then calls this method and passes the actual number of messages in the buffer. Use this method to store the message and dispatch the MessageProcessor to a thread pool. Implement the processing itself inside therun()
method.- Parameters:
numberMessages
- number Messages in bulk buffer.
-
processMessage
public abstract void processMessage(MessageEntry messageEntry)
Process a message. Called when a message is available and the selector matches, if specified. Use this method to store the message and dispatch the MessageProcessor to a thread pool. Implement the processing itself inside therun()
method.- Parameters:
messageEntry
- message entry.
-
processException
public abstract void processException(java.lang.Exception exception)
Process an exception. A timeout is indicated by aQueueTimeoutException
.- Parameters:
exception
- exception.- See Also:
QueueTimeoutException
-
getDispatchToken
public java.lang.String getDispatchToken()
Description copied from interface:AsyncTask
Returns the dispatch token of the task. The dispatch token is the thread name, used for thread assignment in the router's configuration file. It is used to determine the pool by invokingdispatchTask
of the ThreadpoolSwiftlet directly to determine the pool.- Specified by:
getDispatchToken
in interfaceAsyncTask
- Returns:
- dispatch token.
-
getDescription
public java.lang.String getDescription()
Description copied from interface:AsyncTask
Returns a short description of this task. Used for trace outputs.- Specified by:
getDescription
in interfaceAsyncTask
- Returns:
- description.
-
stop
public void stop()
Description copied from interface:AsyncTask
Stops this task. This method doesn't stop the task's execution. It is implementation dependent in which state the task will turn. In most cases, the task does some clean up and returns 'false' onisValid()
to avoid execution.
-
reset
public void reset()
Reset the MessageProcessor to its initial state without the need to recreate it.
-
-