Package com.rabbitmq.client

Examples of com.rabbitmq.client.Channel


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

            final Channel channel = conn.createChannel();

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

            if (queue == null) {
                queue = channel.queueDeclare().getQueue();
            } else {
              channel.queueDeclare(queue, false, false, false, null);
            }

            channel.queueBind(queue, exchange, topicPattern);

            System.out.println("Listening to exchange " + exchange + ", pattern " + topicPattern +
                               " from queue " + queue);

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


            String uri = (args.length > 0) ? args[0] : "amqp://localhost";

            ConnectionFactory connFactory = new ConnectionFactory();
            connFactory.setUri(uri);
            Connection conn = connFactory.newConnection();
            final Channel ch = conn.createChannel();

            ch.queueDeclare("Hello", false, false, false, null);
            JsonRpcServer server =
                new JsonRpcServer(ch, "Hello", HelloJsonService.class,
                                  new HelloJsonService() {
                                      public String greeting(String name) {
                                          return "Hello, "+name+", from JSON-RPC over AMQP!";
View Full Code Here

    final Connection conn;
    try {
      conn = new ConnectionFactory()
          {{setUri(uri);}}.newConnection();
      Channel setup = conn.createChannel();

      setup.exchangeDeclare(EXCHANGE, "direct");
      setup.queueDeclare(QUEUE, false, false, false, null);
      setup.queueBind(QUEUE,EXCHANGE, "");
     
      setup.close();
    } catch(Exception e){
      e.printStackTrace();
      System.exit(1);
      throw null; // placate the compiler
    }

    for(int i = 0; i < threadCount; i++){
      new Thread(){
        @Override public void run(){
          try {
            Channel ch = conn.createChannel();
            while(true){
                ch.close();
                ch = conn.createChannel();
                ch.basicPublish(
                  EXCHANGE,
                  "", null,
                  new byte[1024 * 1024]
                );
                ch.basicGet(QUEUE, true);
            }
          } catch(Exception e){
            synchronized(lock){
              e.printStackTrace();
              System.err.println();
View Full Code Here

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

            Channel ch1 = conn.createChannel();

            String queueName = ch1.queueDeclare().getQueue();
            ch1.queueBind(queueName, exchange, "#");

            QueueingConsumer consumer = new QueueingConsumer(ch1);
            ch1.basicConsume(queueName, true, consumer);
            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String routingKey = delivery.getEnvelope().getRoutingKey();
                String contentType = delivery.getProperties().getContentType();
                System.out.println("Content-type: " + contentType);
View Full Code Here

                System.out.println("Starting connection " + i);
                factory.setUri(uri);
                final Connection conn = factory.newConnection();

                for (int j = 0; j < channelPerConnectionCount; j++) {
                    final Channel ch = conn.createChannel();

                    final int threadNumber = i * channelPerConnectionCount + j;
                    System.out.println("Starting " + threadNumber + " " + ch
                            + " thread...");
                    new Thread(new Runnable() {
View Full Code Here

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

            final Channel ch = conn.createChannel();

            String queueName =
    (requestedQueueName.equals("")
     ? ch.queueDeclare()
     : ch.queueDeclare(requestedQueueName,
                                   false, false, false, null)).getQueue();

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

            QueueingConsumer consumer = new QueueingConsumer(ch);
            ch.basicConsume(queueName, consumer);
            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    Map<String, Object> headers = delivery.getProperties().getHeaders();
    byte[] body = delivery.getBody();
    Object headerFilenameO = headers.get("filename");
    String headerFilename =
        (headerFilenameO == null)
        ? UUID.randomUUID().toString()
        : headerFilenameO.toString();
    File givenName = new File(headerFilename);
    if (givenName.getName().equals("")) {
        System.out.println("Skipping file with empty name: " + givenName);
    } else {
        File f = new File(outputDir, givenName.getName());
        System.out.print("Writing " + f + " ...");
        FileOutputStream o = new FileOutputStream(f);
        o.write(body);
        o.close();
        System.out.println(" done.");
    }
    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

    for(int i = 1; i <= CHANNEL_COUNT; i++)
      assertEquals(i, connection.createChannel().getChannelNumber());
  }

  public void testAllocateAfterFreeingLast() throws Exception{
    Channel ch = connection.createChannel();
    assertEquals(1, ch.getChannelNumber());
    ch.close();
    ch = connection.createChannel();
    assertEquals(1, ch.getChannelNumber());
  }
View Full Code Here

        assertTrue(10 != connection.createChannel().getChannelNumber());
  }

  public void testManualAllocationDoesntBreakThings() throws Exception{
    connection.createChannel((1 << 16) - 1);
    Channel ch = connection.createChannel();
    assertNotNull(ch);
  }
View Full Code Here

    public static void runConnectionShutdownTests(final String uri)
        throws IOException, URISyntaxException, NoSuchAlgorithmException, KeyManagementException
    {
        Connection conn;
        Channel ch;
        // Test what happens when a connection is shut down w/o first
        // closing the channels.
        conn = new ConnectionFactory(){{setUri(uri);}}.newConnection();
        ch = conn.createChannel();
        conn.close();
        // Test what happens when we provoke an error
        conn = new ConnectionFactory(){{setUri(uri);}}.newConnection();
        ch = conn.createChannel();
        try {
            ch.exchangeDeclare("mumble", "invalid");
            throw new RuntimeException("expected shutdown");
        } catch (IOException e) {
        }
        // Test what happens when we just kill the connection
        conn = new ConnectionFactory(){{setUri(uri);}}.newConnection();
View Full Code Here

public class PerQueueTTLGetter {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String queue = "ttl.queue";

        // exchange
        GetResponse response = channel.basicGet(queue, false);
        if(response == null) {
            System.out.println("Got no message...");
        } else {
            System.out.println("Got message: " + new String(response.getBody()));
            channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
        }
        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.