Package com.rabbitmq.client

Examples of com.rabbitmq.client.Channel


    Connection connection = null;
    try {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost(_mqServer);
      connection = factory.newConnection();
      Channel channel = connection.createChannel();

      channel.exchangeDeclare(EXCHANGE_NAME, "topic");
      String queueName = channel.queueDeclare().getQueue();

      String bindingKey = _partition;
      channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

      System.out.println(" [*] " + _consumerId + " Waiting for messages on " + bindingKey
          + ". To exit press CTRL+C");

      QueueingConsumer consumer = new QueueingConsumer(channel);
      channel.basicConsume(queueName, true, consumer);

      while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();
View Full Code Here


    System.out.println("Sending " + count + " messages with random topic id");

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(mqServer);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    for (int i = 0; i < count; i++) {
      int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER);
      String routingKey = "topic_" + rand;
      String message = "message_" + rand;

      channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
      System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

      Thread.sleep(1000);
    }

View Full Code Here

        ConnectionFactory cfconn = new ConnectionFactory();
        cfconn.setHost("localhost");
        cfconn.setPort(AMQP.PROTOCOL.PORT);
        Connection conn = cfconn.newConnection();

        Channel ch = conn.createChannel();
        ch.exchangeDeclare("elasticsearch", "direct", true);
        ch.queueDeclare("elasticsearch", true, false, false, null);

        String message = "{ \"index\" : { \"index\" : \"test\", \"type\" : \"type1\", \"id\" : \"1\" }\n" +
                "{ \"type1\" : { \"field1\" : \"value1\" } }\n" +
                "{ \"delete\" : { \"index\" : \"test\", \"type\" : \"type1\", \"id\" : \"2\" } }\n" +
                "{ \"create\" : { \"index\" : \"test\", \"type\" : \"type1\", \"id\" : \"1\" }\n" +
                "{ \"type1\" : { \"field1\" : \"value1\" } }";

        ch.basicPublish("elasticsearch", "elasticsearch", null, message.getBytes());

        ch.close();
        conn.close();

        Thread.sleep(100000);
    }
View Full Code Here

   * Gets the task channel.
   *
   * @return the task channel
   */
  protected Channel createChannel() {
    Channel channel = null;

    int attempts = 0;
    while (true) {
      attempts++;
      Logger.info("Attempting to connect to queue: attempt " + attempts);
View Full Code Here

    // Log Debug
    Logger.info("Initializing connections to RabbitMQ instance (%s:%s), Queue: %s", RabbitMQPlugin.getHost(), RabbitMQPlugin.getPort(), queue);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
      int prefetchCount = 1;
      channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
      // Add to the number of retries
      attempts++;

      // Log Debug
      Logger.debug("Retry " + attempts);

      // Get Next Delivery Message
      try {
        // http://www.rabbitmq.com/api-guide.html
        // channel.exchangeDeclare(exchangeName, "direct", true);
        // String queueName = channel.queueDeclare().getQueue();
        // channel.queueBind(queueName, exchangeName, routingKey);
       
        channel.exchangeDeclare(queue, plugin.getExchangeType(), true);
        channel.queueDeclare(queue, plugin.isDurable(), false, false, null);
        channel.queueBind(queue, queue, routingKey);

        // Log Debug
        Logger.info("RabbitMQ Task Channel Available: " + channel);

        // Return Channel
View Full Code Here

        if (!channels.containsKey(connection.getName())) {
          if (logger.isLoggable(BasicLevel.DEBUG)) {
            logger.log(BasicLevel.DEBUG, connection.getName() + ": New channel available for distribution.");
          }
          try {
            Channel chan = connection.getConnection().createChannel();
            chan.queueDeclarePassive(amqpQueue);
            channels.put(connection.getName(), chan);
          } catch (IOException exc) {
            if (logger.isLoggable(BasicLevel.DEBUG)) {
              logger.log(BasicLevel.DEBUG, "Channel is not usable.", exc);
            }
          }
        }
      }
      lastUpdate = now;
    }

    // Send the message
    Iterator<Map.Entry<String, Channel>> iter = channels.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, Channel> entry = iter.next();
      try {
        Channel chan = entry.getValue();
        String cnxName = entry.getKey();
        if (!chan.isOpen()) {
          iter.remove();
          continue;
        }
        if (connectionNames != null && !connectionNames.contains(cnxName)) {
          continue;
        }
        if (logger.isLoggable(BasicLevel.DEBUG)) {
          logger.log(BasicLevel.DEBUG, "Sending message on " + cnxName);
        }
        chan.basicPublish("", amqpQueue, props, message.body);
        channels.get(cnxName); // Access the used connection to update the LRU map
        return;
      } catch (IOException exc) {
        if (logger.isLoggable(BasicLevel.DEBUG)) {
          logger.log(BasicLevel.DEBUG, "Channel is not usable, remove from table.", exc);
View Full Code Here

        if (logger.isLoggable(BasicLevel.DEBUG)) {
          logger.log(BasicLevel.DEBUG, "Creating a new consumer on queue " + amqpQueue + " for connection "
              + connection.getName());
        }
        try {
          Channel chan = connection.getConnection().createChannel();
          chan.queueDeclarePassive(amqpQueue);
          AmqpConsumer consumer = new AmqpConsumer(chan, connection.getName());
          chan.basicConsume(amqpQueue, false, consumer);
          channels.add(chan);
        } catch (Exception e) {
          logger.log(BasicLevel.ERROR,
              "Error while starting consumer on connection: " + connection.getName(), e);
        }
View Full Code Here

    public void Subscribe(AMQPRoutingKey topic) throws AMQPException {
        if (callback != null) {
            try {
                Connection connection = connectionFactory.newConnection();

                Channel channel = connection.createChannel();
                channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

                String queueName = channel.queueDeclare().getQueue();
                channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_TOPIC, topic.getNativeKey());

                QueueingConsumer consumer = new QueueingConsumer(channel);
                channel.basicConsume(queueName, true, consumer);

                while (true) {
                    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                    String message = new String(delivery.getBody());
View Full Code Here

    public void Send(OMElement message) throws AMQPException {
        try {
            if (isRoutable(message)) {
                Connection connection = connectionFactory.newConnection();
                Channel channel = connection.createChannel();
                channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT);

                channel.basicPublish(AMQPUtil.EXCHANGE_NAME_FANOUT, "", null, message.toString().getBytes());

                channel.close();
                connection.close();
            }
        } catch (IOException e) {
            throw new AMQPException(e);
        }
View Full Code Here

    public void Send(OMElement message) throws AMQPException {
        try {
            if (isRoutable(message)) {
                Connection connection = connectionFactory.newConnection();
                Channel channel = connection.createChannel();
                channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

                List<String> routingKeys = new ArrayList<String>();
                getRoutingKeys(message, routingKeys);

                for (String routingKey : routingKeys) {
                    channel.basicPublish(
                            AMQPUtil.EXCHANGE_NAME_TOPIC, routingKey, null, message.toString().getBytes());
                }

                channel.close();
                connection.close();
            }
        } catch (IOException e) {
            throw new AMQPException(e);
        }
View Full Code Here

TOP

Related Classes of com.rabbitmq.client.Channel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.