如何解决java中的定时器问题?

发布于 2024-09-08 14:37:16 字数 1707 浏览 0 评论 0原文

我正在使用 Timer 开发 java 电子邮件应用程序,我有两个名为 ActiveProcesses、InActiveProcesses 的数组列表。如果我启动计时器,它将每秒发送包含 InActiveProcesses 列表值的电子邮件。但问题是,如果 InActiveProcesses 列表值相同,计时器就会发送电子邮件。例如,InActiveProcesses 列表包含值 abcd,它将每秒发送具有相同列表值的电子邮件。我只想发送电子邮件,仅 InActiveProcesses 列表包含不同的值。计时器将每秒检查这些值,如果值不同,它将发送电子邮件。如何使用java来处理这个问题。提前致谢。这是代码,

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);

这是我的 Timer 类。

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

int interval = 1000; // iterate every sec.

Timer timer = new Timer();

        timer.scheduleAtFixedRate(new sample() {

        }, delay, interval);

计时器每秒执行 sample() 类,并将电子邮件发送到指定地址。我想处理计时器将同时每秒执行一次,发送电子邮件 es.SendMail("[电子邮件受保护]", "服务器进程已关闭", 消息); 如果 消息 包含值不同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坚持沉默 2024-09-15 14:37:16

更新:发布此答案后问题发生了变化。这是原始问题的答案:

您可以使用 Timer 类。

您可以通过 定时器任务。这个类是抽象的,所以你必须扩展它并实现方法run()

class MyTimerTask extends TimerTask {

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

然后在你的主类中

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!

或者你可以做Timertimer = new Timer(task,delay)让任务在delay毫秒后执行

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

青衫负雪 2024-09-15 14:37:16

我的假设:

  • list3 引用 java.util.List 的实例。
  • 您可以修改 Result 类。

首先,重写 Result 类中的 Object.equals 方法。

那么:

...

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);
    }
}

列表中进程的顺序重要吗?例如,列表“abcd”和“badc”是否不同?如果答案是否定的,那么 java.util.List.equals 将无法满足您的需求。在这种情况下,不要使用列表,而是使用集合。


编辑:

如果保证列表的顺序一致(如果“abcd”永远不会是“badc”),则可能(并且更便宜)构建、保存和比较从 StringBuilder 创建的字符串,而不是构建、保存和比较列表。通过这种方式,您也不必实现 Result.equals 和 Result.hashCode 。

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.

烂人 2024-09-15 14:37:16

这可能与你的问题略有无关,但我忍不住提到你在两个 for 循环中做了两次几乎相同的事情。如果我理解正确,您需要确保没有重复项(例如在 ActiveProcessesInactive Processes 中都存在的条目)。如果是这种情况,为什么在向列表添加条目时不检查重复项呢?

至于计时器,我必须同意前面的答案: java.util.Timer 很可能是您所需要的。从那里开始:)

祝你好运

编辑:为了将来的参考,请尝试指定你的“问题”是什么。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文