Advertisement
Guest User

testing.Worker

a guest
Nov 16th, 2012
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package testing;
  2.  
  3. import org.jgroups.JChannel;
  4. import org.jgroups.blocks.RpcDispatcher;
  5.  
  6. public class Worker {
  7.  
  8.     private JChannel controlChannel;
  9.  
  10.     private RpcDispatcher controlReceiver;
  11.  
  12.     private boolean shutdown = false;
  13.  
  14.  
  15.     public Worker() {
  16.     }
  17.  
  18.  
  19.     protected void joinControl(String nodeName) throws Exception {
  20.         controlChannel = new JChannel();
  21.         controlReceiver = new RpcDispatcher(controlChannel, this);
  22.         controlChannel.setDiscardOwnMessages(true);
  23.         controlChannel.setName(nodeName);
  24.         controlChannel.connect("ControlCluster");
  25.     }
  26.  
  27.  
  28.     /**
  29.      * Close the channels.
  30.      */
  31.     protected void close() {
  32.         controlChannel.close();
  33.         controlReceiver.stop();
  34.     }
  35.  
  36.  
  37.     public void stop() {
  38.         System.out.println("Stop called on " + controlChannel.getName());
  39.         shutdown = true;
  40.     }
  41.  
  42.  
  43.     /**
  44.      * Connect to Control channel and wait for stop command.
  45.      *
  46.      * @param args Node name as first parameter
  47.      * @throws Exception
  48.      */
  49.     public static void main(String[] args) throws Exception {
  50.         Worker worker = new Worker();
  51.         try {
  52.             worker.joinControl(args[0]);
  53.             // Active polling just for testing, use a Semaphore for real work!
  54.             while (!worker.shutdown) {
  55.                 Thread.sleep(100);
  56.             }
  57.         } finally {
  58.             worker.close();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement