Advertisement
Guest User

testing.Control

a guest
Nov 16th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package testing;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6.  
  7. import org.jgroups.Address;
  8. import org.jgroups.JChannel;
  9. import org.jgroups.View;
  10. import org.jgroups.blocks.RequestOptions;
  11. import org.jgroups.blocks.ResponseMode;
  12. import org.jgroups.blocks.RpcDispatcher;
  13.  
  14. public class Control {
  15.  
  16.     private JChannel controlChannel;
  17.  
  18.     private RpcDispatcher controlDispatcher;
  19.  
  20.  
  21.     public Control() {
  22.     }
  23.  
  24.  
  25.     public void joinControl() throws Exception {
  26.         controlChannel = new JChannel();
  27.         controlDispatcher = new RpcDispatcher(controlChannel, new Object());
  28.         controlChannel.setDiscardOwnMessages(true);
  29.         controlChannel.setName("Control");
  30.         controlChannel.connect("ControlCluster");
  31.     }
  32.  
  33.  
  34.     /**
  35.      * Close the channels.
  36.      */
  37.     public void close() {
  38.         controlChannel.close();
  39.         controlDispatcher.stop();
  40.     }
  41.  
  42.  
  43.     public void sendCommand(String command, String target) throws Exception {
  44.  
  45.         Collection<Address> destinations = createDestinationsByTarget(target);
  46.         Object[] args = null;
  47.         Class<?>[] types = null;
  48.         RequestOptions options = new RequestOptions();
  49.         options.setMode(ResponseMode.GET_NONE);
  50.         controlDispatcher.callRemoteMethods(destinations, command, args, types, options);
  51.     }
  52.  
  53.  
  54.     /**
  55.      * Determine all destinations. Do not include those that are just called "Control".
  56.      *
  57.      * @param target If non-<code>null</code>, only query this one.
  58.      * @return
  59.      */
  60.     protected Collection<Address> createDestinationsByTarget(String target) {
  61.         Collection<Address> destinations = new ArrayList<Address>();
  62.         if (controlChannel != null) {
  63.             View view = controlChannel.getView();
  64.             List<Address> members = view.getMembers();
  65.             for (Address address : members) {
  66.                 String name = address.toString();
  67.                 if ("Control".equals(name)) {
  68.                     // Do not send to nodes named "Control".
  69.                     // They are not able to reply.
  70.                 } else if (target != null) {
  71.                     if (address.toString().equals(target)) {
  72.                         destinations.add(address);
  73.                         System.out.println("Limited target: " + address);
  74.                     }
  75.                 } else {
  76.                     destinations.add(address);
  77.                     System.out.println("Unlimited target: " + address);
  78.                 }
  79.             }
  80.         }
  81.         if (destinations.isEmpty()) {
  82.             throw new IllegalArgumentException("Unknown address \"" + target + "\"");
  83.         }
  84.         return destinations;
  85.     }
  86.  
  87.  
  88.     public static void main(String[] args) throws Exception {
  89.         Control control = new Control();
  90.         try {
  91.             control.joinControl();
  92.             control.sendCommand("stop", args[0]);
  93.         } finally {
  94.             control.close();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement