Serializing BlockingQueue / ArrayBlockingQueue in java

I need to "serialize" a large BlockingQueue (sizeof 10_000) that contains the domanis names (strings (objects)) for my crawler - for example, if I stop it or it happens by accident - this will help not to scan it has already been done. What is the best way to do this? In NET, I used binary serialization for such tasks (for example, protobuf, for example) (faster and serialized visualization of information is not so important for understanding - not necessarily an XML representation). But how to do it in java? Maybe you are referring to a sample?

+4
source share
2 answers

BlockingQueueis just an interface, not a specific type. You are serializing / deserializing instances that have specific types. It depends on whether the dynamic type of the instace object exists Serializable.

ArrayBlockingQueueimplements Serializable, which means you can just serialize it and deserialize it with ObjectInputStream/ ObjectOutputStream:

Saving ArrayBlockingQueue:

ArrayBlockingQueue queue = new ArrayBlockingQueue(10);

try (ObjectOutputStream out = new ObjectOutputStream(
        new FileOutputStream("queue.data"))) {
    out.writeObject(queue);
}

Reading saved ArrayBlockingQueue:

ArrayBlockingQueue queue = null;

try (ObjectInputStream in = new ObjectInputStream (
        new FileInputStream("queue.data"))) {
    queue = in.readObject()
}
+2
source

The Java BlockingQueue interface is implemented by some classes, such as ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, and others.

The following are important ways to block queues.

E take () - consumes an element from the queue, waits if the queue is empty, until the manufacturer does something in the queue.

put (E) - , , , .

@oracle docs.

0

All Articles