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. TheQueuePullTransactionregisters 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 implementsAsyncTaskfor 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 protectedMessageProcessor()Creates a new MessageProcessor.protectedMessageProcessor(Selector selector)Creates a new MessageProcessor with a selector.protectedMessageProcessor(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 voidcreateBulkBuffer(int bulkSize)Creates a new bulk buffer in the given sizeMessageEntry[]getBulkBuffer()Returns the bulk bufferlonggetCurrentBulkSize()Returns the current bulkjava.lang.StringgetDescription()Returns a short description of this task.java.lang.StringgetDispatchToken()Returns the dispatch token of the task.longgetMaxBulkSize()Returns the max bulk sizelonggetReceiverId()intgetRegistrationId()Returns the registration id.longgetRegistrationTime()Returns the registration time.SelectorgetSelector()Returns the selector.longgetTimeout()Returns the timeout.java.lang.ObjectgetTransactionId()Returns the transaction id.intgetViewId()Returns the viewId.booleanisAutoCommit()Returns whether the message should be auto committedbooleanisBulkMode()Returns whether this message processor runs in bulk modeabstract voidprocessException(java.lang.Exception exception)Process an exception.abstract voidprocessMessage(MessageEntry messageEntry)Process a message.voidprocessMessages(int numberMessages)Process messages in bulk mode.voidreset()Reset the MessageProcessor to its initial state without the need to recreate it.voidrun()Implements the task logic.voidsetAutoCommit(boolean autoCommit)Sets whether the message should be auto committedprotected voidsetBulkMode(boolean bulkMode)Sets whether this message processor runs in bulk modevoidsetCurrentBulkSize(long currentBulkSize)Sets the current bulk sizevoidsetReceiverId(long receiverId)voidsetRegistrationId(int registrationId)Sets a registration id.voidsetRegistrationTime(long registrationTime)Sets a registration time.protected voidsetTransactionId(java.lang.Object transactionId)Sets the transaction id.voidsetViewId(int viewId)Sets the view id.voidstop()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:AsyncTaskReturns 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 invokingdispatchTaskof the ThreadpoolSwiftlet directly to determine the pool.- Specified by:
getDispatchTokenin interfaceAsyncTask- Returns:
- dispatch token.
-
getDescription
public java.lang.String getDescription()
Description copied from interface:AsyncTaskReturns a short description of this task. Used for trace outputs.- Specified by:
getDescriptionin interfaceAsyncTask- Returns:
- description.
-
stop
public void stop()
Description copied from interface:AsyncTaskStops 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.
-
-