示例#1
0
  @SuppressWarnings("serial")
  private void testLinker(int addressCount, int messageCount) {
    server.setMaxConnections(addressCount);
    Linker linker = new Linker();
    linker.start(reactor);
    for (int i : range(addressCount)) {
      for (int j : range(messageCount)) {
        Sender snd = linker.sender(server.address(i), null);
        assertEquals("sender per address", i + 1, linker.sendersSize());
        final int fi = i, fj = j;
        snd.send(dict("i", fi, "j", fj));
      }
    }
    linker.close();
    reactor.run();

    HashMap<Integer, ArrayList<Map<?, ?>>> by_addr = new HashMap<>();
    for (int i : range(addressCount)) {
      by_addr.put(i, new ArrayList<Map<?, ?>>());
    }
    ArrayList<Map<?, ?>> messagesDicts = sink.getMessagesDicts();
    for (Map<?, ?> m : messagesDicts) {
      assertNotNull("did not receive a dict", m);
      by_addr.get(m.get("i")).add(m);
    }
    for (int i : range(addressCount)) {
      ArrayList<Map<?, ?>> actual = by_addr.get(i);
      ArrayList<Map<?, ?>> expected = new ArrayList<>(messageCount);
      for (int j : range(messageCount)) {
        expected.add(dict("i", i, "j", j));
      }
      assertEquals(String.format("Messages for address %d", i), expected, actual);
    }
    assertEquals("total messages", addressCount * messageCount, messagesDicts.size());
  }
  /**
   * Asynchronously runs the Proton {@link Reactor} accepting and sending messages. Any other call
   * to this method on this {@link AmqpsIotHubConnection} will block until the {@link Reactor}
   * terminates and this {@link AmqpsIotHubConnection} closes. Normally, this method should only be
   * called once by the {@link #open()} method until the {@link AmqpsIotHubConnection} has been
   * closed.
   *
   * @throws IOException if the {@link AmqpsIotHubConnectionBaseHandler} has not been initialized.
   * @throws InterruptedException if there is a problem acquiring the semaphore.
   */
  private synchronized void startReactorAsync() throws IOException, InterruptedException {
    // Acquire permit to continue execution of this method and spawn a new thread.
    reactorSemaphore.acquire();

    if (this.amqpsHandler != null) {
      this.reactor = Proton.reactor(this);

      new Thread(
              () -> {
                try {
                  reactor.run();

                  // Closing here should be okay. The reactor will only stop running if the
                  // connection is remotely
                  // or locally closed. The transport will attempt to receive/send a message and
                  // will get an exception
                  // causing it to create a new AmqpsIoTHubConnection.
                  close();

                  // Release the semaphore and make permit available allowing for the next reactor
                  // thread to spawn.
                  reactorSemaphore.release();
                } catch (Exception e) {
                  close();
                  reactorSemaphore.release();
                }
              })
          .start();
    } else {
      throw new IOException(
          "The Handler has not been initialized. Ensure that the AmqpsIotHubConnection is in an OPEN state by calling open().");
    }
  }
示例#3
0
  public static void main(String[] args) throws IOException {
    try {
      int port = Integer.valueOf(args[1]);
      int numMsgs = Integer.valueOf(args[2]);
      boolean result = false;

      if ("send".equalsIgnoreCase(args[0])) {
        Send send = new Send("localhost", port, numMsgs);
        Reactor r = Proton.reactor(send);
        r.run();
        result = send.getResult();
      } else {
        Reactor r = Proton.reactor(new Recv(port, numMsgs));
        r.run();
        result = true;
      }
      System.exit(result ? 0 : 1);
    } catch (Throwable t) {
      t.printStackTrace();
      System.exit(1);
    }
  }
示例#4
0
 private void testSender(int count) {
   ArrayList<String> expected = new ArrayList<String>(count);
   for (int i = 0; i < count; i++) {
     expected.add(template.render(i));
   }
   server.setMaxConnections(1);
   Sender snd = Sender.Builder().withTarget(server.address()).create();
   reactor.getHandler().add(snd);
   snd.start(reactor);
   for (String msg : expected) {
     snd.send(msg);
   }
   snd.close();
   reactor.run();
   assertEquals("messages received", expected, sink.getMessages());
 }
示例#5
-1
  private void testSampler(final int count, float frequency) {
    server.setMaxConnections(1);
    final ArrayList<String> expected = new ArrayList<String>(count);
    Timeout gen =
        new Timeout() {
          Tag tag = new SimpleTag(0);
          int sent = 0;

          @Override
          public void onSample(DatawireEvent e) {
            String body = template.render(sent);
            expected.add(body);
            Message message = Message.Factory.create();
            message.setBody(new AmqpValue(body));
            DatawireUtils.send(e.getLink(), tag, message);
            sent += 1;
            if (sent >= count) {
              e.getLink().close();
              cancel();
            }
          }
        };
    Sender snd =
        Sender.Builder()
            .withTarget(server.address())
            .withHandlers(new Sampler(gen, frequency))
            .create();
    reactor.getHandler().add(snd);
    gen.setTimeout(reactor, 2000);
    snd.start(reactor);
    reactor.run();
    assertTrue("Sampling timed out", gen.isCancelled());
    assertEquals("Expected messages", expected, sink.getMessages());
  }