Exemple #1
0
  private void send(
      final Address dest, final int num_msgs, final int num_threads, final double oob_prob)
      throws Exception {
    if (num_threads <= 0) throw new IllegalArgumentException("number of threads <= 0");

    if (num_msgs % num_threads != 0)
      throw new IllegalArgumentException(
          "number of messages ( "
              + num_msgs
              + ") needs to be divisible by "
              + "the number o threads ("
              + num_threads
              + ")");

    if (num_threads > 1) {
      final int msgs_per_thread = num_msgs / num_threads;
      Thread[] threads = new Thread[num_threads];
      final AtomicInteger counter = new AtomicInteger(0);
      for (int i = 0; i < threads.length; i++) {
        threads[i] =
            new Thread() {
              public void run() {
                for (int j = 0; j < msgs_per_thread; j++) {
                  Channel sender = Util.tossWeightedCoin(0.5) ? a : b;
                  boolean oob = Util.tossWeightedCoin(oob_prob);
                  int num = counter.incrementAndGet();
                  Message msg = new Message(dest, null, num);
                  if (oob) msg.setFlag(Message.OOB);
                  try {
                    sender.send(msg);
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
              }
            };
        threads[i].start();
      }
      for (int i = 0; i < threads.length; i++) {
        threads[i].join(20000);
      }
      return;
    }

    for (int i = 0; i < num_msgs; i++) {
      Channel sender = Util.tossWeightedCoin(0.5) ? a : b;
      boolean oob = Util.tossWeightedCoin(oob_prob);
      Message msg = new Message(dest, null, i);
      if (oob) msg.setFlag(Message.OOB);
      sender.send(msg);
    }
  }
  @Override
  public void notify(ChangeSet changeSet) {
    if (changeSet == null) {
      return; // do nothing
    }
    if (!isOpen.get()) {
      // The channel is not open ...
      return;
    }
    if (!multipleAddressesInCluster.get()) {
      // We are in clustered mode, but there is only one participant in the cluster (us).
      // So short-circuit the cluster and just notify the local observers ...
      if (hasObservers()) {
        delegate.notify(changeSet);
        logReceivedOperation(changeSet);
      }
      return;
    }

    // There are multiple participants in the cluster, so send all changes out to JGroups,
    // letting JGroups do the ordering of messages...
    try {
      logSendOperation(changeSet);
      byte[] data = serialize(changeSet);
      Message message = new Message(null, null, data);
      channel.send(message);
    } catch (IllegalStateException e) {
      LOGGER.warn(
          BusI18n.unableToNotifyChanges,
          clusteringConfiguration.getClusterName(),
          changeSet.size(),
          changeSet.getWorkspaceName(),
          changeSet.getUserId(),
          changeSet.getProcessKey(),
          changeSet.getTimestamp());
    } catch (Exception e) {
      // Something went wrong here (this should not happen) ...
      String msg =
          BusI18n.errorSerializingChanges.text(
              clusteringConfiguration.getClusterName(),
              changeSet.size(),
              changeSet.getWorkspaceName(),
              changeSet.getUserId(),
              changeSet.getProcessKey(),
              changeSet.getTimestamp(),
              changeSet);
      throw new SystemFailureException(msg, e);
    }
  }
Exemple #3
0
  private static void send(
      Channel sender_channel, Address dest, boolean oob, boolean mixed, int num_msgs)
      throws Exception {
    long seqno = 1;
    for (int i = 0; i < num_msgs; i++) {
      Message msg = new Message(dest, null, seqno++);
      if (mixed) {
        if (i % 2 == 0) msg.setFlag(Message.OOB);
      } else if (oob) {
        msg.setFlag(Message.OOB);
      }

      sender_channel.send(msg);
    }
  }
Exemple #4
0
 public void receive(Message msg) {
   Message reply = new Message(msg.getSrc());
   try {
     System.out.println(
         "-- MySimpleReplier["
             + channel.getAddress()
             + "]: received message from "
             + msg.getSrc());
     if (handle_requests) {
       System.out.println(", sending reply");
       channel.send(reply);
     } else System.out.println("\n");
   } catch (Exception e) {
     e.printStackTrace();
   }
 }