Example usage for com.rabbitmq.client ConnectionFactory getPort

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

Introduction

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

Prototype

public int getPort() 

Source Link

Usage

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

License:Open Source License

/**
 * Build an instance./*ww w.j a  v a 2  s .c o 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: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());/*  ww  w  .ja v  a  2  s  .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  . ja  v  a2  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.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);/*from w ww.  j  a  va  2 s . c om*/
    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();
}

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:zebrogamq.gamelogic.ChannelsManager.java

License:Open Source License

private ConnectionFactory getConnectionFactory(final GameLogicState state) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(Util.getRabbitMQProperties().getProperty("gameLogicBrokerHost"));
    factory.setUsername(state.login);//  ww w. j av a 2  s .c o m
    factory.setPassword(state.password);
    factory.setVirtualHost(state.virtualHost);
    factory.setPort(Integer.valueOf(Util.getRabbitMQProperties().getProperty("gameLogicBrokerPort",
            String.valueOf(factory.getPort()))));
    factory.setConnectionTimeout(CONNECTION_ESTABLISHMENT_TIMEOUT);
    // set the heartbeat value for the amqp connections
    // to avoid the keeping of obsolete consumers
    factory.setRequestedHeartbeat(Integer.valueOf(Util.getRabbitMQProperties()
            .getProperty("amqpConnectionHeartbeat", String.valueOf(factory.getRequestedHeartbeat()))));
    return factory;
}