Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.datis.zookafka.client; import java.io.Closeable; import java.io.IOException; import org.apache.zookeeper.AsyncCallback; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author jeus */ public class Creator extends javax.swing.JFrame implements org.apache.zookeeper.Watcher, Closeable { private static final Logger LOG = LoggerFactory.getLogger(Test1.class); private volatile boolean expired = false; private volatile boolean connected = false; private String hostInfo; ZooKeeper zk; /** * Creates new form Creator */ public Creator() { initComponents(); } public Creator(String hostInfo) { System.out.println("=========================" + hostInfo); this.hostInfo = hostInfo; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTextField1 = new javax.swing.JTextField(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jCheckBox1 = new javax.swing.JCheckBox(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jRadioButton1.setText("Ephemeral"); jRadioButton2.setText("Persistent"); jButton1.setText("Add"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jCheckBox1.setText("Sequential"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1) .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout .createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout .createSequentialGroup().addComponent(jRadioButton2) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE) .addComponent(jCheckBox1))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(25, 25, 25) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jRadioButton1).addComponent(jRadioButton2).addComponent(jCheckBox1)) .addGap(18, 18, 18).addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed System.out.println("***************************************"); String a = jTextField1.getText(); System.out.println("output of textfield:" + a); testCreate(a); }//GEN-LAST:event_jButton1ActionPerformed void startZK() throws IOException { System.out.println("HOST INFORMATION:" + hostInfo); zk = new ZooKeeper(hostInfo, 15000, this); System.out.println(zk.getState()); System.out.println(zk.getState().name()); } /** * @param args the command line arguments */ public static void main(String args[]) throws Exception { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Creator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Creator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Creator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Creator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Creator().setVisible(true); } }); Creator test1; if (args.length == 1) { System.out.println("Has Input argument:" + args[0]); test1 = new Creator(args[0]); test1.startZK(); } else { System.out.println("not input argument"); test1 = new Creator("172.17.0.8:2181"); } test1.startZK(); while (!test1.isConnected()) { Thread.sleep(100); } test1.testCreate("/JeusTest"); while (!test1.isExpired()) { Thread.sleep(1000); } } boolean isExpired() { // System.out.println("Expire is:"+expired); // System.out.println("State IsAlive:"+zk.getState().isAlive()); // System.out.println("State Is Connected:"+zk.getState().isConnected()); // System.out.println("State name:"+zk.getState().name()); // System.out.println("State toString:"+zk.getState().toString()); return expired; } boolean isConnected() { return connected; } public void testCreate(Object path) { String newZnode = (path == null ? "/test1" : (String) path); System.out.println("NewZNODE IS:" + newZnode); createParent(newZnode, new byte[0]); // createParent("/assign", new byte[0]); // createParent("/tasks", new byte[0]); // createParent("/status", new byte[0]); } void createParent(String path, byte[] data) { zk.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, createParentCallback, data); System.out.println("CALLBACK IS:" + createParentCallback.toString()); } AsyncCallback.StringCallback createParentCallback = new AsyncCallback.StringCallback() { public void processResult(int rc, String path, Object ctx, String name) { System.out.println("Callback name:" + name); System.out.println("Callback object :" + ctx.toString()); System.out.println("Callback path:" + path); System.out.println("Callback rc:" + rc); switch (KeeperException.Code.get(rc)) { case CONNECTIONLOSS: /* * Try again. Note that registering again is not a problem. * If the znode has already been created, then we get a * NODEEXISTS event back. */ createParent(path, (byte[]) ctx); System.out.println("*************#######"); break; case OK: System.out.println("PARENT CREATED"); LOG.info("Parent created"); break; case NODEEXISTS: LOG.warn("Parent already registered: " + path); break; default: LOG.error("Something went wrong: ", KeeperException.create(KeeperException.Code.get(rc), path)); } } }; @Override public void process(WatchedEvent e) { LOG.info("Processing event: " + e.toString()); if (e.getType() == Event.EventType.None) { switch (e.getState()) { case SyncConnected: System.out.println("Event SynConnected"); connected = true; break; case Disconnected: System.out.println("Event Disconnected"); connected = false; break; case Expired: System.out.println("Event Expired"); expired = true; connected = false; LOG.error("Session expiration"); default: break; } } } @Override public void close() throws IOException { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }