Solved

Java Delay/Timer Question

Posted on 2001-09-06
4
205 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 67
jmeter usage 4 67
DTD and JAVA versions 1 59
Java Inharitance subclass/child extends, reference variable from base/parent class 9 72
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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