@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;
    }
  }
Пример #2
0
 @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);
   }
 }
Пример #3
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);
  }
Пример #4
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);
      }
    }
  }
 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;
 }
Пример #6
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);
    }
  }
Пример #8
0
 public String getQueue() {
   return method.getQueue();
 }