2

我正在尝试编写一个检查数据库运行状况的程序。程序元素的元素之一应该是程序查询数据库,然后使用等待等待 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);
  }
  }
4

2 回答 2

0

如果要使用等待和通知,我建议您使用Lock InterfaceReentrant Lock Class fromjava.util.concurrent package...

于 2012-06-29T17:37:18.650 回答
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();
}
于 2012-06-29T17:21:27.823 回答