Class MessageProcessor

java.lang.Object
com.swiftmq.swiftlet.queue.MessageProcessor
All Implemented Interfaces:
AsyncTask, Runnable

public abstract class MessageProcessor extends Object implements AsyncTask
Abstract MessageProcessor to register at a QueuePullTransaction. The QueuePullTransaction 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, the setBulkMode(true) must be called and the bulk buffer has to be created with ceateBulkBuffer(size). In bulk mode, the method processMessages(n) is called instead of processMessage(messageEntry). The size of the bulk can be limited by overwritin method getMaxBulkSize() which returns either -1 (unlimited) or the max size in bytes. The Queue Manager will setCurrentBulkSize(n) zu the size of all messages in the current bulk.

MessageProcessor implements AsyncTask for convenience. It is highly recommended to implement the processing of the message inside the run() method and to dispatch this task in a thread pool within processMessage() to avoid blocking of other threads.
Author:
IIT GmbH, Bremen/Germany, Copyright (c) 2000-2002, All Rights Reserved
  • Constructor Details

    • 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 a QueueTimeoutException
      Parameters:
      selector - selector.
      timeout - timeout (0 = no timeout).
      See Also:
  • Method Details

    • getReceiverId

      public long getReceiverId()
    • setReceiverId

      public void setReceiverId(long receiverId)
    • getTransactionId

      public Object getTransactionId()
      Returns the transaction id.
      Returns:
      transaction id.
    • setTransactionId

      protected void setTransactionId(Object transactionId)
      Sets the transaction id. Called from the QueuePullTransaction
      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 with getBulkBuffer() 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 the run() 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 the run() method.
      Parameters:
      messageEntry - message entry.
    • processException

      public abstract void processException(Exception exception)
      Process an exception. A timeout is indicated by a QueueTimeoutException.
      Parameters:
      exception - exception.
      See Also:
    • getDispatchToken

      public 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 invoking dispatchTask of the ThreadpoolSwiftlet directly to determine the pool.
      Specified by:
      getDispatchToken in interface AsyncTask
      Returns:
      dispatch token.
    • getDescription

      public String getDescription()
      Description copied from interface: AsyncTask
      Returns a short description of this task. Used for trace outputs.
      Specified by:
      getDescription in interface AsyncTask
      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' on isValid() to avoid execution.
      Specified by:
      stop in interface AsyncTask
    • reset

      public void reset()
      Reset the MessageProcessor to its initial state without the need to recreate it.
    • run

      public void run()
      Description copied from interface: AsyncTask
      Implements the task logic. This method will be called when a task is executed from a pool thread.
      Specified by:
      run in interface AsyncTask
      Specified by:
      run in interface Runnable