Solved

Java Delay/Timer Question

Posted on 2001-09-06
4
203 Views
Last Modified: 2013-11-24
Hi,

I'm trying to do something with a java servlet
involving a time dealy ...

Basically what happens is i get an alert over the
network every time theres a change on a server
and my code automatically mails off this alert
to the relavant people, - problem the alerts are
far too frequent - 2 a minute type thing

Here my basic algorithm

When an alert is received

Start a timer,

Any alert you get inbetween,

Check if the exact same alert has been
seen already
if not
        break the timer loop
        and send the mail
if so
        add it to an array of alerts
        and wait until timer is finished

reset timer

end timer loop        

So in effect instead of getting a mail
every 2 minutes about the same problem
the program would would it for a half hour
before mailing it out again ...

Would it be useful to run this as a separate
thread ?

Is there an api in java for these type of problems ?

Any help with this is greatly appreciated ..
0
Comment
Question by:michaelgoc
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 6461007
I don't believe there is any specific help for such things. And no, I don't think a separate timer thread is needed here either. A class with a method that is called for every alert is quite sufficient, based on your description. In pseudo code it would be something like:


class AlertFilter
{   long time;
    AlertList alerts;

    AlertFilter()
    {   time=System.currentTimeMillis();
        alerts=new AlertList();
        return;
    }

    void processAlert(Alert a)
    {   checkTime();
        if(!alerts.has(a))
        {   Send Alert
            alerts.add(a);
        }
        return;
    }

    void checkTime()
    {   if(System.currentTimeMillis()-time>30000)
        {   time=System.currentTimeMillis();
            alerts.reset();
        }
        return;
    }
}


Well, that came out as more real code than pseudo. The details of AlertList and Alert should be easy to imagine.
0
 
LVL 92

Expert Comment

by:objects
ID: 6462493
Have a look at the Timer and TimerTask classes.
They should provide the functionality you are looking for.
0
 
LVL 16

Expert Comment

by:imladris
ID: 6464404
Was either answer helpful?

If so, you should now select one, and grade it.

If not, perhaps you could ask a followup question.
0
 

Author Comment

by:michaelgoc
ID: 6470317
The basic algorithm was sound
but the check time method was incorrect,
I have written my own timer class and checked
the time elapsed etc with this, ( seems to be working )
Thanks ,

Michael
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
use lov values 2 64
runtime exception 2 42
ejb mdb examples 1 10
Eclipse Java import and method not resolved 4 52
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question