How to solve this problem with timer in java?

I am developing a java email application using Timer, I have two arraylists named ActiveProcesses, InActiveProcesses. If I run the timer, it will send an email with InActiveProcesses list values ​​for every second. But the problem is that the timer is sent by email if the values ​​of the InActiveProcesses list are the same. For example, the InActiveProcess list contains the value abcd , it will send an email every second with the same List values. I want to send an email, only the InActiveProcesses list contains different values. The timer checks the values ​​every second, if the values ​​are different, it will send an email. How to deal with this problem using java. Thank you 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(" abcdr@gmail.com ", " 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 runs the sample() class for every second and sends an email to the specified address. I want to handle a timer that will run every second at the same time, an email is es.SendMail(" abcdr@gmail.com ", " Server process is down", message); if message contains values.

0
source share
4 answers

Refresh . The question has changed since the publication of this answer. This is the answer to the original question:

You can use the Timer class.

You can plan what actions to take when the timer is off through TimerTask . This class is abstract, so you need to extend it and implement the run() method.

 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 make Timer timer = new Timer(task,delay) to complete the task after delay miliseconds

+2
source

My assumptions:

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

First override the 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(" abcdr@gmail.com ", " Server process is down", sb); } } 

Is the order of the processes on the list important? For example, are there "abcd" and "badc" lists? If the answer is no, then java.util.List.equals will not meet your needs. In this case, do not use lists, but ask.


EDIT:

If the list is guaranteed to be ordered (if "abcd" will never be "bad-c"), it can be (and cheaper) to build, save and compare the string created from StringBuilder, instead of building, saving and comparing the list. This way you do not have to implement Result.equals and Result.hashCode .

+1
source
0
source

This may not be a bit related to your question, but I could not help but mention that you do almost the same thing twice, in two for loops. If I understand correctly, you want to make sure that there are no duplicates (both in the record that exists in both ActiveProcesses and Inactive Processes ). If this is the case, why not check for duplicates when adding an entry to the list?

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

Good luck.

EDIT: for future reference, please try to indicate exactly what your problem is related to the topic

0
source

All Articles