// bug 26552
 public void testServerNamedTransientAutoDeleteQueueAndBindingRecovery()
     throws IOException, InterruptedException, TimeoutException {
   String x = "tmp-fanout";
   Channel ch = connection.createChannel();
   ch.exchangeDelete(x);
   ch.exchangeDeclare(x, "fanout");
   String q = ch.queueDeclare("", false, false, true, null).getQueue();
   final AtomicReference<String> nameBefore = new AtomicReference<String>(q);
   final AtomicReference<String> nameAfter = new AtomicReference<String>();
   final CountDownLatch listenerLatch = new CountDownLatch(1);
   ((AutorecoveringConnection) connection)
       .addQueueRecoveryListener(
           new QueueRecoveryListener() {
             @Override
             public void queueRecovered(String oldName, String newName) {
               nameBefore.set(oldName);
               nameAfter.set(newName);
               listenerLatch.countDown();
             }
           });
   ch.queueBind(nameBefore.get(), x, "");
   restartPrimaryAndWaitForRecovery();
   expectChannelRecovery(ch);
   ch.confirmSelect();
   ch.exchangeDeclare(x, "fanout");
   ch.basicPublish(x, "", null, "msg".getBytes());
   waitForConfirms(ch);
   AMQP.Queue.DeclareOk ok = ch.queueDeclarePassive(nameAfter.get());
   assertEquals(1, ok.getMessageCount());
   ch.queueDelete(nameAfter.get());
   ch.exchangeDelete(x);
 }
  @Override
  public String amqpRegister(User user) throws Exception {

    try {
      Channel rabbitMqChannel = rabbitMqConnection.createChannel();

      // create or get user queue
      AMQP.Queue.DeclareOk queue =
          rabbitMqChannel.queueDeclare(
              "USER_".concat(String.valueOf(user.hashCode())),
              true, // durable
              false, // exclusive
              false, // auto delete
              null // no arguments
              );

      Map<String, Object> bindParams = new HashMap<>();
      bindParams.put(ARGUMENT_TO_ID, user.getId());
      rabbitMqChannel.queueBind(queue.getQueue(), amqpExchange, "", bindParams);
      channelListener.registerChannel(rabbitMqChannel, queue.getQueue(), false);

      // add user channel to active channels
      USER_CHANNEL.put(user, rabbitMqChannel);
      return queue.getQueue();

    } catch (Exception e) {
      logger.info("### Couldn't create user queue .. -> {" + e.getMessage() + "}");
      throw e;
    }
  }
 @Override
 public Destination createTemporaryDestination() {
   try {
     AMQP.Queue.DeclareOk ok = channel.queueDeclare();
     return new AMQPDestination(ok.getQueue(), channel, "", true);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #4
0
  public static IPersistentMap mapFrom(AMQP.Queue.DeclareOk method) {
    Map m = new HashMap();
    m.put(RT.keyword(null, "queue"), method.getQueue());
    m.put(RT.keyword(null, "message-count"), method.getMessageCount());
    m.put(RT.keyword(null, "consumer-count"), method.getConsumerCount());
    m.put(RT.keyword(null, "message_count"), method.getMessageCount());
    m.put(RT.keyword(null, "consumer_count"), method.getConsumerCount());

    return PersistentHashMap.create(m);
  }
 private void expectQueueRecovery(Channel ch, String q)
     throws IOException, InterruptedException, TimeoutException {
   ch.confirmSelect();
   ch.queuePurge(q);
   AMQP.Queue.DeclareOk ok1 = declareClientNamedQueue(ch, q);
   assertEquals(0, ok1.getMessageCount());
   ch.basicPublish("", q, null, "msg".getBytes());
   waitForConfirms(ch);
   AMQP.Queue.DeclareOk ok2 = declareClientNamedQueue(ch, q);
   assertEquals(1, ok2.getMessageCount());
 }
 public AMQP.Queue.DeclareOk queueDeclare(
     String queue,
     boolean durable,
     boolean exclusive,
     boolean autoDelete,
     Map<String, Object> arguments)
     throws IOException {
   final AMQP.Queue.DeclareOk ok =
       delegate.queueDeclare(queue, durable, exclusive, autoDelete, arguments);
   RecordedQueue q =
       new RecordedQueue(this, ok.getQueue())
           .durable(durable)
           .exclusive(exclusive)
           .autoDelete(autoDelete)
           .arguments(arguments);
   if (queue.equals(RecordedQueue.EMPTY_STRING)) {
     q.serverNamed(true);
   }
   recordQueue(ok, q);
   return ok;
 }
 // bug 26552
 public void testClientNamedTransientAutoDeleteQueueAndBindingRecovery()
     throws IOException, InterruptedException, TimeoutException {
   String q = UUID.randomUUID().toString();
   String x = "tmp-fanout";
   Channel ch = connection.createChannel();
   ch.queueDelete(q);
   ch.exchangeDelete(x);
   ch.exchangeDeclare(x, "fanout");
   ch.queueDeclare(q, false, false, true, null);
   ch.queueBind(q, x, "");
   restartPrimaryAndWaitForRecovery();
   expectChannelRecovery(ch);
   ch.confirmSelect();
   ch.queuePurge(q);
   ch.exchangeDeclare(x, "fanout");
   ch.basicPublish(x, "", null, "msg".getBytes());
   waitForConfirms(ch);
   AMQP.Queue.DeclareOk ok = ch.queueDeclare(q, false, false, true, null);
   assertEquals(1, ok.getMessageCount());
   ch.queueDelete(q);
   ch.exchangeDelete(x);
 }
Example #8
0
  @Override
  public void run() {
    while (true) {
      Channel ch = null;
      try {
        ch = RabbitConnection.getInstance().newChanel();
        ch.basicQos(1);
        AMQP.Queue.DeclareOk q = ch.queueDeclare();
        ch.queueBind(q.getQueue(), "amq.direct", routingKey);
        QueueingConsumer consumer = new QueueingConsumer(ch);
        ch.basicConsume(q.getQueue(), true, consumer);

        // Process deliveries
        while (true) {
          QueueingConsumer.Delivery delivery = consumer.nextDelivery();

          String message = new String(delivery.getBody());

          Message msg = handler.obtainMessage();
          Bundle bundle = new Bundle();

          bundle.putString("msg", message);
          msg.setData(bundle);
          handler.sendMessage(msg);
        }
      } catch (InterruptedException e) {
        break;
      } catch (Exception e1) {
        try {
          Thread.sleep(INTERVAL_RECOVERY); // sleep and then try again
        } catch (InterruptedException e) {
          break;
        }
      } finally {
        RabbitConnection.getInstance().closeChanel(ch);
      }
    }
  }
Example #9
0
  public static void t2() throws Exception {
    Channel channel = generateChannel();
    AMQP.Queue.DeclareOk declareOk1 = channel.queueDeclare();
    AMQP.Queue.DeclareOk declareOk2 = channel.queueDeclare();
    AMQP.Queue.DeclareOk declareOk3 = channel.queueDeclare();

    String exchange = "amq.rabbitmq.log";
    channel.queueBind(declareOk1.getQueue(), exchange, "error");
    channel.queueBind(declareOk2.getQueue(), exchange, "waring");
    channel.queueBind(declareOk3.getQueue(), exchange, "info");

    QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
    channel.basicConsume(declareOk1.getQueue(), false, queueingConsumer);
    channel.basicConsume(declareOk2.getQueue(), false, queueingConsumer);
    channel.basicConsume(declareOk3.getQueue(), false, queueingConsumer);

    while (true) {
      QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
      System.out.println("log: " + new String(delivery.getBody()));
      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }
  public void run() {

    try {
      final Connection conn = MQ.createConnection();
      final Channel channel = MQ.createChannel(conn);
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

      System.out.println(String.format("Subscribbed to topic '%s'. Exit with ENTER", MONITOR_KEY));

      // create a queue for this program
      final AMQP.Queue.DeclareOk queue = channel.queueDeclare();
      System.out.println("Create queue " + queue.getQueue());

      // bind the queue to all routing keys that match MONITOR_KEY
      channel.queueBind(queue.getQueue(), Config.EXCHANGE_NAME, MONITOR_KEY);
      System.out.println(
          String.format(
              "Bound queue %s to all keys that match '%s' on exchange %s",
              queue.getQueue(), MONITOR_KEY, Config.EXCHANGE_NAME));

      boolean noAck = false;

      // ask the server to notify us of new message and do not send ack message automatically
      // WARNING: This code is called from the thread that does the communication with the
      // server so sufficient locking is required. Also do not use any blocking calls to
      // the server such as queueDeclare, txCommit, or basicCancel. Basicly only "basicAck"
      // should be called here!!!
      channel.basicConsume(
          queue.getQueue(),
          noAck,
          new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(
                String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                throws IOException {
              // get the delivery tag to ack that we processed the message successfully
              long deliveryTag = envelope.getDeliveryTag();

              // properties.getTimestamp() contains the timestamp
              // that the sender added when the message was published. This
              // time is the time on the sender and NOT the time on the
              // AMQP server. This implies that clients are possibly out of
              // sync!
              System.out.println(
                  String.format(
                      "@%d: %s -> %s",
                      properties.getTimestamp().getTime(),
                      envelope.getRoutingKey(),
                      new String(body)));

              mp.parse(envelope.getRoutingKey(), new String(body));

              System.out.println(envelope.getRoutingKey());
              System.out.println("bericht: " + new String(body));

              // send an ack to the server so it can remove the message from
              // the queue.
              channel.basicAck(deliveryTag, false);
            }

            //				private void parse(String routingKey, String body) {
            //					System.out.println(routingKey);
            //					if(!body.contains("start")){
            //					Robot robot_temp = controller.getRobotFromIdentifier(routingKey.split("_")[1]);
            //					if(robot_temp!=null)
            //					robot_temp.setPose(0.0, (int)Double.parseDouble(body.split("_")[0]),
            // (int)Double.parseDouble(body.split("_")[1]));
            //					}
            //				}
          });

      // wait here until a newline is entered
      stdin.readLine();

      channel.close();
      conn.close();
    } catch (IOException e) {
      e.printStackTrace(System.err);
    }
  }
Example #11
0
 public String protocolMethodName() {
   return method.protocolMethodName();
 }
Example #12
0
 public int protocolMethodId() {
   return method.protocolMethodId();
 }
Example #13
0
 public int protocolClassId() {
   return method.protocolClassId();
 }
Example #14
0
 public String getQueue() {
   return method.getQueue();
 }
Example #15
0
 public int getMessageCount() {
   return method.getMessageCount();
 }
Example #16
0
 public int getConsumerCount() {
   return method.getConsumerCount();
 }