Example usage for com.rabbitmq.client ConnectionFactory setVirtualHost

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

Introduction

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

Prototype

public void setVirtualHost(String virtualHost) 

Source Link

Document

Set the virtual host.

Usage

From source file:blocker.Blocker.java

/**
 * @param argv/*from w  w  w. j a  v a  2  s . com*/
 */
public static void main(String[] argv) throws Exception {
    seconds = Integer.parseInt(argv[7]);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(argv[0]);
    factory.setUsername(argv[2]);
    factory.setPassword(argv[3]);
    factory.setVirtualHost(argv[1]);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(argv[4], "direct", true);
    String queueName = channel.queueDeclare(argv[5], true, false, false, null).getQueue();

    //                          exchange  key
    channel.queueBind(queueName, argv[4], argv[6]);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            JSONParser parser = new JSONParser();

            Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread th, Throwable ex) {
                    System.out.println("Uncaught exception: " + ex);
                }
            };

            try {
                Object obj = parser.parse(message);
                JSONObject jobj = (JSONObject) obj;
                String IP = (String) jobj.get("clientip");
                Thread t = new Thread(new BlockerThread(IP));
                t.setUncaughtExceptionHandler(h);
                t.start();
            } catch (ParseException ex) {
                Logger.getLogger(Blocker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    };
    channel.basicConsume(argv[5], true, consumer);
}

From source file:ch.icclab.cyclops.consume.RabbitMQListener.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 * @return channel reference or null//from  w ww. ja  v  a 2 s.c o m
 */
private Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(credentials.getConsumerUsername());
    factory.setPassword(credentials.getConsumerPassword());
    factory.setHost(credentials.getConsumerHost());
    factory.setPort(credentials.getConsumerPort());
    factory.setVirtualHost(credentials.getConsumerVirtualHost());
    factory.setAutomaticRecoveryEnabled(true);

    Channel chan;

    try {
        // create new connection
        connection = factory.newConnection();

        // create/connect to the channel
        chan = connection.createChannel();

        logger.trace(String.format("RabbitMQ Consumer connected to %s:%d", credentials.getConsumerHost(),
                credentials.getConsumerPort()));

    } catch (Exception e) {
        logger.error(String.format("RabbitMQ Consumer couldn't be created: %s", e.getMessage()));
        connection = null;
        chan = null;
    }

    // return channel reference, or null
    return chan;
}

From source file:ch.icclab.cyclops.publish.RabbitMQPublisher.java

License:Open Source License

/**
 * Initialise connection to RabbitMQ/*from   w  w w. j  a  v  a2  s.  c  o m*/
 * @return status
 */
private Boolean initialiseConnection() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(credentials.getPublisherUsername());
        factory.setPassword(credentials.getPublisherPassword());
        factory.setHost(credentials.getPublisherHost());
        factory.setPort(credentials.getPublisherPort());
        factory.setVirtualHost(credentials.getPublisherVirtualHost());
        factory.setAutomaticRecoveryEnabled(true);

        connection = factory.newConnection();
        channel = connection.createChannel();

        // declare exchange to be used (we want it to be durable and based on routing key)
        channel.exchangeDeclare(credentials.getPublisherDispatchExchange(), "direct", true);
        channel.exchangeDeclare(credentials.getPublisherBroadcastExchange(), "fanout", true);

        logger.trace(String.format("RabbitMQ Publisher connected to %s:%d", credentials.getPublisherHost(),
                credentials.getPublisherPort()));
        logger.trace(String.format(
                "RabbitMQ Publisher will dispatch to \"%s\" and broadcast to \"%s\" exchanges",
                credentials.getPublisherDispatchExchange(), credentials.getPublisherBroadcastExchange()));

        return true;
    } catch (Exception e) {
        logger.error(String.format("RabbitMQ Publisher couldn't be created: %s", e.getMessage()));
        return false;
    }
}

From source file:ch.icclab.cyclops.rabbitmq.RabbitMQAbstract.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 * @return channel reference or null/*from www  .  ja v  a  2s  .c om*/
 */
protected Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(credentials.getRabbitMQUsername());
    factory.setPassword(credentials.getRabbitMQPassword());
    factory.setHost(credentials.getRabbitMQHost());
    factory.setPort(credentials.getRabbitMQPort());
    factory.setVirtualHost(credentials.getRabbitMQVirtualHost());

    Channel chan;

    try {

        logger.trace("Creating connection to RabbitMQ for host: " + credentials.getRabbitMQHost()
                + " and port: " + credentials.getRabbitMQPort());

        // create new connection
        connection = factory.newConnection();

        logger.trace("Creating and connecting to RabbitMQ channel");

        // create/connect to the channel
        chan = connection.createChannel();

    } catch (Exception ex) {
        logger.error("Couldn't start Rabbit MQ: " + ex.getMessage());
        connection = null;
        chan = null;
    }

    // return channel reference, or null
    return chan;
}

From source file:ch.icclab.cyclops.usecases.mcn.rabbitMQClient.McnRabbitMQClient.java

License:Open Source License

/**
 * Will return channel for RabbitMQ connection
 *
 * @return channel reference or null/* ww w .  j  ava2  s .  co m*/
 */
private Channel getChannel() {
    // connect to the RabbitMQ based on settings from Load
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(rabbitMQSettings.getRabbitMQUsername());
    factory.setPassword(rabbitMQSettings.getRabbitMQPassword());
    factory.setVirtualHost(rabbitMQSettings.getRabbitMQVirtualHost());
    factory.setHost(rabbitMQSettings.getRabbitMQHost());
    factory.setPort(rabbitMQSettings.getRabbitMQPort());
    try {
        // create new connection
        connection = factory.newConnection();

        // create/connect to the channel
        channel = connection.createChannel();
        channel.queueDeclare(queueName, true, false, false, null);
    } catch (Exception ex) {
        logger.error("Couldn't start Rabbit MQ: " + ex.getMessage());
        ex.printStackTrace();
    }

    // return channel reference, or null
    return channel;
}

From source file:com.codio.collab.core.queue.QueueFactory.java

License:Apache License

private void openConnection() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setVirtualHost(getConnectionConfig().getVhost());
    factory.setUsername(getConnectionConfig().getUser());
    factory.setPassword(getConnectionConfig().getPassword());
    factory.setPort(getConnectionConfig().getPort());
    factory.setHost(getConnectionConfig().getHost());
    factory.setAutomaticRecoveryEnabled(true);
    try {/*from  w  w w.j  a  v a  2 s . c o m*/
        connection = factory.newConnection();
    } catch (IOException e) {
        LOG.error("Can not open rmq connection {}", e.getMessage());
    }
}

From source file:com.colm.app.Main.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Connection connection;/*from  w  w  w  .ja  v a2  s .c o m*/
    Channel channel;
    String replyQueueName;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.0.13");
    factory.setPassword("admin");
    factory.setUsername("admin");
    factory.setVirtualHost("admin");
    connection = factory.newConnection();
    channel = connection.createChannel();
    String queneNAme = "rabbitmq_test";
    channel.queueDeclare(queneNAme, false, false, false, null);
    String message = "Something Else";
    for (int i = 0; i < 1000; i++) {
        channel.basicPublish("", queneNAme, null, message.getBytes());
    }
    System.out.println("Sent '" + message + "'");
    channel.close();
    connection.close();

}

From source file:com.espertech.esperio.amqp.AMQPSink.java

License:Open Source License

public void open(DataFlowOpOpenContext openContext) {
    log.info("Opening AMQP, settings are: " + settings.toString());

    try {//from   ww  w  .  ja va 2s .  c om
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(settings.getHost());
        if (settings.getPort() > -1) {
            connectionFactory.setPort(settings.getPort());
        }
        if (settings.getUsername() != null) {
            connectionFactory.setUsername(settings.getUsername());
        }
        if (settings.getPassword() != null) {
            connectionFactory.setPassword(settings.getPassword());
        }
        if (settings.getVhost() != null) {
            connectionFactory.setVirtualHost(settings.getVhost());
        }

        connection = connectionFactory.newConnection();
        channel = connection.createChannel();

        if (settings.getExchange() != null) {
            channel.exchangeDeclarePassive(settings.getExchange());
        }

        final AMQP.Queue.DeclareOk queue;
        if (settings.getQueueName() == null || settings.getQueueName().trim().length() == 0) {
            queue = channel.queueDeclare();
        } else {
            // java.lang.String queue,boolean durable,boolean exclusive,boolean autoDelete,java.util.Map<java.lang.String,java.lang.Object> arguments) throws java.io.IOException
            queue = channel.queueDeclare(settings.getQueueName(), settings.isDeclareDurable(),
                    settings.isDeclareExclusive(), settings.isDeclareAutoDelete(),
                    settings.getDeclareAdditionalArgs());
        }
        if (settings.getExchange() != null && settings.getRoutingKey() != null) {
            channel.queueBind(queue.getQueue(), settings.getExchange(), settings.getRoutingKey());
        }

        final String queueName = queue.getQueue();
        log.info("AMQP producing queue is " + queueName + (settings.isLogMessages() ? " with logging" : ""));
    } catch (IOException e) {
        String message = "AMQP setup failed: " + e.getMessage();
        log.error(message, e);
        throw new RuntimeException(message, e);
    }
}

From source file:com.espertech.esperio.amqp.AMQPSource.java

License:Open Source License

public void open(DataFlowOpOpenContext openContext) {
    log.info("Opening AMQP, settings are: " + settings.toString());

    try {/*from   ww  w . ja va  2s .co m*/
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(settings.getHost());
        if (settings.getPort() > -1) {
            connectionFactory.setPort(settings.getPort());
        }
        if (settings.getUsername() != null) {
            connectionFactory.setUsername(settings.getUsername());
        }
        if (settings.getPassword() != null) {
            connectionFactory.setPassword(settings.getPassword());
        }
        if (settings.getVhost() != null) {
            connectionFactory.setVirtualHost(settings.getVhost());
        }

        connection = connectionFactory.newConnection();
        channel = connection.createChannel();

        channel.basicQos(settings.getPrefetchCount());
        if (settings.getExchange() != null) {
            channel.exchangeDeclarePassive(settings.getExchange());
        }

        final AMQP.Queue.DeclareOk queue;
        if (settings.getQueueName() == null || settings.getQueueName().trim().length() == 0) {
            queue = channel.queueDeclare();
        } else {
            // java.lang.String queue,boolean durable,boolean exclusive,boolean autoDelete,java.util.Map<java.lang.String,java.lang.Object> arguments) throws java.io.IOException
            queue = channel.queueDeclare(settings.getQueueName(), settings.isDeclareDurable(),
                    settings.isDeclareExclusive(), settings.isDeclareAutoDelete(),
                    settings.getDeclareAdditionalArgs());
        }
        if (settings.getExchange() != null && settings.getRoutingKey() != null) {
            channel.queueBind(queue.getQueue(), settings.getExchange(), settings.getRoutingKey());
        }

        final String queueName = queue.getQueue();
        log.info("AMQP consuming queue " + queueName + (settings.isLogMessages() ? " with logging" : ""));

        consumer = new QueueingConsumer(channel);
        consumerTag = channel.basicConsume(queueName, settings.isConsumeAutoAck(), consumer);
    } catch (IOException e) {
        String message = "AMQP source setup failed: " + e.getMessage();
        log.error(message, e);
        throw new EPException(message, e);
    }
}

From source file:com.github.liyp.rabbitmq.demo2.Producer.java

License:Apache License

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("liyp");
    factory.setPassword("liyp");
    factory.setVirtualHost("/");
    factory.setHost("127.0.0.1");
    factory.setPort(5672);/*from   w  w w.ja  v a2s .c  o  m*/
    Connection conn = factory.newConnection();
    Channel channel = conn.createChannel();

    for (int i = 0; i < 10000; i++) {
        byte[] messageBodyBytes = "Hello, world!".getBytes();
        channel.basicPublish("", "my-queue", null, messageBodyBytes);
        System.out.println(channel.isOpen());
    }

    channel.close();
    conn.close();
}