LOST messages

I have 2 queues (A and B) which would hold incoming messages, to save time I would only write these messages when the queue is at the capacity of 1000. When queue A is full, then the messages would be store in queue B and the messages in queue A would get flush out.

if ((queueA.size() < 1000) && (queueB.size() == 0)

queueA.add(message)

else if ((queueA.size() == 0) && (queueB.size() < 1000))

queueB.add(message)

else if (queueA.size >= 1000)

queueB.add(message)

flush(queueA)

else if (queueB.size() >= 1000)

queueA.add(message)

flush(queueB)

The problem occur when there is 1000 messages store in the queue and no other incoming message, then these 1000 messages would be lost. Can anybody help?

[801 byte] By [mpanga] at [2007-10-3 3:56:32]
# 1
Flush the queue after a specific time has elapsed?
-Kayaman-a at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 2
I thought about it but it seems there would still be left over messages in the queue.
mpanga at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 3
> I thought about it but it seems there would still be> left over messages in the queue.Why?Btw. I do also think that your original code is a bit complex. Why not just swap the queues when one is full?Kaj
kajbja at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 4

originally it was only a simple if....else....

if (queueA is full)

write to queueB

else

write to queueA

but since I would flush out everything in queueA when it's full, the next incoming message would immediately start to write to queueA again. Since i want to perserve the order, I would want the incoming messages continue to store in queueB until it's full.

As for the left over, I would perform some stat calculation at a certain time (say 00:00), if at 00:00, the timer doesn't go off, there would still be X number of messages in there, wouldn't it?

mpanga at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 5

This is what I meant.

//List activeQueue;

//List passiveQueue;

if (isFull(activeQueue)) {

List tmp = activeQueue;

activeQueue = passiveQueue;

passiveQueue = tmp;

flush(passiveQueue);

}

activeQueue.add(message);

Kaj

kajbja at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 6
> As for the left over, I would perform some stat> calculation at a certain time (say 00:00), if at> 00:00, the timer doesn't go off, there would still be> X number of messages in there, wouldn't it?Why wouldn't the timer go off?
kajbja at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 7
given that the program supports concurrency , there would be problem with the suggested approach?
mpanga at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 8
> given that the program supports concurrency , there> would be problem with the suggested approach?No
kajbja at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 9
Why not just have one queue and flush it when it gets to 1000 entries?and you also need to consider what happens if you never get to 1000 of course.
ejpa at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 10

according to the codes given by kajbj, what if there are more than 1 messages arrive when activeQueue 1 message from being full? I don't get how the following codes would work for concurrency, and exactly when messages would start to write to the passiveQueue? Thanks

//List activeQueue;

//List passiveQueue;

if (isFull(activeQueue)) {

List tmp = activeQueue;

activeQueue = passiveQueue;

passiveQueue = tmp;

flush(passiveQueue);

}

activeQueue.add(message);

mpanga at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...
# 11

> according to the codes given by kajbj, what if there

> are more than 1 messages arrive when activeQueue 1

> message from being full? I don't get how the

> following codes would work for concurrency, and

> exactly when messages would start to write to the

> passiveQueue? Thanks

You just need to synchronize the code.

Kaj

kajbja at 2007-7-14 21:54:45 > top of Java-index,Java Essentials,Java Programming...