Example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory

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

Introduction

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

Prototype

ConnectionFactory

Source Link

Usage

From source file:org.apache.flink.streaming.connectors.rabbitmq.RMQSink.java

License:Apache License

/**
 * Initializes the connection to RMQ./*from   w ww .j  a  v a 2 s  . co m*/
 */
public void initializeConnection() {
    factory = new ConnectionFactory();
    factory.setHost(HOST_NAME);
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    initDone = true;
}

From source file:org.apache.flink.streaming.connectors.rabbitmq.RMQSource.java

License:Apache License

/**
 * Initializes the connection to RMQ.//from w w  w.  j  a v  a2  s  .  c o  m
 */
private void initializeConnection() {
    factory = new ConnectionFactory();
    factory.setHost(HOST_NAME);
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
    } catch (IOException e) {
        throw new RuntimeException("Cannot create RMQ connection with " + QUEUE_NAME + " at " + HOST_NAME, e);
    }
}

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

License:Apache License

@VisibleForTesting
static ConnectionFactory createConnectionFactoryFrom(Context context) {
    String host = context.getString(AmqpSourceConfigurationConstants.HOST, Constants.Defaults.HOST);
    int port = context.getInteger(AmqpSourceConfigurationConstants.PORT, Constants.Defaults.PORT);
    String virtualHost = context.getString(AmqpSourceConfigurationConstants.VIRTUAL_HOST,
            Constants.Defaults.VIRTUAL_HOST);
    String userName = context.getString(AmqpSourceConfigurationConstants.USER_NAME,
            Constants.Defaults.USER_NAME);
    String password = context.getString(AmqpSourceConfigurationConstants.PASSWORD, Constants.Defaults.PASSWORD);
    int connectionTimeout = context.getInteger(AmqpSourceConfigurationConstants.CONNECTION_TIMEOUT,
            Constants.Defaults.CONNECTION_TIMEOUT);
    int requestHeartbeat = context.getInteger(AmqpSourceConfigurationConstants.REQUEST_HEARTBEAT,
            Constants.Defaults.REQUESTED_HEARTBEAT);

    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(host);//from  w  w  w  .ja  v a 2 s.c  o  m
    connectionFactory.setPort(port);
    connectionFactory.setVirtualHost(virtualHost);
    connectionFactory.setUsername(userName);
    connectionFactory.setPassword(password);
    connectionFactory.setConnectionTimeout(connectionTimeout);
    connectionFactory.setRequestedHeartbeat(requestHeartbeat);

    return connectionFactory;
}

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

License:Apache License

public static ConnectionFactory getFactory(Context context) {
    Preconditions.checkArgument(context != null, "context cannot be null.");
    ConnectionFactory factory = new ConnectionFactory();

    String hostname = context.getString("hostname");
    Preconditions.checkArgument(hostname != null, "No hostname specified.");
    factory.setHost(hostname);/*  w  w  w . j  a  v a  2  s .c o  m*/

    int port = context.getInteger(RabbitMQConstants.CONFIG_PORT, -1);

    if (-1 != port) {
        factory.setPort(port);
    }

    String username = context.getString(RabbitMQConstants.CONFIG_USERNAME);

    if (null == username) {
        factory.setUsername(ConnectionFactory.DEFAULT_USER);
    } else {
        factory.setUsername(username);
    }

    String password = context.getString(RabbitMQConstants.CONFIG_PASSWORD);

    if (null == password) {
        factory.setPassword(ConnectionFactory.DEFAULT_PASS);
    } else {
        factory.setPassword(password);
    }

    String virtualHost = context.getString(RabbitMQConstants.CONFIG_VIRTUALHOST);

    if (null != virtualHost) {
        factory.setVirtualHost(virtualHost);
    }

    int connectionTimeout = context.getInteger(RabbitMQConstants.CONFIG_CONNECTIONTIMEOUT, -1);

    if (connectionTimeout > -1) {
        factory.setConnectionTimeout(connectionTimeout);
    }

    //        boolean useSSL = context.getBoolean("usessl", false);
    //        if(useSSL){
    //            factory.useSslProtocol();
    //        }

    return factory;
}

From source file:org.apache.helix.recipes.rabbitmq.ConsumerThread.java

License:Apache License

@Override
public void run() {
    Connection connection = null;
    try {//from   w  w w .  jav  a 2 s . co m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(_mqServer);
        connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String queueName = channel.queueDeclare().getQueue();

        String bindingKey = _partition.toString();
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

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

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            String routingKey = delivery.getEnvelope().getRoutingKey();

            System.out.println(" [x] " + _consumerId + " Received '" + routingKey + "':'" + message + "'");
        }
    } catch (InterruptedException e) {
        System.err.println(" [-] " + _consumerId + " on " + _partition + " is interrupted ...");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:org.apache.helix.recipes.rabbitmq.Emitter.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("USAGE: java Emitter rabbitmqServer (e.g. localhost) numberOfMessage (optional)");
        System.exit(1);/*from   w  w w . j  a va  2  s . c  o  m*/
    }

    final String mqServer = args[0]; // "zzhang-ld";
    int count = Integer.MAX_VALUE;
    if (args.length > 1) {
        try {
            count = Integer.parseInt(args[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    System.out.println("Sending " + count + " messages with random topic id");

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(mqServer);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    for (int i = 0; i < count; i++) {
        int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER);
        String routingKey = "topic_" + rand;
        String message = "message_" + rand;

        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

        Thread.sleep(1000);
    }

    connection.close();
}

From source file:org.apache.james.backend.rabbitmq.DockerRabbitMQ.java

License:Apache License

public ConnectionFactory connectionFactory() {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(getHostIp());
    connectionFactory.setPort(getPort());
    connectionFactory.setUsername(getUsername());
    connectionFactory.setPassword(getPassword());
    return connectionFactory;
}

From source file:org.apache.james.backend.rabbitmq.DockerRabbitMQExtensionTest.java

License:Apache License

@BeforeEach
public void setup(DockerRabbitMQ rabbitMQ) {
    connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(rabbitMQ.getHostIp());
    connectionFactory.setPort(rabbitMQ.getPort());
    connectionFactory.setUsername(rabbitMQ.getUsername());
    connectionFactory.setPassword(rabbitMQ.getPassword());
}

From source file:org.apache.james.backend.rabbitmq.RabbitMQConnectionFactory.java

License:Apache License

private ConnectionFactory from(RabbitMQConfiguration rabbitMQConfiguration) {
    try {/*  w  ww . j a va 2s  .  co m*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(rabbitMQConfiguration.getUri());
        connectionFactory.useNio();
        return connectionFactory;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.james.transport.mailets.amqp.AmqpRule.java

License:Apache License

@Override
protected void before() throws Throwable {
    amqpUri = "amqp://" + rabbitMqContainer.getIp();
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(amqpUri);//from ww  w  .j  ava 2  s .  c  om
    waitingForRabbitToBeReady(factory);
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT);
    queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, exchangeName, routingKey);
}