예제 #1
0
  public AsyncResult<Exception> connect() {
    this.status = AMBStatus.CONNECTING;
    final AsyncResultImpl<Exception> res = new AsyncResultImpl<Exception>();

    try {

      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost(host);
      if (port != 0) factory.setPort(port);
      connection = factory.newConnection();
      channel = connection.createChannel();
      status = AMBStatus.CONNECTED;
      onconnect.onconnect(this);
      res.completed(null);

      channel.queueDeclare(id, false, false, false, null);
      channel.basicConsume(
          id,
          new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(
                String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                throws IOException {

              MessageImpl msg;
              try {
                msg =
                    (MessageImpl)
                        new ObjectInputStream(new ByteArrayInputStream(body)).readObject();

                if (onmessage != null) onmessage.onmessage(AMBRabbit.this, msg);
                channel.basicAck(envelope.getDeliveryTag(), false);
              } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
            }
          });

    } catch (Exception e) {
      status = AMBStatus.DISCONNECTED;
      res.completed(e);
    }

    return res;
  }
예제 #2
0
  @Test
  public void testBase() throws Exception {
    AMB amb = AMBBuilder.builder().seed("rabbitmq://localhost:5672").build();

    String testValue = UID.randomUID();
    final AsyncResultImpl<String> res = new AsyncResultImpl<String>();

    amb.setCallback(
        new OnMessage() {

          public void onmessage(AMB amb, Message message) {
            System.out.println(message.string());
            res.completed(message.string());
          }
        });

    amb.connect().get();
    amb.message().to(amb.id()).data(testValue).send();

    Assert.assertEquals(testValue, res.get());

    amb.disconnect();
  }