1 package javaclients.jdbc;
  2 
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.util.Global;
  6 import org.xmlBlaster.client.I_XmlBlasterAccess;
  7 import org.xmlBlaster.client.I_Callback;
  8 import org.xmlBlaster.client.XmlDbMessageWrapper;
  9 import org.xmlBlaster.client.key.UpdateKey;
 10 import org.xmlBlaster.client.qos.UpdateQos;
 11 
 12 
 13 /**
 14  * Example code how to access the xmlBlaster JDBC service
 15  * asynchronous with the subscribe() method.
 16  * <p />
 17  * The result of the query is delivered asynchronously
 18  * with the callback update() method.
 19  * <p />
 20  * The publishing of the query is not blocking.
 21  * <p />
 22  * See README for usage
 23  *
 24  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.service.rdbms.html">Requirement engine.service.rdbms</a>
 25  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.service.rdbms.jdbcpool.html">Requirement engine.service.rdbms.jdbcpool</a>
 26  */
 27 public class XmlDBClient implements I_Callback
 28 {
 29    private static String ME = "XmlDBClient";
 30    private final Global glob;
 31    private static Logger log = Logger.getLogger(XmlDBClient.class.getName());
 32    private I_XmlBlasterAccess con = null;
 33    private String results;
 34    private boolean done = false;
 35 
 36    /**
 37     * Constructor declaration
 38     */
 39    public XmlDBClient(Global glob) {
 40       this.glob = glob;
 41 
 42       initBlaster();
 43       query();
 44       waitOnResults();
 45    }
 46 
 47 
 48    /**
 49     */
 50    private void waitOnResults() {
 51       while (!done) {
 52          try {
 53             Thread.sleep(1000);
 54          }
 55          catch (InterruptedException e) {}
 56          System.out.println("Waiting...");
 57       }
 58       System.out.println(results);
 59       logout();
 60    }
 61 
 62    /**
 63     * This is the callback method invoked from xmlBlaster
 64     * delivering us a new asynchronous message. 
 65     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
 66     */
 67    public String update(String cbSessionId, UpdateKey key, byte[] content, UpdateQos updateQos) {
 68       results = new String(content);
 69       log.info("Receiving message oid=" + key.getOid() + " state=" + updateQos.getState());
 70       done = true;
 71       return "";
 72    }
 73 
 74    /**
 75     * Find xmlBlaster server and login.
 76     */
 77    public void initBlaster() {
 78       try {
 79          con = glob.getXmlBlasterAccess();
 80          con.connect(null, this);
 81          log.info("Connected to xmlBlaster");
 82       }
 83       catch (Exception e) {
 84          e.printStackTrace();
 85          log.severe("Login to xmlBlaster failed");
 86          System.exit(1);
 87       }
 88    }
 89 
 90    /**
 91     * Logout from xmlBlaster
 92     */
 93    public void logout() {
 94       if (con == null) return;
 95       log.info("Logout ...");
 96       con.disconnect(null);
 97    }
 98 
 99    /**
100     * Send the SQL message.
101     */
102    private void query() {
103       XmlDbMessageWrapper wrap = new XmlDbMessageWrapper(glob,
104          glob.getProperty().get("user", "postgres"),
105          glob.getProperty().get("pass", ""),
106          glob.getProperty().get("url",  "jdbc:postgresql://24.3.47.214/postgres"));
107 
108       boolean confirm = glob.getProperty().get("confirm", true);
109       String type = glob.getProperty().get("type", "query");
110       int limit = glob.getProperty().get("limit", 50);
111       String queryStr = glob.getProperty().get("query", "select * from intrauser");
112 
113       wrap.init(type, limit, confirm, queryStr);
114 
115       try {
116          con.publish(wrap.toMessage());
117          log.info("Published query ...");
118          if (log.isLoggable(Level.FINEST)) log.finest(wrap.toXml());
119       }
120       catch (Exception e) { log.severe(e.getMessage()); }
121 
122       if (!queryStr.equalsIgnoreCase("query") && !confirm) {
123          logout();
124          log.info("Done, no waiting on confirmation");
125          System.exit(0);
126       }
127    }
128 
129    /**
130     * @param args Command line
131     */
132    public static void main(String args[]) {
133       XmlDBClient client = new XmlDBClient(new Global(args));
134    }
135 }


syntax highlighted by Code2HTML, v. 0.9.1