Example usage for com.rabbitmq.client ConnectionFactory getHost

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

Introduction

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

Prototype

public String getHost() 

Source Link

Usage

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

License:Open Source License

/**
 * Build an instance.//from w  w  w.  jav  a2 s  . c  om
 *
 * @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:net.roboconf.messaging.internal.utils.RabbitMqUtilsTest.java

License:Apache License

@Test
public void testConfigureFactory() throws Exception {

    String address = "http://roboconf.net/some/path";
    int port = 18547;
    String username = "toto";
    String password = "123456789";

    ConnectionFactory factory = new ConnectionFactory();
    Assert.assertNotSame(address, factory.getHost());
    Assert.assertNotSame(port, factory.getPort());

    RabbitMqUtils.configureFactory(factory, "http://roboconf.net:" + port + "/some/path", username, password);
    Assert.assertEquals(address, factory.getHost());
    Assert.assertEquals(port, factory.getPort());
    Assert.assertEquals(username, factory.getUsername());
    Assert.assertEquals(password, factory.getPassword());
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtilsTest.java

License:Apache License

@Test
public void testConfigureFactory() throws Exception {

    String address = "http://roboconf.net/some/path";
    int port = 18547;
    String username = "toto";
    String password = "123456789";

    ConnectionFactory factory = new ConnectionFactory();
    Assert.assertNotSame(address, factory.getHost());
    Assert.assertNotSame(port, factory.getPort());

    Map<String, String> configuration = new HashMap<>();
    configuration.put(RABBITMQ_SERVER_IP, "http://roboconf.net:" + port + "/some/path");
    configuration.put(RABBITMQ_SERVER_USERNAME, username);
    configuration.put(RABBITMQ_SERVER_PASSWORD, password);

    RabbitMqUtils.configureFactory(factory, configuration);
    Assert.assertEquals(address, factory.getHost());
    Assert.assertEquals(port, factory.getPort());
    Assert.assertEquals(username, factory.getUsername());
    Assert.assertEquals(password, factory.getPassword());
}

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());
    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());//w ww. j  ava 2 s .  com
    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.flume.RabbitMQUtilTest.java

License:Apache License

public void getFactory_minimal() {
    context = new Context();
    context.put(RabbitMQConstants.CONFIG_HOSTNAME, "server01.example.com");

    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());
}

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  2s  . c  om
 * @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.springframework.amqp.rabbit.connection.LocalizedQueueConnectionFactoryTests.java

License:Apache License

@SuppressWarnings("unchecked")
private ConnectionFactory mockCF(final String address, final CountDownLatch latch) throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Channel channel = mock(Channel.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    when(connection.createChannel(false)).thenReturn(channel);
    when(connection.isOpen()).thenReturn(true, false);
    when(channel.isOpen()).thenReturn(true, false);
    doAnswer(invocation -> {// w  w w .  jav  a2s.c  o m
        String tag = UUID.randomUUID().toString();
        consumers.put(address, invocation.getArgumentAt(6, Consumer.class));
        consumerTags.put(address, tag);
        if (latch != null) {
            latch.countDown();
        }
        return tag;
    }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            any(Consumer.class));
    when(connectionFactory.getHost()).thenReturn(address);
    this.channels.put(address, channel);
    return connectionFactory;
}

From source file:org.springframework.amqp.rabbit.junit.BrokerRunningTests.java

License:Apache License

@Test
public void testVars() {
    BrokerRunning brokerRunning = BrokerRunning.isBrokerAndManagementRunning();
    brokerRunning.setAdminPassword("foo");
    brokerRunning.setAdminUser("bar");
    brokerRunning.setHostName("baz");
    brokerRunning.setPassword("qux");
    brokerRunning.setPort(1234);// w ww  .j  a v a  2s.com
    brokerRunning.setUser("fiz");

    assertEquals("http://baz:15672/api/", brokerRunning.getAdminUri());
    ConnectionFactory connectionFactory = brokerRunning.getConnectionFactory();
    assertEquals("baz", connectionFactory.getHost());
    assertEquals(1234, connectionFactory.getPort());
    assertEquals("fiz", connectionFactory.getUsername());
    assertEquals("qux", connectionFactory.getPassword());
}

From source file:org.springframework.amqp.rabbit.junit.BrokerRunningTests.java

License:Apache License

@Test
public void testEnvironmentVars() {
    Map<String, String> vars = new HashMap<>();
    vars.put("RABBITMQ_TEST_ADMIN_PASSWORD", "FOO");
    vars.put("RABBITMQ_TEST_ADMIN_URI", "http://foo/bar");
    vars.put("RABBITMQ_TEST_ADMIN_USER", "BAR");
    vars.put("RABBITMQ_TEST_HOSTNAME", "BAZ");
    vars.put("RABBITMQ_TEST_PASSWORD", "QUX");
    vars.put("RABBITMQ_TEST_PORT", "2345");
    vars.put("RABBITMQ_TEST_USER", "FIZ");
    BrokerRunning.setEnvironmentVariableOverrides(vars);
    BrokerRunning brokerRunning = BrokerRunning.isBrokerAndManagementRunning();

    assertEquals("http://foo/bar", brokerRunning.getAdminUri());
    ConnectionFactory connectionFactory = brokerRunning.getConnectionFactory();
    assertEquals("BAZ", connectionFactory.getHost());
    assertEquals(2345, connectionFactory.getPort());
    assertEquals("FIZ", connectionFactory.getUsername());
    assertEquals("QUX", connectionFactory.getPassword());
    DirectFieldAccessor dfa = new DirectFieldAccessor(brokerRunning);
    assertEquals("BAR", dfa.getPropertyValue("adminUser"));
    assertEquals("FOO", dfa.getPropertyValue("adminPassword"));

    BrokerRunning.clearEnvironmentVariableOverrides();
}