实现等待和通知

时间:2012-06-29 17:08:49

标签: java wait notify kdb

我正在尝试编写一个检查数据库运行状况的程序。程序元素的一个元素应该是程序查询数据库然后等待5分钟。如果没有响应,它会通知并发送一些电子邮件。我与数据库/发送电子邮件的连接都有效,但我无法实现等待和通知。 我在一个简单的程序中阅读了api并且它易于理解,但是我真的很困惑如何在这种情况下实现它以及所有额外的复杂性,因为我无法通过静态方法调用动态的错误。

我一直在阅读很多有等待和通知的线程,但是在我的程序中没有弄清楚如何正确处理它。如果有人能给我一些提示,那将是一个巨大的帮助。谢谢!

import com.fmr.ipgt.email.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import javax.mail.MessagingException;

class MyResource {
synchronized void qQuery() throws Exception {
    String query = ".z.k"; // The query that is used to query q; this can be changed here.
    int version = 0;
    c qConn = null;
    qConn = new c(Main.host,Main.port); // Connect to the q database
      while (Main.healthy) {
          Object o = qConn.k(query); // Query q
          version = c.t(o);
          if(!(version==0)) {
              break; // End the process if the database responds
          }
          }
  }
  synchronized void start() throws Exception {
    Main.setHealth(false);
    Main.sendMessages();
  }
}

class MyThread implements Runnable {
  MyResource myResource;

  MyThread(String name, MyResource so) {
    myResource = so;
    new Thread(this, name).start();
  }

  public void run() {

    try {
      myResource.qQuery(); // Begin a method to query q.
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

public class Main {

private static String[] recipients;
private static String subject = "Database Failure";
private static String message = "The database has failed or is in a hung state";
private static String from;
static String host;
static int port;
private static String emails;
private static int minutes;
static boolean healthy = true;
public static void main(String args[]) throws Exception {

    // Import information from the configuration file
      SAXBuilder builder = new SAXBuilder();
      File xmlFile = new File("/export/home/jflt/file.xml"); // Note: The directory for the configuration file may need to be changed

      try {

        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List list = rootNode.getChildren("parameters");
           Element node = (Element) list.get(0);

          host = node.getChildText("host");
           port = Integer.parseInt(node.getChildText("port"));
           emails = node.getChildText("emails");
           String delims = "[ ]+";
           recipients = emails.split(delims); // parse email list
           minutes = Integer.parseInt(node.getChildText("time"));
           from = node.getChildText("from");

      } catch (IOException io) {
        System.out.println(io.getMessage());
      } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
      }
    MyResource unhealthy = new MyResource();
    new MyThread("MyThread", unhealthy);  // Create new Thread
    new MyThread("WaitThread", unhealthy);
    while(healthy) {
    Thread.sleep(minutes*60000); // The wrong thread is sleeping here. The main method should probably be a different thread instead which will then need to wait and the second thread will notify.
    }
    unhealthy.start(); // The database has not responded for the given time. Report that it is unhealthy.
  }
  public static void setHealth(boolean health){
      System.out.println("database unhealthy");
    healthy  = health;  
  }

  public static void sendMessages() throws MessagingException {
        System.out.println("sending emails");
      FCAPMailSender.postMail(recipients,subject,message,from);
  }
  }

2 个答案:

答案 0 :(得分:0)

使用发送电子邮件的ExecutorService安排任务。收到回复后,取消该任务。如果超过5分钟,则执行程序线程已经发送了邮件,并且取消是无操作。否则,电子邮件将中止。


这甚至不是wait() / notify()问题;线程之间没有数据传递。这是与ExecutorService解决方案等效的低级别。

void test() {
  Thread t = new Thread() {
    @Override
    public void run() {
      try { Thread.sleep(TimeUnit.MINUTES.toMillis(5)); } 
      catch(InterruptedException abort) { return; }
      email();
    }
  }
  t.start();
  query();
  t.interrupt();
}

答案 1 :(得分:0)

如果要使用等待和通知,我建议您使用java.util.concurrent package...

中的锁定界面可重入锁定类