Package com.rabbitmq.client

Examples of com.rabbitmq.client.Channel


            //  Connect to the AMQP broker
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(hostName);
            factory.setPort(portNumber);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            //  Establish the PUB/SUB wiring
            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, "PUBSUB", null);
            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            for (;;) {

                //  Get next request
                String msg = consumer.nextDelivery().getBody().toString();
View Full Code Here


            //  Connect to the AMQP broker
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(hostName);
            factory.setPort(portNumber);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            for (;;) {

                //  Send a message.
                channel.basicPublish("PUBSUB", null, null,
                    "Hello, World???".getBytes());

                //  Sleep for one second.
                Thread.sleep (1000);
            }
View Full Code Here

  }

  private <T> T doExecute(ChannelCallback<T> action) {
    Assert.notNull(action, "Callback object must not be null");
    RabbitResourceHolder resourceHolder = getTransactionalResourceHolder();
    Channel channel = resourceHolder.getChannel();
    if (this.confirmCallback != null || this.returnCallback != null) {
      addListener(channel);
    }
    try {
      if (logger.isDebugEnabled()) {
View Full Code Here

  private static final String EXCHANGE_NAME = "header_test";

  public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
   
      if (argv.length < 1){
        System.err.println("Usage: EmitLogHeader message queueName [headers]...");
        System.exit(1);
      }

      // The API requires a routing key, but in fact if you are using a header exchange the
      // value of the routing key is not used in the routing. You can store information
      // for the receiver here as the routing key is still available in the received message.
      String routingKey = "ourTestRoutingKey";

      // Argument processing: the first arg is the message, the rest are
      // key value pairs for headers.
      String message = argv[0];

      // The map for the headers.
      Map<String, Object> headers = new HashMap<String, Object>();

      // The rest of the arguments are key value header pairs.  For the purpose of this
      // example, we are assuming they are all strings, but that is not required by RabbitMQ
      for(int i = 1; i < argv.length; i++)  {
        System.out.println("Adding header " + argv[i] + " with value " + argv[i + 1] + " to Map");
        headers.put(argv[i], argv[i+1]);
        i++;
      }

      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
 
      connection = factory.newConnection();
      channel = connection.createChannel();

      channel.exchangeDeclare(EXCHANGE_NAME, "headers");

      AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();

      // MessageProperties.PERSISTENT_TEXT_PLAIN is a static instance of AMQP.BasicProperties
      // that contains a delivery mode and a priority. So we pass them to the builder.
      builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());   
      builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());   

      // Add the headers to the builder.
      builder.headers(headers);

      // Use the builder to create the BasicProperties object.
      AMQP.BasicProperties theProps = builder.build();

      // Now we add the headers.  This example only uses string headers, but they can also be integers
      channel.basicPublish(EXCHANGE_NAME, routingKey, theProps, message.getBytes());
      System.out.println(" [x] Sent message: '" + message + "'");

    }
    catch  (Exception e) {
      e.printStackTrace();
View Full Code Here

  public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
   
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
   
    channel.basicQos(1);
   
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
   
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
     
      System.out.println(" [x] Received '" + message + "'");
      doWork(message);
      System.out.println(" [x] Done");

      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }        
  }
View Full Code Here

  public static void main(String[] argv) throws Exception {

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

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");

    String severity = getSeverity(argv);
    String message = getMessage(argv);

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

    channel.close();
    connection.close();
  }
View Full Code Here

  public static void main(String[] argv) throws Exception {

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

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");
    String queueName = channel.queueDeclare().getQueue();
   
    if (argv.length < 1){
      System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
      System.exit(1);
    }
   
    for(String severity : argv){   
      channel.queueBind(queueName, EXCHANGE_NAME, severity);
    }
   
    System.out.println(" [*] Waiting for messages. 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

  public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
   
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
   
    String message = getMessage(argv);
   
    channel.basicPublish( "", TASK_QUEUE_NAME,
                MessageProperties.PERSISTENT_TEXT_PLAIN,
                message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
   
    channel.close();
    connection.close();
  }
View Full Code Here

    return fib(n-1) + fib(n-2);
  }
   
  public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
 
      connection = factory.newConnection();
      channel = connection.createChannel();
     
      channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
 
      channel.basicQos(1);
 
      QueueingConsumer consumer = new QueueingConsumer(channel);
      channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
 
      System.out.println(" [x] Awaiting RPC requests");
 
      while (true) {
        String response = null;
       
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
       
        BasicProperties props = delivery.getProperties();
        BasicProperties replyProps = new BasicProperties
                                         .Builder()
                                         .correlationId(props.getCorrelationId())
                                         .build();
       
        try {
          String message = new String(delivery.getBody(),"UTF-8");
          int n = Integer.parseInt(message);
 
          System.out.println(" [.] fib(" + message + ")");
          response = "" + fib(n);
        }
        catch (Exception e){
          System.out.println(" [.] " + e.toString());
          response = "";
        }
        finally
          channel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
 
          channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
      }
    }
    catch  (Exception e) {
      e.printStackTrace();
View Full Code Here

  public static void main(String[] argv) throws Exception {

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

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

    String message = getMessage(argv);

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

    channel.close();
    connection.close();
  }
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.