Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testing;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import org.jgroups.Address;
- import org.jgroups.JChannel;
- import org.jgroups.View;
- import org.jgroups.blocks.RequestOptions;
- import org.jgroups.blocks.ResponseMode;
- import org.jgroups.blocks.RpcDispatcher;
- public class Control {
- private JChannel controlChannel;
- private RpcDispatcher controlDispatcher;
- public Control() {
- }
- public void joinControl() throws Exception {
- controlChannel = new JChannel();
- controlDispatcher = new RpcDispatcher(controlChannel, new Object());
- controlChannel.setDiscardOwnMessages(true);
- controlChannel.setName("Control");
- controlChannel.connect("ControlCluster");
- }
- /**
- * Close the channels.
- */
- public void close() {
- controlChannel.close();
- controlDispatcher.stop();
- }
- public void sendCommand(String command, String target) throws Exception {
- Collection<Address> destinations = createDestinationsByTarget(target);
- Object[] args = null;
- Class<?>[] types = null;
- RequestOptions options = new RequestOptions();
- options.setMode(ResponseMode.GET_NONE);
- controlDispatcher.callRemoteMethods(destinations, command, args, types, options);
- }
- /**
- * Determine all destinations. Do not include those that are just called "Control".
- *
- * @param target If non-<code>null</code>, only query this one.
- * @return
- */
- protected Collection<Address> createDestinationsByTarget(String target) {
- Collection<Address> destinations = new ArrayList<Address>();
- if (controlChannel != null) {
- View view = controlChannel.getView();
- List<Address> members = view.getMembers();
- for (Address address : members) {
- String name = address.toString();
- if ("Control".equals(name)) {
- // Do not send to nodes named "Control".
- // They are not able to reply.
- } else if (target != null) {
- if (address.toString().equals(target)) {
- destinations.add(address);
- System.out.println("Limited target: " + address);
- }
- } else {
- destinations.add(address);
- System.out.println("Unlimited target: " + address);
- }
- }
- }
- if (destinations.isEmpty()) {
- throw new IllegalArgumentException("Unknown address \"" + target + "\"");
- }
- return destinations;
- }
- public static void main(String[] args) throws Exception {
- Control control = new Control();
- try {
- control.joinControl();
- control.sendCommand("stop", args[0]);
- } finally {
- control.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement