Package com.rabbitmq.client

Examples of com.rabbitmq.client.Channel


  private static final String EXCHANGE_NAME = "topic_logs";

  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.exchangeDeclare(EXCHANGE_NAME, "topic");

      String routingKey = getRouting(argv);
      String message = getMessage(argv);

      channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
      System.out.println(" [x] Sent '" + routingKey + "':'" + 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(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
   
    channel.close();
    connection.close();
  }
View Full Code Here

  private static final String EXCHANGE_NAME = "topic_logs";

  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.exchangeDeclare(EXCHANGE_NAME, "topic");
      String queueName = channel.queueDeclare().getQueue();
      if (argv.length < 1){
        System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
        System.exit(1);
      }
   
      for(String bindingKey : argv){   
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
      }
   
      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(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
   
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);
   
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      System.out.println(" [x] Received '" + message + "'");
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: ReceiveLogsHeader queueName [headers]...");
        System.exit(1);
      }
   
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
 
      connection = factory.newConnection();
      channel = connection.createChannel();

      channel.exchangeDeclare(EXCHANGE_NAME, "headers");

      // 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 receive information
      // from the sender here as the routing key is still available in the received message.
      String routingKeyFromUser = "ourTestRoutingKey";
      // Argument processing: the first arg is the local queue name, the rest are
      // key value pairs for headers.
      String queueInputName = 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
      // Note that when you run this code you should include the x-match header on the command
      // line. Example:
      //    java -cp $CP ReceiveLogsHeader testQueue1  x-match any header1 value1
      for(int i = 1; i < argv.length; i++)  {
        headers.put(argv[i], argv[i+1]);
        System.out.println("Binding header " + argv[i] + " and value " + argv[i + 1] + " to queue " + queueInputName);
        i++;
      }

      String queueName = channel.queueDeclare(queueInputName, true, false, false, null).getQueue();
      channel.queueBind(queueName, EXCHANGE_NAME, routingKeyFromUser, headers);
   
      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 routingKeyFromSender = 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.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");
   
    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());

View Full Code Here

            ConnectionFactory connFactory = new ConnectionFactory();
            connFactory.setUri(uri);
            Connection conn = connFactory.newConnection();

            final Channel ch = conn.createChannel();

      if (exchange == null) {
    System.err.println("Please supply exchange name to send to (-e)");
    System.exit(2);
      }
      if (routingKey == null) {
    System.err.println("Please supply routing key to send to (-k)");
    System.exit(2);
      }
      ch.exchangeDeclare(exchange, exchangeType);

      for (String filename : cmd.getArgs()) {
    System.out.print("Sending " + filename + "...");
    File f = new File(filename);
    FileInputStream i = new FileInputStream(f);
    byte[] body = new byte[(int) f.length()];
    i.read(body);
    i.close();

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("filename", filename);
    headers.put("length", (int) f.length());
    BasicProperties props = new BasicProperties.Builder().headers(headers).build();
    ch.basicPublish(exchange, routingKey, props, body);
    System.out.println(" done.");
      }

      conn.close();
        } catch (Exception ex) {
View Full Code Here

            ConnectionFactory connFactory = new ConnectionFactory();
            connFactory.setUri(uri);
            Connection conn = connFactory.newConnection();

            final Channel ch = conn.createChannel();

            ch.queueDeclare(queueName, false, false, false, null);

            QueueingConsumer consumer = new QueueingConsumer(ch);
            ch.basicConsume(queueName, consumer);
            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                System.out.println("Message: " + new String(delivery.getBody()));
                ch.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        } catch (Exception ex) {
            System.err.println("Main thread caught exception: " + ex);
            ex.printStackTrace();
            System.exit(1);
View Full Code Here

public class QueueingConsumerShutdownTests extends BrokerTestCase{
  static final String QUEUE = "some-queue";
  static final int THREADS = 5;

  public void testNThreadShutdown() throws Exception{
    Channel channel = connection.createChannel();
    final QueueingConsumer c = new QueueingConsumer(channel);
    channel.queueDeclare(QUEUE, false, true, true, null);
    channel.basicConsume(QUEUE, c);
    final AtomicInteger count = new AtomicInteger(THREADS);
    final CountDownLatch latch = new CountDownLatch(THREADS);

    for(int i = 0; i < THREADS; i++){
      new Thread(){
View Full Code Here

            ConnectionFactory cfconn = new ConnectionFactory();
            cfconn.setUri(uri);
            Connection conn = cfconn.newConnection();

            Channel ch = conn.createChannel();

            if (exchange == null) {
                exchange = "amq.topic";
            } else {
                ch.exchangeDeclare(exchange, "topic");
            }

            System.out.println("Sending to exchange " + exchange + ", prefix: " + topicPrefix);

            int thisTimeCount = 0;
            int allTimeCount = 0;
            long startTime = System.currentTimeMillis();
            long nextSummaryTime = startTime;
            while (true) {
                ch.basicPublish(exchange, topicPrefix + newSuffix(), null, message.getBytes());
                thisTimeCount++;
                allTimeCount++;
                long now = System.currentTimeMillis();
                if (now > nextSummaryTime) {
                    int thisTimeRate = (int)(1.0 * thisTimeCount / (now - nextSummaryTime + SUMMARISE_EVERY) * SUMMARISE_EVERY);
 
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.