JDBQueue

Java Persistence Queue

Introduction

The basic idea of JDBQueue is to have a persistence Queue. Then, if the program need to restart, the queue is restored. We use a database because serialize a Queue objet is not faster, for each insertion.

This messages in the queue has 2 states:

There is a third-party state, that it is when the message are handled and it is completed. The work do completeTask and the message are removed from the DBQueue (persistence queue).

http://luisbastiao.files.wordpress.com/2012/09/arch.png

Download

You can download the source code from github and compile by yourself with Maven 3.

Or on the other hand, you can use a compiled version: https://dl.dropbox.com/u/2859837/JDBQueue-v0.1.zip

It contains all bindings to work with sqlite4java. You should choose the one for your platform, for instance, sqlite4java-win32-x86.dll.

Example 1

In order to handle the message an implementation of ITask should be provided. Moreover, to poll the messages automatically, a JDBWorker need to be created.

DBQueue q = new DBQueue("queue.db");
JDBWorker worker = new JDBWorker(q,new TestTask());
worker.start();

q.add("Test");
q.add("Test2");
q.poll();
q.take();

worker.join();

Example 2

class TestTask implements ITask
{
    private DBQueue queue; 
    public TestTask(DBQueue queue )
    {
        this.queue = queue;
    }
    public void handlerMessage(MessageObj message) {

        System.out.println(message);
        queue.completedTask(message.getId());

    }

}
public void workerTest()
{

    DBQueue q = new DBQueue("queue.db");


    JDBWorker worker = new JDBWorker(q,new TestTask(q), 500);
    worker.start();

    for (int i = 0 ; i<10 ; i++)
    {
        q.add("je1");
        q.add("je2");
        q.add("je3");
        System.out.println("Size: " + q.size());
        System.out.println("Size of pending " + q.sizePending());
        System.out.println("Size of progress " +q.sizeProgress());
        q.add("je4");
        q.add("je5");

    }
    while(q.size()>0)
    {
         System.out.println("Size: " + q.size());
        System.out.println("Size of pending " + q.sizePending());
        System.out.println("Size of progress " +q.sizeProgress());
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(WorkerTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    System.out.println("Closing? " + q.size());
    worker.close();
    try {
        worker.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(WorkerTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

Dependence

In order to grant the persistence, JDBQueue uses sqlite4java.

Notes

This work is still working in progress and used by a project. Generalization might be necessary to other cases. Feel free to contribute back to it.

Author

Luís A. Bastião Silva luis.kop@gmail.com

Licence

Copyright 2012 - Luís A. Bastião Silva

JDBQueue is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

JDBQueue is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with JDBQueue. If not, see http://www.gnu.org/licenses/.