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:org.belio.mq.RabbitConsumer.java

private void openPublisher() {
    try {//from w  w w  .  ja va  2  s  .  c  om
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqproperties.getProperty("host"));
        factory.setVirtualHost(mqproperties.getProperty("vhost"));
        factory.setUsername(mqproperties.getProperty("username"));
        factory.setPassword(mqproperties.getProperty("password"));
        factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
        // Create a new connection to MQ
        publishingConnection = factory.newConnection();
        // Create a new channel and declare it's type and exhange as well
        publishingChannel = connection.createChannel();
        publishingChannel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null);
        publishingChannel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX),
                mqproperties.getProperty("type"));
        publishingChannel.queueBind(queueType.name().concat(QUEUE_SUFFIX),
                queueType.name().concat(EXCHANGE_SUFFIX), "");
    } catch (IOException exception) {
        Launcher.LOG.error(exception.getLocalizedMessage(), exception);
    }
}

From source file:org.belio.mq.RabbitPublisher.java

@Override
public void open() throws IOException {
    synchronized (RabbitPublisher.class) {
        try {/*  w  w w  .  j ava 2s .  c  o  m*/
            // Create a connection factory and 
            hosts = mqproperties.getProperty("host").split(",");

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(mqproperties.getProperty("host"));
            factory.setVirtualHost(mqproperties.getProperty("vhost"));
            factory.setUsername(mqproperties.getProperty("username"));
            factory.setPassword(mqproperties.getProperty("password"));
            factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
            // Create a new connection to MQ
            connection = factory.newConnection();
            // Create a new channel and declare it's type and exhange as well
            channel = connection.createChannel();
            channel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null);
            channel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX), mqproperties.getProperty("type"));
            channel.queueBind(queueType.name().concat(QUEUE_SUFFIX), queueType.name().concat(EXCHANGE_SUFFIX),
                    "");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.geoserver.notification.common.sender.RabbitMQSender.java

License:Open Source License

public void initialize() throws Exception {

    if (uri == null) {
        if (this.username != null && !this.username.isEmpty() && this.password != null
                && !this.password.isEmpty()) {
            this.uri = "amqp://" + this.username + ":" + this.password + "@" + this.host + ":" + this.port;
        } else {//from   w w  w. j av a 2 s.  c  o  m
            this.uri = "amqp://" + this.host + ":" + this.port;
        }

    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(this.uri);
    String vHost = (this.virtualHost != null && !this.virtualHost.isEmpty() ? this.virtualHost : "/");
    factory.setVirtualHost(vHost);
    factory.setSaslConfig(new CustomSaslConfig());
    conn = factory.newConnection();
    channel = conn.createChannel();
}

From source file:org.graylog2.inputs.amqp.AMQPConsumer.java

License:Open Source License

private Channel connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(server.getConfiguration().getAmqpUsername());
    factory.setPassword(server.getConfiguration().getAmqpPassword());
    factory.setVirtualHost(server.getConfiguration().getAmqpVirtualhost());
    factory.setHost(server.getConfiguration().getAmqpHost());
    factory.setPort(server.getConfiguration().getAmqpPort());

    connection = factory.newConnection(Executors.newCachedThreadPool(
            new ThreadFactoryBuilder().setNameFormat("amqp-consumer-" + queueConfig.getId() + "-%d").build()));

    return connection.createChannel();
}

From source file:org.graylog2.inputs.transports.AmqpConsumer.java

License:Open Source License

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);/*from w ww. j  av  a  2s.c  om*/
    factory.setPort(port);
    factory.setVirtualHost(virtualHost);

    if (tls) {
        try {
            LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId());
            factory.useSslProtocol();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IOException("Couldn't enable TLS for AMQP input.", e);
        }
    }

    // Authenticate?
    if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

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

    if (null == channel) {
        LOG.error("No channel descriptor available!");
    }

    if (null != channel && prefetchCount > 0) {
        channel.basicQos(prefetchCount);

        LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }

    connection.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            if (cause.isInitiatedByApplication()) {
                LOG.info("Not reconnecting connection, we disconnected explicitly.");
                return;
            }
            while (true) {
                try {
                    LOG.error("AMQP connection lost! Trying reconnect in 1 second.");

                    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

                    connect();

                    LOG.info("Connected! Re-starting consumer.");

                    run();

                    LOG.info("Consumer running.");
                    break;
                } catch (IOException e) {
                    LOG.error("Could not re-connect to AMQP broker.", e);
                }
            }
        }
    });
}

From source file:org.graylog2.messagehandlers.amqp.AMQPBroker.java

License:Open Source License

public Connection getConnection() throws IOException {
    if (connection == null || !connection.isOpen()) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
        factory.setVirtualHost(getVirtualHost());
        factory.setHost(getHost());//from   w ww. j a  v  a  2  s .c om
        factory.setPort(getPort());
        this.connection = factory.newConnection();
    }

    return this.connection;
}

From source file:org.graylog2.radio.transports.amqp.AMQPSender.java

License:Open Source License

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);/*from  w  w  w  .j av  a2  s .c  o m*/
    factory.setPort(port);

    factory.setVirtualHost(vHost);

    // Authenticate?
    if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

    factory.setConnectionTimeout((int) connectTimeout.getMillis());

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

    // It's ok if the queue or exchange already exist.
    channel.queueDeclare(queueName, true, false, false, null);
    channel.exchangeDeclare(exchangeName, queueType, false, false, null);

    channel.queueBind(queueName, exchangeName, routingKey);
}

From source file:org.iplantcollaborative.ClientRegistrar.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostPublish());

    factory.setAutomaticRecoveryEnabled(true);

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

    LOG.info("client registrar connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());

    this.channel.basicQos(1);
    this.channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#");

    this.consumer = new DefaultConsumer(this.channel) {
        @Override/*from   w ww . j  a  va 2 s. c o  m*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            LOG.info("registration - " + message);

            BasicProperties replyProps = new BasicProperties.Builder()
                    .correlationId(properties.getCorrelationId()).build();

            ARequest request = RequestFactory.getRequestInstance(message);

            // handle lease request
            if (request instanceof RequestLease) {
                ResponseLease res = lease((RequestLease) request);
                String response_json = serializer.toJson(res);

                if (properties.getReplyTo() != null) {
                    channel.basicPublish("", properties.getReplyTo(), replyProps, response_json.getBytes());
                } else {
                    LOG.error("unable to return response. reply_to field is null");
                }
            } else {
                LOG.error("Unknown request : " + message);
            }

            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    };

    this.workerThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                channel.basicConsume(QUEUE_NAME, false, consumer);
                LOG.info("Waiting for registrations");
            } catch (IOException ex) {
                LOG.error("Exception occurred while consuming message", ex);
            }
        }
    });
    this.workerThread.start();
}

From source file:org.iplantcollaborative.DataStoreMessageReceiver.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostSubscribe());

    factory.setAutomaticRecoveryEnabled(true);

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

    LOG.info("subscriber connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());

    this.channel.basicQos(1);
    this.queueName = this.channel.queueDeclare().getQueue();
    this.channel.queueBind(this.queueName, EXCHANGE_NAME, "#");

    this.consumer = new DefaultConsumer(this.channel) {
        @Override/*  ww w.java2s .co  m*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            LOG.debug("subscribe - " + envelope.getRoutingKey() + ":" + message);

            DataStoreMessageProcessor processor = binder.getProcessor();
            if (processor != null) {
                processor.process(envelope.getRoutingKey(), message);
            } else {
                LOG.error("processor not registered");
            }

            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    };

    this.workerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                channel.basicConsume(queueName, consumer);
                LOG.info("Waiting for messages");
            } catch (IOException ex) {
                LOG.error("Exception occurred while consuming a message", ex);
            }
        }
    });
    this.workerThread.start();
}

From source file:org.iplantcollaborative.MessagePublisher.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostPublish());

    factory.setAutomaticRecoveryEnabled(true);

    this.connection = factory.newConnection();
    this.channel = this.connection.createChannel();
    this.channel.basicQos(1);

    LOG.info("publisher connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());
}