Example usage for com.rabbitmq.client ConnectionFactory newConnection

List of usage examples for com.rabbitmq.client ConnectionFactory newConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory newConnection.

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:pl.nask.hsn2.unicorn.connector.ConnectorImpl.java

License:Open Source License

private void createConnection() throws ConnectionException {
    ConnectionFactory factory = new ConnectionFactory();
    String[] addressParts = address.split(":");
    factory.setHost(addressParts[0]);/*from w  w w  .j a v a  2s .co  m*/
    if (addressParts.length > 1) {
        factory.setPort(Integer.parseInt(addressParts[1]));
    }

    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (IOException e) {
        throw new ConnectionException("Creating connection error!", e);
    }
}

From source file:pluginmanager.RequestManager.java

License:Open Source License

@Override
public void run() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");

    // RESPONSE QUEUE STUFF
    try {//from  w  ww .  j av a 2s.co m
        responseConnection = factory.newConnection();
        responseChannel = responseConnection.createChannel();
        responseChannel.queueDeclare(RESPONSE_QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    } catch (IOException | TimeoutException e2) {
        e2.printStackTrace();
    }

    Consumer consumer = new DefaultConsumer(responseChannel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            HttpResponse response = (HttpResponse) SerializationUtils.deserialize(body);
            try {
                response.write(sockDrawer.get(response.id).getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    try {
        responseChannel.basicConsume(RESPONSE_QUEUE_NAME, true, consumer);
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    //REQUEST QUEUE STUFF
    try {
        requestConnection = factory.newConnection();
        requestChannel = requestConnection.createChannel();
        requestChannel.queueDeclare(REQUEST_QUEUE_NAME, false, false, false, null);
    } catch (IOException | TimeoutException e1) {
        e1.printStackTrace();
    }

    System.out.println("Request Manger Running");
    while (true) {
        if (!queue.isEmpty()) {
            System.out.println("Processing");
            PriorityRequest request = queue.poll();
            byte[] data = SerializationUtils.serialize(request);
            try {
                requestChannel.basicPublish("", REQUEST_QUEUE_NAME, null, data);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            //response.write(socket.getOutputStream());
        } else {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}