Package com.rabbitmq.client

Examples of com.rabbitmq.client.Connection


            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();
            JsonRpcClient client = new JsonRpcClient(ch, "", "Hello", RPC_TIMEOUT_ONE_SECOND);
            HelloJsonService service =
                (HelloJsonService) client.createProxy(HelloJsonService.class);

            System.out.println(service.greeting(request));

            java.util.List<Integer> numbers = new java.util.ArrayList<Integer>();
            numbers.add(1);
            numbers.add(2);
            numbers.add(3);
            System.out.println("1 + 2 + 3 = " + service.sum(numbers));

            conn.close();
        } catch (Exception e) {
            System.err.println("Main thread caught exception: " + e);
            e.printStackTrace();
            System.exit(1);
        }
View Full Code Here


    }

    private void loginOk(String name, byte[][] responses) throws IOException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setSaslConfig(new Config(name, responses));
        Connection connection = factory.newConnection();
        connection.close();
    }
View Full Code Here

    {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("testadmin");
        factory.setPassword("test");
        factory.setVirtualHost("/test");
        Connection connection = factory.newConnection();
        adminCh = connection.createChannel();
        withNames(new WithName() {
                public void with(String name) throws IOException {
                    adminCh.exchangeDeclare(name, "direct");
                    adminCh.queueDeclare(name, false, false, false, null);
                }});
View Full Code Here

    }

    public void publishOneInOneOutReceive(int backlogSize, int bodySize, int repeatCount, int sampleGranularity) throws IOException, InterruptedException {
        String q = "test";
        BasicProperties props = MessageProperties.MINIMAL_PERSISTENT_BASIC;
        Connection conn = newConnection();
        Channel chan = conn.createChannel();
        byte[] body = new byte[bodySize];
        List<Long> plateauSampleTimes = new ArrayList<Long>(repeatCount);
        List<Double> plateauSampleDeltas = new ArrayList<Double>(repeatCount);

        trace("Declaring and purging queue " + q);
        chan.queueDeclare(q, true, false, false, null);
        chan.queuePurge(q);
        chan.basicQos(1);

        trace("Building backlog out to " + backlogSize + " messages, each " + bodySize + " bytes long");
        for (int i = 0; i < backlogSize; i++) {
            chan.basicPublish("", q, props, body);
        }

        redeclare(q, chan);

        trace("Beginning plateau of " + repeatCount + " repeats, sampling every " + sampleGranularity + " messages");

        QueueingConsumer consumer = new QueueingConsumer(chan);
        chan.basicConsume(q, consumer);

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < repeatCount; i++) {
            if (((i % sampleGranularity) == 0) && (i > 0)) {
                long now = System.currentTimeMillis();
                double delta = 1000 * (now - startTime) / (double) sampleGranularity;
                plateauSampleTimes.add(now);
                plateauSampleDeltas.add(delta);
                System.out.print(String.format("# %3d%%; %012d --> %g microseconds/roundtrip            \r",
                                    (100 * i / repeatCount),
                                    now,
                                    delta));
                startTime = System.currentTimeMillis();
            }
            chan.basicPublish("", q, props, body);
            QueueingConsumer.Delivery d = consumer.nextDelivery();
            chan.basicAck(d.getEnvelope().getDeliveryTag(), false);
        }
        System.out.println();

        trace("Switching QOS to unlimited");
        chan.basicQos(0);

        trace("Draining backlog");
        for (int i = 0; i < backlogSize; i++) {
            QueueingConsumer.Delivery d = consumer.nextDelivery();
            chan.basicAck(d.getEnvelope().getDeliveryTag(), false);
        }

        redeclare(q, chan);

        trace("Closing connection");
        chan.close();
        conn.close();

        trace("Sample results (timestamp in milliseconds since epoch; microseconds/roundtrip)");
        System.out.println("(See log file for results; final sample was " +
                plateauSampleDeltas.get(plateauSampleDeltas.size() - 1) + ")");
        for (int i = 0; i < plateauSampleTimes.size(); i++) {
View Full Code Here

            int portNumber = (args.length > 1) ? Integer.parseInt(args[1]) : AMQP.PROTOCOL.PORT;

            ConnectionFactory connFactory = new ConnectionFactory();
            connFactory.setHost(hostName);
            connFactory.setPort(portNumber);
            Connection conn = connFactory.newConnection();
            final Channel ch = conn.createChannel();

            ch.queueDeclare("Hello", false, false, false, null);
            StringRpcServer server = new StringRpcServer(ch, "Hello") {
                    public String handleStringCall(String request) {
                        System.out.println("Got request: " + request);
View Full Code Here

*/
public class PerQueueTTLPublisher {

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

        String exchange = "ttl.exchange";
        String queue = "ttl.queue";

        // exchange
        channel.exchangeDeclare(exchange, "direct");

        // queue
        channel.queueDeclare(queue, true, false, false, Collections.singletonMap("x-message-ttl", (Object) 30000L));
        channel.queueBind(queue, exchange, queue, null);

        // send a message
        AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(2).build();
        for(int x = 0; x < 10; x++) {
            channel.basicPublish(exchange, queue, props, ("Msg [" + x + "]").getBytes());
        }

        System.out.println("Done");
        channel.close();
        connection.close();
    }
View Full Code Here

    public static void main(String[] args) {
        try {
            final String uri = optArg(args, 0, "amqp://localhost");
            boolean writeStats = optArg(args, 1, true);
            boolean autoAck = optArg(args, 2, true);
            final Connection conn = new ConnectionFactory(){{setUri(uri);}}.newConnection();
            System.out.println("Channel 0 fully open.");
            new ConsumerMain(conn, writeStats, autoAck).run();
        } catch (Exception e) {
            System.err.println("Main thread caught exception: " + e);
            e.printStackTrace();
View Full Code Here

            String message = (args.length > 3) ? args[3] :
                "the time is " + new java.util.Date().toString();

            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 + ", topic " + topic);
            ch.basicPublish(exchange, topic, 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

                            socket.setReceiveBufferSize(bufferSize);
                            socket.setSendBufferSize(bufferSize);
                        }
                    };

                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel();
                Queue.DeclareOk res = channel.queueDeclare();
                String queueName = res.getQueue();

                long start;

                start = System.nanoTime();

                for(int i = 0; i < MESSAGE_COUNT; i++) {
                    channel.basicPublish("", queueName,
                                         MessageProperties.BASIC, MESSAGE);
                }

                QueueingConsumer consumer = new QueueingConsumer(channel);
                channel.basicConsume(queueName, true, consumer);

                long publishTime = System.nanoTime() - start;

                start = System.nanoTime();

                for(int i = 0; i < MESSAGE_COUNT; i++){
                    consumer.nextDelivery();
                }

                long consumeTime = System.nanoTime() - start;

                double publishRate =
                    MESSAGE_COUNT / (publishTime / NANOSECONDS_PER_SECOND);
                double consumeRate =
                    MESSAGE_COUNT / (consumeTime / NANOSECONDS_PER_SECOND);

                if(useNagle){
                    publishRateNagle = publishRate;
                    consumeRateNagle = consumeRate;
                } else {
                    publishRateNoNagle = publishRate;
                    consumeRateNoNagle = consumeRate;
                }

                connection.close();
                // Small sleep to remove noise from hammering the server.
                Thread.sleep(100);
            }

            System.out.println(bufferSize + ", " +
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

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.