tags:

views:

43

answers:

2

Hi,

I'm trying to write a message acknowledgement application where I need to: a) Add every new message on a message queue. Just using an Arraylist for creating a message queue. b) Notify a timertask to expect an acknowledge in 50 sec for the message so make it to either sleep for 50sec or wake up when an acknowledgement is received.

What is the best practice to implement this?

Thanks

A: 

i thought u wanted to have some mechanism, where updation needs after particular intervals, you can use the thread and set a sleep as per interval requirement like :

public void run() {

    while (Start == true) {
        getMessage();      //yourmethod();
        try {
            Thread.sleep(50);
        } catch (InterruptedException ie) {
            stop();
        }
    }
}
Static Void Main
+1  A: 

I'm not quite clear what your needs are. What does this have to do with Swing or timers? What kind of threading are you dealing with here? I'll make some assumptions and suggest a couple things.

It sounds like you want to put a message in a queue, then wait until a response is received, or 50s max. You should check out BlockingQueue. It is thread-safe, and you can wait for a specific amount of time for another thread to put something in it. This seems like it could be useful for message/acknowledge problem.

BlockingQueue<MSG> queue = new LinkedBlockingQueue<MSG>();

// put a message in the queue
queue.put( msg );

// have a thread wait on the queue until something is available in it
MSG msg = queue.poll( 50, TimeUnit.SECONDS );

I need more details on your problem for more specific help.

wolfcastle
@Wolfcastle: This has been really helpful.
Global Dictator
Basically we have a timeout of 50 secs for receiving an acknowledgement to confirm successfull delivery of any message. So, we could receive an acknowledgement at any stage upto 50 sec ie could be just 2 secs after the message was sent. Hence i just wanted a timer task that sleeps for 50 secs and wakes up just before 50 secs or when an acknowledgement is received. But If we dont receive an acknowledgement back within 50 secs, we update the status of that msg as failure.
Global Dictator
Also, there could be multiple msgs that were being sent. Hence when we receive an acknowledgment, it needs to be checked which msg is it for before updating the status of that particular msg as successful. I hope that makes sense.. The part to design the TimerTask and how to make it to wake up before its sleep time is completed ie upon an event like when a message comes in, is what I need help with. I was just wondering wheather it was possible to use Swing.Timer or util.Timer class to achieve the same. Could you please provide the code using them
Global Dictator
Unless you are updating UI components after every message acknowledge, I doubt you want to use swing timers. java.util.Timer is better, but a [ScheduledExecutorService](http://download-llnw.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html) would probably be even better.
wolfcastle
I think you have two different approaches here, depending on the volumn of messages you are dealing with. For a small amount of messages, you could schedule a timeout task for every received message that goes off in 50s. If you receive the ack sooner, you just cancel the timeout task. If you have large numbers of messages, you might simply have 1 task that goes off every Xms that checks all outstanding messages and times out ones that have been waiting for an acknowledgement longer than 50s. http://download.oracle.com/javase/tutorial/essential/concurrency/executors.html may be of use.
wolfcastle
@wolfcastle, this is exactly my line of thought. But could you provide some sample code to implement this pls..
Global Dictator
We are mt expectign to many msgs hence the first approach is realy what I'm looking for..
Global Dictator
We are not expecting too many msgs hence the first approach is realy what I'm looking for.. Any sample code woudl be really helpful
Global Dictator