Database Reference
In-Depth Information
Next, the queue itself is created. The
QueueBuilder
's builder method
always takes four arguments: the Curator client, a
QueueConsumer<T>
object, a
QueueSerializer<T>
object, and a path. In this case, the thread
acts only as a Producer, so the
QueueConsumer
object can be left as
null
.
Then, a
DistributedQueue
object is built and started. Like the client
itself, the queue must be started before it can be used:
queue = QueueBuilder.
builder
(client,
null
,
new
SimpleSerializer<WorkUnit>(),
"/queues/work-unit")
.buildQueue();
queue.start();
Curator does not ship with any built-in
QueueSerializer
implementations. It is left entirely to the application to implement an
appropriate serializer for each queue. In this case, the
WorkUnit
implements the
Serializable
interface to simple Java. Serialization is
used to convert the
WorkUnit
object to and from a byte array. The
SimpleSerializer
implements a serializer that can be used for any class
that implements
Serializable
:
public class
SimpleSerializer<T
extends
Serializable>
implements
QueueSerializer<T> {
public byte
[] serialize(T item) {
ByteArrayOutputStream bos =
new
ByteArrayOutputStream();
byte
[] value =
null
;
try
{
ObjectOutput out =
new
ObjectOutputStream(bos);
out.writeObject(item);
value = bos.toByteArray();
out.close();
}
catch
(IOException e) { }
try
{
bos.close();
}
catch
(IOException e) { }