de.javakaffee.simplequeue
Class RichBDBQueue<T>

java.lang.Object
  extended by de.javakaffee.simplequeue.RichBDBQueue<T>

public class RichBDBQueue<T>
extends Object

A wrapper around BDBQueue that provides json serialization and blocking queue semantics. Created on Jun 22, 2011

Author:
Martin Grotzke (initial creation)

Constructor Summary
RichBDBQueue(File queueBaseDir, String queueName, int cacheSize, Class<T> type)
          Constructs the queue using the given file for persistence.
 
Method Summary
 void clear()
           
 void close()
          Close the queue and release the thread processing consume(Consumer) is there is any.
 void consume(Consumer<T> consumer)
          Consume the queue using the given consumer.
protected  org.codehaus.jackson.map.ObjectMapper createObjectMapper()
          Create the ObjectMapper used for serializing.
 long getPauseTimeInMillis()
          The time in millis that consume(Consumer) goes to sleep when the queue is empty before checking again.
 boolean isEmpty()
           
 T peek()
           
 void push(T object)
          Serializes the given object and adds the serialized form to the queue file.
 void remove()
           
 void setPauseTimeInMillis(long pauseTimeInMillis)
          Sets the time in millis that consume(Consumer) goes to sleep when the queue is empty before checking again.
 long size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RichBDBQueue

public RichBDBQueue(File queueBaseDir,
                    String queueName,
                    int cacheSize,
                    Class<T> type)
             throws IOException
Constructs the queue using the given file for persistence.

Parameters:
queueBaseDir - queue database environment directory path
queueName - descriptive queue name
cacheSize - how often to sync the queue to disk
Throws:
IOException - thrown when the given file does not exist or is not writeable.
Method Detail

createObjectMapper

protected org.codehaus.jackson.map.ObjectMapper createObjectMapper()
Create the ObjectMapper used for serializing.

Returns:
the configured ObjectMapper.

push

public void push(T object)
          throws IOException
Serializes the given object and adds the serialized form to the queue file.

Parameters:
object - the object to add.
Throws:
IOException
See Also:
BDBQueue.push(byte[])

consume

public void consume(Consumer<T> consumer)
             throws IOException,
                    InterruptedException
Consume the queue using the given consumer. This method will run forever and feed the consumer with new elements read from the queue. An element is removed from the queue after it was consumed successfully (by Consumer.consume(Object)). If the consumer fails consuming it, the element will try again. Therefore, if you can't process the element but want that it's removed, just handle your exception accordingly and don't throw an exception.

Parameters:
consumer - the consumer used for handling queue elements.
Throws:
IOException
InterruptedException

clear

public void clear()

close

public void close()
Close the queue and release the thread processing consume(Consumer) is there is any.


isEmpty

public boolean isEmpty()

peek

public T peek()
       throws IOException
Throws:
IOException

remove

public void remove()

size

public long size()

getPauseTimeInMillis

public long getPauseTimeInMillis()
The time in millis that consume(Consumer) goes to sleep when the queue is empty before checking again.

Returns:
the pauseTimeInMillis

setPauseTimeInMillis

public void setPauseTimeInMillis(long pauseTimeInMillis)
Sets the time in millis that consume(Consumer) goes to sleep when the queue is empty before checking again.

Parameters:
pauseTimeInMillis - the pauseTimeInMillis to set


Copyright © 2010-2011. All Rights Reserved.