Package com.rabbitmq.client

Examples of com.rabbitmq.client.Connection


    public static void runProducerConsumerTest(String uri, int commitEvery)
        throws IOException, URISyntaxException, NoSuchAlgorithmException, KeyManagementException
    {
        ConnectionFactory cfconnp = new ConnectionFactory();
        cfconnp.setUri(uri);
        Connection connp = cfconnp.newConnection();
        ProducerMain p = new ProducerMain(connp, 2000, 10000, false, commitEvery, true);
        new Thread(p).start();
        ConnectionFactory cfconnc = new ConnectionFactory();
        cfconnc.setUri(uri);
        Connection connc = cfconnc.newConnection();
        ConsumerMain c = new ConsumerMain(connc, false, true);
        c.run();
    }
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

            String request = (args.length > 0) ? args[0] : "Rabbit";
            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) {
            System.err.println("Main thread caught exception: " + e);
            e.printStackTrace();
            System.exit(1);
        }
View Full Code Here

            r.print(params.base, params.filePrefix);
    }


    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 routingKey = args[3];
            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

            String exchange = (args.length > 2) ? args[2] : "";
            String routingKey = (args.length > 3) ? args[3] : "SimpleQueue";

            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

            Thread[] consumerThreads = new Thread[consumerCount];
            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,
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);
View Full Code Here

        public void run() {
            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);
            } catch (Throwable e) {
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

TOP

Related Classes of com.rabbitmq.client.Connection

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.