Package com.rabbitmq.client

Examples of com.rabbitmq.client.Channel


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

            ConnectionFactory cfconn = new ConnectionFactory();
            cfconn.setUri(uri);
            Connection conn = cfconn.newConnection();
            Channel ch = conn.createChannel();
            RpcClient service = new RpcClient(ch, "", "Hello");

            System.out.println(service.stringCall(request));
            conn.close();
        } catch (Exception e) {
View Full Code Here


  // The thrown runtime exception should get intercepted by the
  // consumer exception handler, and result in a clean shut down.
  public void testCloseWithFaultyConsumer() throws Exception{
    SpecialConnection connection = new SpecialConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare("x", "direct");
    channel.queueDeclare("q", false, false, false, null);
    channel.queueBind("q", "x", "k");

    channel.basicConsume("q", true, new DefaultConsumer(channel){
        @Override
        public void handleDelivery(String consumerTag,
                                   Envelope envelope,
                                   AMQP.BasicProperties properties,
                                   byte[] body) {
            throw new RuntimeException("I am a bad consumer");
        }
    });

    channel.basicPublish("x", "k", null, new byte[10]);

    assertTrue(closeLatch.await(200, TimeUnit.MILLISECONDS));
    assertTrue(connection.hadValidShutdown());
  }
View Full Code Here

    }


    public Results run() throws Exception{
        Connection con = new ConnectionFactory(){{setHost(params.host); setPort(params.port);}}.newConnection();
        Channel channel = con.createChannel();

        Results r = new Results(params.maxBindingExp);

        for (int y = 0; y < params.maxBindingExp; y++) {

            final int maxBindings = pow(params.base, y);

            String[] routingKeys =  new String[maxBindings];
            for (int b = 0; b < maxBindings; b++) {
                routingKeys[b] = UUID.randomUUID().toString();
            }

            Stack<String> queues = new Stack<String>();

            int maxQueueExp = Math.min(params.maxQueueExp, params.maxExp - y);

            System.out.println("---------------------------------");
            System.out.println("| bindings = " + maxBindings + ", messages = " + params.messageCount);

            System.out.println("| Routing");

            int q = 0;

            // create queues & bindings, time routing
            Measurements creation = new CreationMeasurements(maxQueueExp);
            float routingTimes[] = new float[maxQueueExp];
            for (int x = 0; x < maxQueueExp; x++) {

                final int maxQueues = pow(params.base, x);

                for (; q < maxQueues; q++) {
                    AMQP.Queue.DeclareOk ok = channel.queueDeclare();
                    queues.push(ok.getQueue());
                    for (int b = 0; b < maxBindings; b++) {
                        channel.queueBind(ok.getQueue(), "amq.direct", routingKeys[b]);
                    }
                }

                creation.addDataPoint(x);

                float routingTime = timeRouting(channel, routingKeys);
                routingTimes[x] = routingTime;
                printTime(params.base, x, routingTime);
            }

            r.routingTimes[y] = routingTimes;
            float[] creationTimes = creation.analyse(params.base);
            r.creationTimes[y] = creationTimes;
            System.out.println("| Creating");
            printTimes(params.base, creationTimes);

            // delete queues & bindings
            Measurements deletion = new DeletionMeasurements(maxQueueExp);
            for (int x = maxQueueExp - 1; x >= 0; x--) {

                final int maxQueues = (x == 0) ? 0 : pow(params.base, x - 1);

                for (; q > maxQueues; q--) {
                    channel.queueDelete(queues.pop());
                }

                deletion.addDataPoint(x);
            }

            float[] deletionTimes = deletion.analyse(params.base);
            r.deletionTimes[y] = deletionTimes;
            System.out.println("| Deleting");
            printTimes(params.base, deletionTimes);
        }

        channel.close();
        con.close();

        return r;
    }
View Full Code Here

            String message = args[4];

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

            ch.exchangeDeclare(exchange, exchangeType);
            ch.basicPublish(exchange, routingKey, null, message.getBytes());
            ch.close();
            conn.close();
        } catch (Exception e) {
            System.err.println("Main thread caught exception: " + e);
            e.printStackTrace();
            System.exit(1);
View Full Code Here

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

            Channel ch = conn.createChannel();

            if (exchange.equals("")) {
                ch.queueDeclare(routingKey, false, false, false, null);
            }
            ch.basicPublish(exchange, routingKey, null, message.getBytes());
            ch.close();
            conn.close();
        } catch (Exception e) {
            System.err.println("Main thread caught exception: " + e);
            e.printStackTrace();
            System.exit(1);
View Full Code Here

            Connection[] consumerConnections = new Connection[consumerCount];
            for (int i = 0; i < consumerCount; i++) {
                System.out.println("starting consumer #" + i);
                Connection conn = factory.newConnection();
                consumerConnections[i] = conn;
                Channel channel = conn.createChannel();
                if (consumerTxSize > 0) channel.txSelect();
                channel.exchangeDeclare(exchangeName, exchangeType);
                String qName =
                        channel.queueDeclare(queueName,
                                             flags.contains("persistent"),
                                             exclusive, autoDelete,
                                             null).getQueue();
                if (prefetchCount > 0) channel.basicQos(prefetchCount);
                channel.queueBind(qName, exchangeName, id);
                Thread t =
                    new Thread(new Consumer(channel, id, qName,
                                            consumerTxSize, autoAck,
                                            multiAckEvery, stats, timeLimit));
                consumerThreads[i] = t;
            }
            Thread[] producerThreads = new Thread[producerCount];
            Connection[] producerConnections = new Connection[producerCount];
            Channel[] producerChannels = new Channel[producerCount];
            for (int i = 0; i < producerCount; i++) {
                System.out.println("starting producer #" + i);
                Connection conn = factory.newConnection();
                producerConnections[i] = conn;
                Channel channel = conn.createChannel();
                producerChannels[i] = channel;
                if (producerTxSize > 0) channel.txSelect();
                if (confirm >= 0) channel.confirmSelect();
                channel.exchangeDeclare(exchangeName, exchangeType);
                final Producer p = new Producer(channel, exchangeName, id,
                                                flags, producerTxSize,
                                                rateLimit, minMsgSize, timeLimit,
                                                confirm, stats);
                channel.addReturnListener(p);
                channel.addConfirmListener(p);
                Thread t = new Thread(p);
                producerThreads[i] = t;
            }

            for (int i = 0; i < consumerCount; i++) {
View Full Code Here

    }

    private void startPublisher() throws IOException, InterruptedException {

        final Connection conn = connectionFactory.newConnection();
        final Channel pubCh = conn.createChannel();

        //This forces the initialisation of the guid generation, which
        //is an interaction with the persister and not something we
        //want to see delay things.
        publish(pubCh);

        //a synchronous request, to make sure the publish is done
        pubCh.queueDeclare();

        //signal the main thread
        init.release();
        //wait for main thread to let us resume
        resume.await();
View Full Code Here

            try {
                long startTime = System.currentTimeMillis();

                // Setup
                Connection conn = connectionFactory.newConnection();
                Channel ch = conn.createChannel();
                ch.queueDeclare(QUEUE_NAME, true, false, false, null);
                ch.confirmSelect();

                // Publish
                for (long i = 0; i < msgCount; ++i) {
                    ch.basicPublish("", QUEUE_NAME,
                                    MessageProperties.PERSISTENT_BASIC,
                                    "nop".getBytes());
                }

                ch.waitForConfirmsOrDie();

                // Cleanup
                ch.queueDelete(QUEUE_NAME);
                ch.close();
                conn.close();

                long endTime = System.currentTimeMillis();
                System.out.printf("Test took %.3fs\n",
                                  (float)(endTime - startTime)/1000);
View Full Code Here

    static class Consumer implements Runnable {
        public void run() {
            try {
                // Setup
                Connection conn = connectionFactory.newConnection();
                Channel ch = conn.createChannel();
                ch.queueDeclare(QUEUE_NAME, true, false, false, null);

                // Consume
                QueueingConsumer qc = new QueueingConsumer(ch);
                ch.basicConsume(QUEUE_NAME, true, qc);
                for (int i = 0; i < msgCount; ++i) {
                    qc.nextDelivery();
                }

                // Cleanup
                ch.close();
                conn.close();
            } catch (Throwable e) {
                System.out.println("Whoosh!");
                System.out.print(e);
            }
View Full Code Here

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

            ConnectionFactory cfconn = new ConnectionFactory();
            cfconn.setUri(uri);
            Connection conn = cfconn.newConnection();
            Channel ch = conn.createChannel();
            JsonRpcClient client = new JsonRpcClient(ch, "", "Hello", RPC_TIMEOUT_ONE_SECOND);
            HelloJsonService service =
                (HelloJsonService) client.createProxy(HelloJsonService.class);

            System.out.println(service.greeting(request));
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.