public void testGetInstance() {
   Context context1 = new MockContext(mMockApplication);
   Client c = Proton.getInjector(context1).getInstance(Client.class);
   assertNotNull(c);
   assertEquals(
       c.contextScopedClass1, Proton.getInjector(context1).getInstance(ContextScopedClass.class));
   assertNotSame(
       c.contextScopedClass1,
       Proton.getInjector(new MockContext(mMockApplication))
           .getInstance(ContextScopedClass.class));
 }
  public void testInject() {
    Context context1 = new MockContext(mMockApplication);
    Client obj1 = Proton.getInjector(context1).inject(new Client());
    assertNotNull(obj1.contextScopedClass1);
    assertEquals(obj1.contextScopedClass1, obj1.contextScopedClass2);
    assertNotNull(obj1.contextScopedClassProvider1);
    assertEquals(obj1.contextScopedClassProvider1, obj1.contextScopedClassProvider2);

    Client obj2 = Proton.getInjector(context1).inject(new Client());
    assertEquals(obj1.contextScopedClass1, obj2.contextScopedClass2);
    assertEquals(obj1.contextScopedClassProvider1, obj2.contextScopedClassProvider2);

    Client obj3 = Proton.getInjector(new MockContext(mMockApplication)).inject(new Client());
    assertNotSame(obj1.contextScopedClass1, obj3.contextScopedClass1);
    assertNotSame(obj1.contextScopedClassProvider1, obj3.contextScopedClassProvider1);
  }
Exemplo n.º 3
0
 @Override
 public void onLinkFlow(Event event) {
   Sender snd = (Sender) event.getLink();
   if (snd.getCredit() > 0 && snd.getLocalState() != EndpointState.CLOSED) {
     Message message = Proton.message();
     ++count;
     message.setBody(new AmqpValue("message-" + count));
     byte[] msgData = new byte[1024];
     int length;
     while (true) {
       try {
         length = message.encode(msgData, 0, msgData.length);
         break;
       } catch (BufferOverflowException e) {
         msgData = new byte[msgData.length * 2];
       }
     }
     byte[] tag = String.valueOf(count).getBytes();
     Delivery dlv = snd.delivery(tag);
     snd.send(msgData, 0, length);
     dlv.settle();
     snd.advance();
     if (count == numMsgs) {
       snd.close();
       snd.getSession().close();
       snd.getSession().getConnection().close();
       result = true;
     }
   }
 }
Exemplo n.º 4
0
    @Override
    public void onDelivery(Event event) {
      Receiver recv = (Receiver) event.getLink();
      Delivery delivery = recv.current();
      if (delivery.isReadable() && !delivery.isPartial()) {
        int size = delivery.pending();
        byte[] buffer = new byte[size];
        int read = recv.recv(buffer, 0, buffer.length);
        recv.advance();

        Message msg = Proton.message();
        msg.decode(buffer, 0, read);

        ++count;
        String msgBody = ((AmqpValue) msg.getBody()).getValue().toString();
        String expected = "message-" + count;
        if (!expected.equals(msgBody)) {
          throw new RuntimeException(
              "Received message body '" + msgBody + "', expected: '" + expected + "'");
        }

        if (count == numMsgs) {
          recv.close();
          recv.getSession().close();
          recv.getSession().getConnection().close();
          acceptor.close();
        }
      }
    }
  /**
   * 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().");
    }
  }
Exemplo n.º 6
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);
    }
  }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   mMockApplication = new MockApplication();
   Proton.initialize(
       mMockApplication,
       new DefaultModule() {
         @Override
         protected void configure() {
           super.configure();
           bind(Client.class);
           bind(ContextScopedClass.class);
         }
       });
 }
 @Override
 protected void tearDown() throws Exception {
   Proton.destroy();
   super.tearDown();
 }