Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing Wait and Notify
    primarykey
    data
    text
    <p>I'm trying to write a program that checks on the health of a database. One of the elements of the elements of the program is supposed to be that the program queries a database and then waits 5 minutes using wait. If there is no response it notifies and sends out some emails. My connection to the database/sending out emails all works, but i'm having trouble implementing wait and notify. I read the api and its easy to understand in a simple program, but i'm really confused as to how to implement it in this case with all the additional complications because of errors where I can't call something dynamic from a static method.</p> <p>I've been reading through lots of threads with wait and notify, but haven't figured out how do to it correctly in my program. If anyone could give me a few tips it would be a huge help. Thanks!</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload