tags:

views:

144

answers:

4

Hi Im developing the java Email application with Timer, I have a two arraylists named ActiveProcesses,InActiveProcesses. If I start the Timer it will send the Email with InActiveProcesses list values for every seconds. But the problem is the timer is sent the Email if the InActiveProcesses list values is same.For Example the InActiveProcess list contains the value abcd, it will send Email every seconds with same List values. I want to send the Email only the InActiveProcesses list contains different values. The Timer will check the values every seconds if the values are different it will send the Email. How to handle this problem using java. Thanks in advance. Here is the code,

for (int i = 0; i < InActiveProcess.size(); i++)
{
    if (!ActiveProcess.contains(InActiveProcess.get(i)))
    {
        list3.add(InActiveProcess.get(i));
    }
}
for (int i = 0; i < ActiveProcess.size(); i++)
{
    if (!InActiveProcess.contains(ActiveProcess.get(i)))
    {
        list3.add(ActiveProcess.get(i));
    }
}
log.info("Processes which are Not Running: " + list3);

StringBuilder sb = new StringBuilder();

for (int k = 0; k < list3.size(); k++)
{

    Result = list3.get(k);

    sb.append(Result.toString());

    sb.append(" ");

}

String message = sb.toString();

log.info(message);

sms.SendMessage("1254554555", message);

es.SendMail("[email protected]", " Server process is down", message);

This is my Timer class.

int delay = 5000; // delay for 5 sec.

int interval = 1000; // iterate every sec.

Timer timer = new Timer();

        timer.scheduleAtFixedRate(new sample() {

        }, delay, interval);

The Timer is Execute the sample() class for Every seconds and sent Email to specified Address. I want to handle the Timer will Execute for every second at the same time, the Email is sent es.SendMail("[email protected]", " Server process is down", message); if message contains values is different.

+2  A: 

Update: The question changed after posting this answer. This is the answer to the original question:

You can use a Timer class.

You can schedule actions to be taken when the timer is off through a TimerTask. This class is abstract so you have to extend it and implement the method run().

class MyTimerTask extends TimerTask {

   @Override
   public void run(){
   // Task to do here
 }
}

And then in your main class

MyTimerTask task = new MyTimerTask(); // this class implements WHAT to do inside the method run();
Date date = new Date(...) // This class will tell timer WHEN to do the task
Timer timer = new Timer(task,date); //Good to go!

Alternatively you can do Timer timer = new Timer(task,delay) to have the task executed after delay miliseconds

pakore
Why the down vote?
pakore
You answered a different (and unasked) question.
Noel Ang
No I did not. The user edited his question adding my changes after I replied. At the begining he was asking how to implement a timer in Java to do what he wanted.
pakore
A: 

Take a look at java.util.Timer or SheduledExecutorServices

Redlab
A: 

It might be slightly unrelated to your question but I couldn't help mentioning that you do pretty much the same thing twice, in two for loops. If I understand correctly you want to make sure there are no duplicates (as in an entry that exists in both ActiveProcesses and Inactive Processes). If that's the case why not check for duplicates when you add an entry to the list?

As for the timer, I'll have to agree with the previous answers: java.util.Timer is most likely what you need. Start off there :)

Good luck

EDIT: for future reference, please try to specify what your "issue" is on the subject instead

posdef
Instead of checking for duplicates just perform lista.removeAll(listb); listb.removeAll(lista);
Syntax
+1  A: 

My assumptions:

  • list3 refers to an instance of java.util.List.
  • You can modify the Result class.

First, override Object.equals method in your Result class.

Then:

...

log.info("Processes which are Not Running: " + list3);
if (!list3.equals(this.previousList)) {
    // update history for next time
    this.previousList.clear();
    this.previousList.addAll(list3);

    // Prepare and send email
    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < list3.size(); k++)
    {
        Result = list3.get(k);
        sb.append(Result.toString());
        sb.append(" ");
        String message = sb.toString();
        log.info(message);
        sms.SendMessage("1254554555", message);
        es.SendMail("[email protected]", " Server process is down", sb);
    }
}

Is the order of the processes in the list important? For example, are lists "a-b-c-d" and "b-a-d-c" different? If the answer is no, then java.util.List.equals won't meet your needs. In that case, don't use lists, but sets.


EDIT:

If the list is guaranteed to be consistently ordered (if 'a-b-c-d' can never be 'b-a-d-c'), it might be possible (and cheaper) to build, save and compare the string created from StringBuilder, instead of building, saving, and comparing the list. In this way you would not have to implement Result.equals and Result.hashCode either.

Noel Ang