Example usage for com.rabbitmq.client ConnectionFactory getVirtualHost

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

Introduction

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

Prototype

public String getVirtualHost() 

Source Link

Document

Retrieve the virtual host.

Usage

From source file:com.zero_x_baadf00d.play.module.rabbitmq.RabbitMQModuleImpl.java

License:Open Source License

/**
 * Build an instance./*from  www.  j a  v a  2 s  .  co m*/
 *
 * @param lifecycle     The current application lifecyle
 * @param configuration The current application configuration
 * @since 16.05.19
 */
@Inject
public RabbitMQModuleImpl(final ApplicationLifecycle lifecycle, final Config configuration) {
    this.configuration = configuration;
    try {
        final String uri = configuration.getString(RabbitMQModuleImpl.RABBITMQ_CONN_URI);
        if (uri == null || uri.isEmpty()) {
            throw new RuntimeException("URI is empty");
        }
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(uri);
        connectionFactory
                .setRequestedHeartbeat(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_HEARTBEAT));
        connectionFactory
                .setNetworkRecoveryInterval(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_RECOVERY));
        connectionFactory.setConnectionTimeout(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_CONN_TIMEOUT));
        connectionFactory.setAutomaticRecoveryEnabled(
                configuration.getBoolean(RabbitMQModuleImpl.RABBITMQ_AUTO_RECOVERY));
        if (uri.toLowerCase(Locale.ENGLISH).startsWith("amqps://")) {
            connectionFactory.useSslProtocol();
        }

        final ExecutorService es = Executors
                .newFixedThreadPool(configuration.getInt(RabbitMQModuleImpl.RABBITMQ_EXECUTOR));
        this.rabbitConnection = connectionFactory.newConnection(es);
        RabbitMQModuleImpl.LOGGER.info("RabbitMQ connected at {}",
                String.format("amqp%s://%s:%d/%s", connectionFactory.isSSL() ? "s" : "",
                        connectionFactory.getHost(), connectionFactory.getPort(),
                        connectionFactory.getVirtualHost()));
    } catch (Exception ex) {
        this.rabbitConnection = null;
        if (!this.configuration.getBoolean(RabbitMQModuleImpl.RABBITMQ_BYPASS_ERROR)) {
            RabbitMQModuleImpl.LOGGER.error("Can't initialize RabbitMQ module", ex);
            throw new RuntimeException(ex);
        } else {
            RabbitMQModuleImpl.LOGGER.warn("Can't initialize RabbitMQ module: {}", ex.getMessage());
        }
    }

    lifecycle.addStopHook(() -> {
        RabbitMQModuleImpl.LOGGER.info("Shutting down RabbitMQ");
        if (this.rabbitConnection != null) {
            this.rabbitConnection.close();
        }
        return CompletableFuture.completedFuture(null);
    });
}

From source file:org.apache.flume.amqp.AmqpSourceTest.java

License:Apache License

@Test
public void testCreateConnectionFactoryFrom() throws Exception {
    Context ctx = createContext();

    ConnectionFactory connectionFactory = AmqpSource.createConnectionFactoryFrom(ctx);

    assertThat(connectionFactory.getHost(), is(HOST_NAME));
    assertThat(connectionFactory.getPort(), is(PORT));
    assertThat(connectionFactory.getVirtualHost(), is(VIRTUAL_HOST));
    assertThat(connectionFactory.getUsername(), is(USER_NAME));
    assertThat(connectionFactory.getPassword(), is(PASSWORD));
    assertThat(connectionFactory.getConnectionTimeout(), is(CONNECTION_TIMEOUT));
    assertThat(connectionFactory.getRequestedHeartbeat(), is(REQUEST_HEARTBEAT));
}

From source file:org.apache.flume.RabbitMQUtilTest.java

License:Apache License

@Test
public void getFactory() {
    ConnectionFactory factory = RabbitMQUtil.getFactory(context);
    Assert.assertNotNull("factory should not be null", context);

    Assert.assertEquals("Host does not match", context.getString(RabbitMQConstants.CONFIG_HOSTNAME),
            factory.getHost());/*  ww  w . j  av  a  2s  .  c o  m*/
    Assert.assertEquals("Port does not match", context.getInteger(RabbitMQConstants.CONFIG_PORT),
            (Integer) factory.getPort());
    Assert.assertEquals("ConnectionTimeout does not match",
            context.getInteger(RabbitMQConstants.CONFIG_CONNECTIONTIMEOUT),
            (Integer) factory.getConnectionTimeout());
    Assert.assertEquals("Password does not match", context.getString(RabbitMQConstants.CONFIG_PASSWORD),
            factory.getPassword());
    Assert.assertEquals("Username does not match", context.getString(RabbitMQConstants.CONFIG_USERNAME),
            factory.getUsername());
    Assert.assertEquals("VirtualHost does not match", context.getString(RabbitMQConstants.CONFIG_VIRTUALHOST),
            factory.getVirtualHost());
}

From source file:org.apache.synapse.message.store.RabbitMQStoreTest.java

License:Open Source License

/**
 * call init method with dummy values and validating connectionFactory object
 *
 * @throws NoSuchFieldException/*from  w w  w  . j a v  a 2  s .  c  o  m*/
 * @throws IllegalAccessException
 */
@Test
public void testInit() throws NoSuchFieldException, IllegalAccessException {
    ConnectionFactory factory = (ConnectionFactory) connectionFactory.get(rabbitMQStore);
    Assert.assertEquals("should return previously stored values", factory.getPort(), Integer.parseInt(PORT));
    Assert.assertEquals("should return previously stored values", factory.getHost(), HOST);
    Assert.assertEquals("should return previously stored values", factory.getPassword(), PASSWORD);
    Assert.assertEquals("should return previously stored values", factory.getUsername(), USERNAME);
    Assert.assertEquals("should return previously stored values", factory.getVirtualHost(), VIRTUAL_HOST);
}

From source file:org.teksme.server.common.messaging.AMQPBrokerManager.java

License:Apache License

private void toString(ConnectionFactory connFactory) {
    logger.info("[ Username: " + connFactory.getUsername() + " | Password: ****** | Virtual Host: "
            + connFactory.getVirtualHost() + " | Host: " + connFactory.getHost() + " | Port: "
            + connFactory.getPort() + " ]");
}

From source file:zipkin2.autoconfigure.collector.rabbitmq.ZipkinRabbitMQCollectorPropertiesTest.java

License:Apache License

@Test
public void uriProperlyParsedAndIgnoresOtherProperties_whenUriSet() throws Exception {
    properties.setUri(URI.create("amqp://admin:admin@localhost:5678/myv"));
    properties.setAddresses(Collections.singletonList("will_not^work!"));
    properties.setUsername("bob");
    properties.setPassword("letmein");
    properties.setVirtualHost("drwho");

    assertThat(properties.toBuilder()).extracting("connectionFactory").allSatisfy(object -> {
        ConnectionFactory connFactory = (ConnectionFactory) object;
        assertThat(connFactory.getHost()).isEqualTo("localhost");
        assertThat(connFactory.getPort()).isEqualTo(5678);
        assertThat(connFactory.getUsername()).isEqualTo("admin");
        assertThat(connFactory.getPassword()).isEqualTo("admin");
        assertThat(connFactory.getVirtualHost()).isEqualTo("myv");
    });/*from   w  ww .  ja v  a  2 s. c o  m*/
}