예제 #1
0
  private void send(Address dest) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final BlockingReceiver receiver = new BlockingReceiver(latch);
    final int NUM = 10;
    b.setReceiver(receiver);

    a.send(dest, 1); // the only regular message
    for (int i = 2; i <= NUM; i++) a.send(new Message(dest, i).setFlag(Message.Flag.OOB));

    sendStableMessages(a, b);
    List<Integer> list = receiver.getMsgs();
    for (int i = 0; i < 20; i++) {
      if (list.size() == NUM - 1) break;
      sendStableMessages(a, b);
      Util.sleep(500); // give the asynchronous msgs some time to be received
    }

    System.out.println("list = " + list);
    assert list.size() == NUM - 1 : "list is " + list;
    assert list.contains(2) && list.contains(10);

    System.out.println("[" + Thread.currentThread().getName() + "]: releasing latch");
    latch.countDown();

    for (int i = 0; i < 20; i++) {
      if (list.size() == NUM) break;
      sendStableMessages(a, b);
      Util.sleep(1000); // give the asynchronous msgs some time to be received
    }

    System.out.println("list = " + list);
    assert list.size() == NUM : "list is " + list;
    for (int i = 1; i <= NUM; i++) assert list.contains(i);
  }
예제 #2
0
  public void testRegularAndOOBMulticasts() throws Exception {
    DISCARD discard = new DISCARD();
    ProtocolStack stack = a.getProtocolStack();
    stack.insertProtocol(discard, ProtocolStack.BELOW, NAKACK2.class);
    a.setDiscardOwnMessages(true);

    Address dest = null; // send to all
    Message m1 = new Message(dest, null, 1);
    Message m2 = new Message(dest, null, 2);
    m2.setFlag(Message.OOB);
    Message m3 = new Message(dest, null, 3);

    MyReceiver receiver = new MyReceiver("C2");
    b.setReceiver(receiver);
    a.send(m1);
    discard.setDropDownMulticasts(1);
    a.send(m2);
    a.send(m3);

    Util.sleep(500);
    Collection<Integer> list = receiver.getMsgs();
    for (int i = 0; i < 10; i++) {
      System.out.println("list = " + list);
      if (list.size() == 3) break;
      Util.sleep(1000); // give the asynchronous msgs some time to be received
      sendStableMessages(a, b);
    }
    assert list.size() == 3 : "list is " + list;
    assert list.contains(1) && list.contains(2) && list.contains(3);
  }
예제 #3
0
  public void testRegularAndOOBUnicasts2() throws Exception {
    DISCARD discard = new DISCARD();
    ProtocolStack stack = a.getProtocolStack();
    stack.insertProtocol(
        discard, ProtocolStack.BELOW, (Class<? extends Protocol>[]) Util.getUnicastProtocols());

    Address dest = b.getAddress();
    Message m1 = new Message(dest, 1);
    Message m2 = new Message(dest, 2).setFlag(Message.Flag.OOB);
    Message m3 = new Message(dest, 3).setFlag(Message.Flag.OOB);
    Message m4 = new Message(dest, 4);

    MyReceiver receiver = new MyReceiver("B");
    b.setReceiver(receiver);
    a.send(m1);

    discard.setDropDownUnicasts(2);
    a.send(m2); // dropped
    a.send(m3); // dropped
    a.send(m4);

    Collection<Integer> list = receiver.getMsgs();
    int count = 10;
    while (list.size() < 4 && --count > 0) {
      Util.sleep(500); // time for potential retransmission
      sendStableMessages(a, b);
    }
    System.out.println("list = " + list);
    assert list.size() == 4 : "list is " + list;
    assert list.contains(1) && list.contains(2) && list.contains(3) && list.contains(4);
  }
예제 #4
0
  /**
   * Tests sending 1, 2 (OOB) and 3, where they are received in the order 1, 3, 2. Message 3 should
   * not get delivered until message 4 is received (http://jira.jboss.com/jira/browse/JGRP-780)
   */
  public void testRegularAndOOBUnicasts() throws Exception {
    DISCARD discard = new DISCARD();
    ProtocolStack stack = a.getProtocolStack();
    stack.insertProtocol(discard, ProtocolStack.BELOW, UNICAST.class, UNICAST2.class);

    Address dest = b.getAddress();
    Message m1 = new Message(dest, null, 1);
    Message m2 = new Message(dest, null, 2);
    m2.setFlag(Message.OOB);
    Message m3 = new Message(dest, null, 3);

    MyReceiver receiver = new MyReceiver("C2");
    b.setReceiver(receiver);
    a.send(m1);
    discard.setDropDownUnicasts(1);
    a.send(m2);
    a.send(m3);

    Collection<Integer> list = receiver.getMsgs();
    int count = 10;
    while (list.size() < 3 && --count > 0) {
      Util.sleep(500); // time for potential retransmission
      sendStableMessages(a, b);
    }

    assert list.size() == 3 : "list is " + list;
    assert list.contains(1) && list.contains(2) && list.contains(3);
  }
예제 #5
0
 public void send(MessageType type) throws Exception {
   Packet packet = new Packet();
   packet.setType(type);
   Message message = new Message();
   message.setObject(packet);
   channel.send(message);
 }
  @Override
  public void run() {
    try {
      log.info("Hello from Cluster microservice provider!");

      channel = new JChannel((String) context.getProperties().get(CLUSTER_CONFIGURATION));
      channel.setReceiver(receiver);

      channel.connect((String) context.getProperties().get(CLUSTER_GROUP));

      channel.send(new Message(null, "Holáryjou!"));

      try {
        while (!Thread.currentThread().isInterrupted()) {
          Thread.sleep(1000);
        }
      } catch (InterruptedException ie) {
        Utils.shutdownLog(log, ie);
      } finally {
        channel.close();
      }
    } catch (Exception e) {
      log.error("Cluster microservice provider failed: ", e);
    }
  }
예제 #7
0
  /** Tests https://jira.jboss.org/jira/browse/JGRP-1079 */
  public void testOOBMessageLoss() throws Exception {
    Util.close(b); // we only need 1 channel
    MyReceiver receiver = new MySleepingReceiver("C1", 1000);
    a.setReceiver(receiver);

    TP transport = a.getProtocolStack().getTransport();
    transport.setOOBRejectionPolicy("discard");

    final int NUM = 10;

    for (int i = 1; i <= NUM; i++) {
      Message msg = new Message(null, null, i);
      msg.setFlag(Message.OOB);
      a.send(msg);
    }
    STABLE stable = (STABLE) a.getProtocolStack().findProtocol(STABLE.class);
    if (stable != null) stable.runMessageGarbageCollection();
    Collection<Integer> msgs = receiver.getMsgs();

    for (int i = 0; i < 20; i++) {
      if (msgs.size() == NUM) break;
      Util.sleep(1000);
      sendStableMessages(a, b);
    }

    System.out.println("msgs = " + Util.print(msgs));

    assert msgs.size() == NUM
        : "expected " + NUM + " messages but got " + msgs.size() + ", msgs=" + Util.print(msgs);
    for (int i = 1; i <= NUM; i++) {
      assert msgs.contains(i);
    }
  }
예제 #8
0
  private void sendMessage() {
    try {
      /**
       * 参数里指定Channel使用的协议栈,如果是空的,则使用默认的协议栈, 位于JGroups包里的udp.xml。参数可以是一个以冒号分隔的字符串,
       * 或是一个XML文件,在XML文件里定义协议栈。
       */
      logger.warn("发送监控同步数据通知");

      Properties prop = SystemPropertiesUtils.getProp();
      String hostName = NetUtils.getLocalHost();
      prop.put("node.ip", NetUtils.getIpByHost(hostName));
      prop.put("node.host", hostName);
      prop.put("install.path", getInstallPath());
      // 创建一个通道
      JChannel channel = new JChannel();
      // 加入一个群
      channel.connect("MonitorContainer");
      // 发送事件
      // 这里的Message的第一个参数是发送端地址
      // 第二个是接收端地址
      // 第三个是发送的字符串
      // 具体参见jgroup send API
      Message msg = new Message(null, null, prop);
      // 发送
      channel.send(msg);
      // 关闭通道
      channel.close();
    } catch (Exception e) {
      logger.error(e.getMessage());
    }
  }
예제 #9
0
  /** Tests https://jira.jboss.org/jira/browse/JGRP-1079 for unicast messages */
  public void testOOBUnicastMessageLoss() throws Exception {
    MyReceiver receiver = new MySleepingReceiver("C2", 1000);
    b.setReceiver(receiver);

    a.getProtocolStack().getTransport().setOOBRejectionPolicy("discard");

    final int NUM = 10;
    final Address dest = b.getAddress();
    for (int i = 1; i <= NUM; i++) {
      Message msg = new Message(dest, null, i);
      msg.setFlag(Message.OOB);
      a.send(msg);
    }

    Collection<Integer> msgs = receiver.getMsgs();

    for (int i = 0; i < 20; i++) {
      if (msgs.size() == NUM) break;
      Util.sleep(1000);
      // sendStableMessages(c1,c2); // not needed for unicasts !
    }

    assert msgs.size() == NUM
        : "expected " + NUM + " messages but got " + msgs.size() + ", msgs=" + Util.print(msgs);
    for (int i = 1; i <= NUM; i++) {
      assert msgs.contains(i);
    }
  }
예제 #10
0
 /**
  * Send num unicasts on both channels and verify the other end received them
  *
  * @param num
  * @throws Exception
  */
 private void sendToEachOtherAndCheck(int num) throws Exception {
   for (int i = 1; i <= num; i++) {
     a.send(b_addr, "m" + i);
     b.send(a_addr, "m" + i);
   }
   List<Message> l1 = r1.getMessages();
   List<Message> l2 = r2.getMessages();
   for (int i = 0; i < 10; i++) {
     if (l1.size() == num && l2.size() == num) break;
     Util.sleep(500);
   }
   System.out.println("l1 = " + print(l1));
   System.out.println("l2 = " + print(l2));
   assert l1.size() == num;
   assert l2.size() == num;
 }
예제 #11
0
  public static void main(String[] args) throws Exception {

    JChannel channel = new JChannel();

    channel.setReceiver(
        new ReceiverAdapter() {

          public void receive(Message msg) {
            Address sender = msg.getSrc();
            System.out.println(msg.getObject() + " [" + sender + "]");
          }

          public void viewAccepted(View view) {
            System.out.println("view: " + view);
          }
        });

    channel.connect("ChatCluster");

    System.out.println(
        channel.getView().getCreator().equals(channel.getView().getMembers().get(0)));

    for (; ; ) {
      String line = Util.readStringFromStdin(": ");
      channel.send(null, line);
    }
  }
예제 #12
0
 public void sendInfo(String info, Address address) throws Exception {
   Packet packet = new Packet();
   packet.setType(MessageType.Info);
   packet.setInfo(info);
   Message message = new Message(address);
   message.setObject(packet);
   channel.send(message);
 }
예제 #13
0
 public void send(MessageType type, Address address) throws Exception {
   Packet packet = new Packet();
   packet.setType(type);
   packet.setReply(false);
   Message message = new Message(address);
   message.setObject(packet);
   channel.send(message);
 }
예제 #14
0
 public void sendJoin(String name) throws Exception {
   Message message = new Message();
   Packet packet = new Packet();
   packet.setType(MessageType.Join);
   packet.setpName(name);
   message.setObject(packet);
   channel.send(message);
 }
예제 #15
0
 public void sendGameStatusUpdate(MessageType type) throws Exception {
   Packet packet = new Packet();
   packet.setType(type);
   packet.setReply(true);
   packet.setGameStatus(new GameStatus().update(gameInfo));
   Message message = new Message();
   message.setObject(packet);
   channel.send(message);
 }
예제 #16
0
 protected void sendMessage(JChannel ch, String message) {
   try {
     Message msg = new Message(null, message);
     msg.setFlag(Message.Flag.OOB);
     ch.send(msg);
   } catch (Exception e) {
     e.printStackTrace(System.err);
   }
 }
예제 #17
0
 private void sendMessage(String text) {
   String line = "[" + user_name + "] " + text;
   Message msg = new Message(null, null, line);
   try {
     channel.send(msg);
   } catch (Exception e) {
     System.out.println("Sending failed!");
   }
 }
예제 #18
0
 public void sendGameStatusToClient() throws Exception {
   Packet packet = new Packet();
   packet.setType(MessageType.Update_Families);
   packet.setReply(false);
   packet.setGameStatus(new GameStatus().update(serverGameInfo));
   Message message = new Message();
   message.setObject(packet);
   channel.send(message);
 }
예제 #19
0
 /** Send Clear command to all members in Group */
 public void sendClearPanelMsg() {
   DrawCommand comm = new DrawCommand(DrawCommand.CLEAR);
   try {
     byte[] buf = Util.streamableToByteBuffer(comm);
     if (use_unicasts) sendToAll(buf);
     else channel.send(new Message(null, null, buf));
   } catch (Exception ex) {
     System.err.println(ex);
   }
 }
예제 #20
0
  private void send(Address dest) throws ChannelNotConnectedException, ChannelClosedException {
    final ReentrantLock lock = new ReentrantLock();
    final BlockingReceiver receiver = new BlockingReceiver(lock);
    final int NUM = 10;
    c2.setReceiver(receiver);

    System.out.println("[" + Thread.currentThread().getName() + "]: locking lock");
    lock.lock();
    c1.send(new Message(dest, null, 1));
    for (int i = 2; i <= NUM; i++) {
      Message msg = new Message(dest, null, i);
      msg.setFlag(Message.OOB);
      c1.send(msg);
    }
    sendStableMessages(c1, c2);
    Util.sleep(500);

    List<Integer> list = receiver.getMsgs();
    for (int i = 0; i < 20; i++) {
      if (list.size() == NUM - 1) break;
      System.out.println("list = " + list);
      Util.sleep(1000); // give the asynchronous msgs some time to be received
    }

    System.out.println("list = " + list);

    assert list.size() == NUM - 1 : "list is " + list;
    assert list.contains(2) && list.contains(10);

    System.out.println("[" + Thread.currentThread().getName() + "]: unlocking lock");
    lock.unlock();

    for (int i = 0; i < 20; i++) {
      if (list.size() == NUM) break;
      System.out.println("list = " + list);
      Util.sleep(1000); // give the asynchronous msgs some time to be received
    }

    System.out.println("list = " + list);
    assert list.size() == NUM : "list is " + list;
    for (int i = 1; i <= NUM; i++) assert list.contains(i);
  }
 @Override
 public void send(String routingKey, CommandMessage<?> commandMessage) throws Exception {
   String destination =
       consistentHash.getMember(routingKey, commandMessage.getPayloadType().getName());
   if (destination == null) {
     throw new CommandDispatchException(
         "No node known to accept " + commandMessage.getPayloadType().getName());
   }
   Address dest = getAddress(destination);
   channel.send(dest, new DispatchMessage(commandMessage, serializer, false));
 }
 protected void receive(Message msg) {
   String greeting = (String) msg.getObject();
   msgs.add(greeting);
   System.out.println("received " + greeting + " from " + msg.getSrc());
   if (HELLO1.equals(greeting)) {
     try {
       a.send(new Message(a.getAddress(), HELLO2));
     } catch (Exception e) {
       ex = e;
     }
   }
 }
예제 #23
0
  @Test(dataProvider = "provider")
  public void testSimpleMessageReception(String props) throws Exception {
    c1 = new JChannel(props);
    c2 = new JChannel(props);
    MyReceiver r1 = new MyReceiver("c1"), r2 = new MyReceiver("c2");
    c1.setReceiver(r1);
    c2.setReceiver(r2);
    c1.connect("testSimpleMessageReception");
    c2.connect("testSimpleMessageReception");

    int NUM = 100;
    Address c1_addr = c1.getAddress(), c2_addr = c2.getAddress();
    for (int i = 1; i <= NUM; i++) {
      c1.send(c1_addr, "bla");
      c1.send(c2_addr, "bla");
      c2.send(c2_addr, "bla");
      c2.send(c1_addr, "bla");
    }

    for (int i = 0; i < 10; i++) {
      if (r1.getNum() == NUM * 2 && r2.getNum() == NUM * 2) break;
      Util.sleep(500);
    }

    System.out.println(
        "c1 received "
            + r1.getNum()
            + " msgs, "
            + getNumberOfRetransmissions(c1)
            + " retransmissions");
    System.out.println(
        "c2 received "
            + r2.getNum()
            + " msgs, "
            + getNumberOfRetransmissions(c2)
            + " retransmissions");

    assert r1.getNum() == NUM * 2 : "expected " + NUM * 2 + ", but got " + r1.getNum();
    assert r2.getNum() == NUM * 2 : "expected " + NUM * 2 + ", but got " + r2.getNum();
  }
예제 #24
0
 private static void sendAndCheck(JChannel channel, Address dest, int num, MyReceiver receiver)
     throws Exception {
   receiver.clear();
   for (int i = 1; i <= num; i++) channel.send(dest, "m" + i);
   List<Message> list = receiver.getMessages();
   for (int i = 0; i < 20; i++) {
     if (list.size() == num) break;
     Util.sleep(500);
   }
   System.out.println("list = " + print(list));
   int size = list.size();
   assert size == num : "list has " + size + " elements: " + list;
 }
예제 #25
0
 /**
  * Send State (content on WhiteBoard) to all members of Group
  *
  * @param copy
  */
 protected void sendOwnState(final Map<Point, Color> copy) {
   if (copy == null) return;
   for (Point point : copy.keySet()) {
     // we don't need the color: it is our draw_color anyway
     DrawCommand comm = new DrawCommand(DrawCommand.DRAW, point.x, point.y, drawColor.getRGB());
     try {
       byte[] buf = Util.streamableToByteBuffer(comm);
       if (use_unicasts) sendToAll(buf);
       else channel.send(new Message(null, buf));
     } catch (Exception ex) {
       System.err.println(ex);
     }
   }
 }
 private void updateMembership() throws MembershipUpdateFailedException {
   try {
     if (channel.isConnected()) {
       channel.send(
           new Message(
                   null,
                   new JoinMessage(currentLoadFactor, new HashSet<String>(supportedCommandTypes)))
               .setFlag(Message.Flag.RSVP));
     }
   } catch (Exception e) {
     throw new MembershipUpdateFailedException(
         "Failed to dispatch Join message to Distributed Command Bus Members", e);
   }
 }
예제 #27
0
 public void run() {
   try {
     latch.await();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   for (int i = 0; i < NUM_MSGS; i++) {
     try {
       Message msg = new Message(dest, null, buf);
       ch.send(msg);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
 @Override
 public <R> void send(
     String routingKey, CommandMessage<?> commandMessage, CommandCallback<R> callback)
     throws Exception {
   String destination =
       consistentHash.getMember(routingKey, commandMessage.getPayloadType().getName());
   if (destination == null) {
     throw new CommandDispatchException(
         "No node known to accept " + commandMessage.getPayloadType().getName());
   }
   Address dest = getAddress(destination);
   callbacks.put(
       commandMessage.getIdentifier(), new MemberAwareCommandCallback<R>(dest, callback));
   channel.send(dest, new DispatchMessage(commandMessage, serializer, true));
 }
예제 #29
0
  public void testRegularAndOOBUnicasts2() throws Exception {
    DISCARD discard = new DISCARD();
    ProtocolStack stack = c1.getProtocolStack();
    stack.insertProtocol(discard, ProtocolStack.BELOW, UNICAST.class, UNICAST2.class);

    Address dest = c2.getAddress();
    Message m1 = new Message(dest, null, 1);
    Message m2 = new Message(dest, null, 2);
    m2.setFlag(Message.OOB);
    Message m3 = new Message(dest, null, 3);
    m3.setFlag(Message.OOB);
    Message m4 = new Message(dest, null, 4);

    MyReceiver receiver = new MyReceiver("C2");
    c2.setReceiver(receiver);
    c1.send(m1);

    discard.setDropDownUnicasts(1);
    c1.send(m3);

    discard.setDropDownUnicasts(1);
    c1.send(m2);

    c1.send(m4);
    Util.sleep(1000); // sleep some time to receive all messages

    Collection<Integer> list = receiver.getMsgs();
    int count = 10;
    while (list.size() < 4 && --count > 0) {
      Util.sleep(500); // time for potential retransmission
      sendStableMessages(c1, c2);
    }
    log.info("list = " + list);
    assert list.size() == 4 : "list is " + list;
    assert list.contains(1) && list.contains(2) && list.contains(3) && list.contains(4);
  }
예제 #30
0
 /**
  * Every time a message gets broadcasted, make sure we update the cluster.
  *
  * @param o the message to broadcast.
  * @return The same message.
  */
 public BroadcastAction filter(Object originalMessage, Object o) {
   if (o instanceof String) {
     String message = (String) o;
     // Avoid re-broadcasting
     if (!receivedMessages.remove(message)) {
       try {
         jchannel.send(new Message(null, null, message));
       } catch (ChannelException e) {
         logger.log(Level.WARNING, "", e);
       }
     }
     return new BroadcastAction(message);
   } else {
     return new BroadcastAction(o);
   }
 }