Advertisement
kendy

OracleConnector

Oct 19th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package OraclePinger;
  2.  
  3. import java.io.IOException;
  4.  
  5. // Thread for connecting to all targets in parallel via a single selector
  6. public class Connector extends Thread {
  7.     Selector sel;
  8.     //  Printer printer;
  9.  
  10.     // List of pending targets.  We use this list because if we try to
  11.     // register a channel with the selector while the connector thread is
  12.     // blocked in the selector then we will block.
  13.     LinkedList<Target> pending = new LinkedList<Target>();
  14.  
  15.     public Connector(/*Printer pr*/) throws Exception {
  16.         //      printer = pr;
  17.         sel = Selector.open();
  18.         setName("Connector");
  19.     }
  20.  
  21.     // Initiate a connection sequence to the given target and add the
  22.     // target to the pending-target list
  23.     public void add(Target t) {
  24.         SocketChannel sc = null;
  25.         try {
  26.  
  27.             // Open the channel, set it to non-blocking, initiate connect
  28.             sc = SocketChannel.open();
  29.             sc.configureBlocking(false);
  30.  
  31.             // Record the time we started
  32.             t.channel = sc;
  33.             t.connectStart = System.currentTimeMillis();
  34.  
  35.             boolean connected = sc.connect(t.address);
  36.             if (connected) {
  37.                 t.connectFinish = t.connectStart;
  38.                 sc.close();
  39.                 //              printer.add(t);
  40.             } else {
  41.                 // Add the new channel to the pending list
  42.                 synchronized (pending) {
  43.                     pending.add(t);
  44.                 }
  45.  
  46.                 // Nudge the selector so that it will process the pending list
  47.                 sel.wakeup();
  48.             }
  49.         } catch (IOException x) {
  50.             if (sc != null) {
  51.                 try {
  52.                     sc.close();
  53.                 } catch (IOException xx) {
  54.                     // print sc close IOception
  55.                 }
  56.             }
  57.             t.failure = x;
  58.             //          printer.add(t);
  59.         }
  60.     }
  61.  
  62.     // Process any targets in the pending list
  63.     void processPendingTargets() throws IOException {
  64.         synchronized (pending) {
  65.             while (pending.size() > 0) {
  66.                 Target t = (Target)pending.removeFirst();
  67.                 try {
  68.                     // Register the channel with the selector, indicating
  69.                     // interest in connection completion and attaching the
  70.                     // target object so that we can get the target back
  71.                     // after the key is added to the selector's
  72.                     // selected-key set
  73.                     t.channel.register(sel, SelectionKey.OP_CONNECT, t);
  74.                 } catch (IOException x) {
  75.                     // Something went wrong, so close the channel and
  76.                     // record the failure
  77.                     t.channel.close();
  78.                     t.failure = x;
  79.                     //                  printer.add(t);
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     // Process keys that have become selected
  86.     void processSelectedKeys() throws IOException {
  87.  
  88.         for ( SelectionKey sk : sel.selectedKeys() ) {//Iterator i = sel.selectedKeys().iterator(); i.hasNext();) {
  89.  
  90.             // Retrieve the next key and remove it from the set
  91.             //sk = (SelectionKey)i.next();
  92.             //i.remove();
  93.  
  94.             // Retrieve the target and the channel
  95.             Target t = (Target)sk.attachment();
  96.             SocketChannel sc = (SocketChannel)sk.channel();
  97.  
  98.             // Attempt to complete the connection sequence
  99.             try {
  100.                 if (sc.finishConnect()) {
  101.                     sk.cancel();
  102.                     t.connectFinish = System.currentTimeMillis();
  103.                     sc.close();
  104.                     //                  printer.add(t);
  105.                 }
  106.             } catch (IOException x) {
  107.                 sc.close();
  108.                 t.failure = x;
  109.                 //              printer.add(t);
  110.             }
  111.         }
  112.     }
  113.  
  114.     volatile boolean shutdown = false;
  115.  
  116.     // Invoked by the main thread when it's time to shut down
  117.     public void shutdown() {
  118.         shutdown = true;
  119.         sel.wakeup();
  120.     }
  121.  
  122.     // Connector thread loop
  123.     public void run() {
  124.         for (;;) {
  125.             try {
  126.                 int n = sel.select();
  127.                 if (n > 0)
  128.                     processSelectedKeys();
  129.                 processPendingTargets();
  130.                 if (shutdown) {
  131.                     sel.close();
  132.                     return;
  133.                 }
  134.             } catch (IOException x) {
  135.                 x.printStackTrace();
  136.             }
  137.         }
  138.     }
  139.  
  140.     //synchronize it to avoid thread interference &/ memory inconsistence during list removal
  141.     //  public synchronized void clearList(){
  142.     //      pending.clear();
  143.     //  }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement