Example usage for com.rabbitmq.client.impl AMQConnection start

List of usage examples for com.rabbitmq.client.impl AMQConnection start

Introduction

In this page you can find the example usage for com.rabbitmq.client.impl AMQConnection start.

Prototype

public void start() throws IOException, TimeoutException 

Source Link

Document

Start up the connection, including the MainLoop thread.

Usage

From source file:ylj.demo.mq.amqp.qpid_jms.Listener.java

License:Apache License

public static void main(String[] args) throws JMSException, URLSyntaxException {

    //amqp://[<user>:<pass>@][<clientid>]<virtualhost>[?<option>='<value>'[&<option>='<value>']]
    //amqp://guest:guest@client1/development?brokerlist='tcp://localhost:5672'
    //     String url="amqp://newsrec0.photo.163.org:11613/??";
    //     String URL = "amqp://guest:guest@clientID/test?brokerlist='tcp://newsrec0.photo.163.org:11613'";

    String URL = "amqp://guest:guest@clientId/?brokerlist='tcp://newsrec10.photo.163.org:5672'";

    String channelName = "recsys.news.userfeed.usermodel";
    //      ConnectionFactory factory = new AMQConnectionFactory(host, port, user, password);
    AMQConnectionFactory factory = new AMQConnectionFactory();
    factory.setConnectionURLString(URL);
    //   ConnectionFactory factory = new AMQConnectionFactory(URL);      
    org.apache.qpid.client.AMQConnection connection = factory.createConnection();
    connection.start();

    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

    Destination dest = null;/*from  www . ja va  2 s .co m*/
    boolean topic = true;
    topic = false;
    if (topic) {
        dest = session.createTopic(channelName);
    } else {
        dest = session.createQueue(channelName);
    }

    MessageConsumer consumer = session.createConsumer(dest);
    long start = System.currentTimeMillis();
    long count = 1;

    System.out.println("Waiting for messages...");

    while (true) {

        Message msg = consumer.receive();

        if (msg instanceof TextMessage) {

            String body = ((TextMessage) msg).getText();
            if ("SHUTDOWN".equals(body)) {
                long diff = System.currentTimeMillis() - start;
                System.out.println(String.format("Received %d in %.2f seconds", count, (1.0 * diff / 1000.0)));
                connection.close();
                System.exit(1);
            } else {
                try {
                    if (count != msg.getIntProperty("id")) {
                        System.out.println("mismatch: " + count + "!=" + msg.getIntProperty("id"));
                    }
                } catch (NumberFormatException ignore) {
                }
                if (count == 1) {
                    start = System.currentTimeMillis();
                } else if (count % 1000 == 0) {
                    System.out.println(String.format("Received %d messages.", count));
                }
                count++;
            }

        } else {
            System.out.println("Unexpected message type: " + msg.getClass());
        }
    }
}