Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package ppp.network;
  2.  
  3. import java.io.Closeable;
  4. import java.io.IOException;
  5. import java.util.logging.Logger;
  6.  
  7. import org.jgroups.JChannel;
  8. import org.jgroups.ReceiverAdapter;
  9. import org.jgroups.View;
  10. import org.jgroups.blocks.MethodCall;
  11. import org.jgroups.blocks.RequestOptions;
  12. import org.jgroups.blocks.ResponseMode;
  13. import org.jgroups.blocks.RpcDispatcher;
  14. import org.jgroups.fork.ForkChannel;
  15. import org.jgroups.protocols.FORK;
  16.  
  17. public class JChannelRunner extends ReceiverAdapter implements Closeable {
  18.  
  19.     public static class Boe1 {
  20.  
  21.         public void boeee() {
  22.             LOGGER.info("boe-1");
  23.         }
  24.  
  25.     }
  26.  
  27.     private final static Logger LOGGER = Logger.getLogger(JChannelRunner.class.getName());
  28.  
  29.     JChannel channel;
  30.  
  31.     ForkChannel forkChannel;
  32.  
  33.     RpcDispatcher dispatcher1;
  34.  
  35.     public JChannelRunner() throws Exception {
  36.         channel = new JChannel();
  37.         channel.setReceiver(this);
  38.  
  39.         if (channel.getProtocolStack().findProtocol(FORK.class) == null) {
  40.             channel.getProtocolStack().addProtocol(new FORK());
  41.         }
  42.  
  43.         forkChannel = new ForkChannel(channel, "fork", "fork");
  44.         dispatcher1 = new RpcDispatcher(forkChannel, new Boe1());
  45.  
  46.         channel.connect("test");
  47.         forkChannel.connect("test");
  48.     }
  49.  
  50.     public static void main(final String[] args) throws IOException, Exception {
  51.         try (JChannelRunner runner = new JChannelRunner()) {
  52.             Thread.sleep(60 * 1000);
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void close() throws IOException {
  58.         dispatcher1.close();
  59.         forkChannel.close();
  60.         channel.close();
  61.     }
  62.  
  63.     @Override
  64.     public void viewAccepted(final View new_view) {
  65.         LOGGER.info("viewAccepted:start");
  66.         try {
  67.             final MethodCall call = new MethodCall(Boe1.class.getMethod("boeee"));
  68.             final RequestOptions options = new RequestOptions(ResponseMode.GET_ALL, 0, true, null);
  69.             dispatcher1.callRemoteMethods(null, call, options);
  70.         } catch (final Exception e) {
  71.             e.printStackTrace();
  72.         }
  73.         LOGGER.info("viewAccepted:end");
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement