Solved

Java Delay/Timer Question

Posted on 2001-09-06
4
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

626 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