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.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/* w  w  w .  j  av a2  s  .  c  om*/
        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());
}

From source file:org.jenkinsci.plugins.pushreceiver.RabbitMQConnector.java

License:Apache License

private RabbitMQConnector(String server, String routing) throws InstantiationException {
    try {/*  w  w  w  .  j av  a  2 s.co  m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(server);
        connection = factory.newConnection();
        channel = connection.createChannel();

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

        if (routing == null || "".equals(routing.trim())) {
            routing = "#";
        }
        if (!routing.endsWith("#")) {
            routing = routing + ".#";
        }

        channel.queueBind(queueName, EXCHANGE_NAME, routing);

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

        running = true;
        thread = new Thread(this);
        thread.start();
    } catch (IOException e) {
        LOGGER.severe(
                "Could not create Rabbit MQ Connection. Please verify server name in the global configuration. "
                        + e);
        throw new InstantiationException();
    }
}

From source file:org.kairosdb.plugin.rabbitmq.core.RabbitmqService.java

License:MIT License

@Override
public void start() throws KairosDBException {

    try {//from ww w .  j  a  v  a2s.co m
        LOGGER.info("[KRMQ] Starting to RabbitMQ consumer thread.");

        // Socket abstract connection with broker
        ConnectionFactory rabbitmqConnectionFactory = new ConnectionFactory();
        rabbitmqConnectionFactory.setHost(rabbmitmqHost);
        rabbitmqConnectionFactory.setVirtualHost(rabbitmqVirtualHost);
        rabbitmqConnectionFactory.setUsername(rabbitmqUser);
        rabbitmqConnectionFactory.setPassword(rabbitmqPassword);
        rabbitmqConnectionFactory.setPort(rabbitmqPort);
        rabbitmqConnectionFactory.setConnectionTimeout(rabbitmqTimeout);
        rabbitmqConnectionFactory.setRequestedChannelMax(rabbitmqChannelMax);
        rabbitmqConnectionFactory.setRequestedFrameMax(rabbitmqFrameMax);
        rabbitmqConnectionFactory.setRequestedHeartbeat(rabbitmqHearbeat);
        rabbitmqConnectionFactory.setAutomaticRecoveryEnabled(true);

        // Get KairosDatastore implementation
        KairosDatastore kairosDatabase = googleInjector.getInstance(KairosDatastore.class);

        // Create consumer thread
        RabbitmqConsumer consumer = new RabbitmqConsumer(kairosDatabase, rabbitmqConnectionFactory,
                bindingsFile, configurationJSONFieldValue, configurationJSONTimeStamp, configurationJSONTags,
                configurationCSVSeperator, configurationDefaultContentType);

        // Start consumer thread
        consumerThread = new Thread(consumer);
        consumerThread.start();

    } catch (Exception e) {
        LOGGER.error("[KRMQ] An error occurred: ", e);
    }
}

From source file:org.mazerunner.core.messaging.Worker.java

License:Apache License

public void doMain(String[] args) throws Exception {

    CmdLineParser parser = new CmdLineParser(this);

    // if you have a wider console, you could increase the value;
    // here 80 is also the default
    parser.setUsageWidth(80);/*from  w  ww .j a v a2 s.  c  o m*/

    try {
        // parse the arguments.
        parser.parseArgument(args);

        if (sparkMaster == "" || hadoopHdfs == "")
            throw new CmdLineException(parser, "Options required: --hadoop.hdfs <url>, --spark.master <url>");

        ConfigurationLoader.getInstance().setHadoopHdfsUri(hadoopHdfs);
        ConfigurationLoader.getInstance().setSparkHost(sparkMaster);
        ConfigurationLoader.getInstance().setAppName(sparkAppName);
        ConfigurationLoader.getInstance().setExecutorMemory(sparkExecutorMemory);
        ConfigurationLoader.getInstance().setDriverHost(driverHost);
        ConfigurationLoader.getInstance().setRabbitmqNodename(rabbitMqHost);

    } catch (CmdLineException e) {
        // if there's a problem in the command line,
        // you'll get this exception. this will report
        // an error message.
        System.err.println(e.getMessage());
        System.err.println("java -cp $CLASSPATH [<spark-config-options>] <main-class> [<mazerunner-args>]");
        // print the list of available options
        parser.printUsage(System.err);
        System.err.println();

        // print option sample. This is useful some time
        System.err.println("  Example: java -cp $CLASSPATH org.mazerunner.core.messaging.Worker"
                + parser.printExample(ALL));

        return;
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename());
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicQos(20);

    // Initialize spark context
    GraphProcessor.initializeSparkContext();

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

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

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

        System.out.println(" [x] Received '" + message + "'");

        // Deserialize message
        Gson gson = new Gson();
        ProcessorMessage processorMessage = gson.fromJson(message, ProcessorMessage.class);

        // Run PageRank
        GraphProcessor.processEdgeList(processorMessage);

        System.out.println(" [x] Done '" + message + "'");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:org.mule.transport.amqp.AbstractAmqpITCase.java

License:Open Source License

public AbstractAmqpITCase() throws IOException {
    super();// www . ja va 2s.c o m
    setDisposeManagerPerSuite(true);

    factory = new ConnectionFactory();
    factory.setUsername("mule");
    factory.setPassword("elum");
    factory.setVirtualHost("mule-test");
    connection = factory.newConnection();
}

From source file:org.mule.transport.amqp.AmqpConnector.java

License:Open Source License

@Override
public void doInitialise() throws InitialisationException {
    if (connectionFactory == null) {
        connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
    } else {/*from   w  w  w  .  j a  v a  2  s  .  c  o  m*/
        if (connectionFactory.getVirtualHost() != null) {
            setVirtualHost(connectionFactory.getVirtualHost());
        } else {
            connectionFactory.setVirtualHost(virtualHost);
        }
        setUsername(connectionFactory.getUsername());
        setPassword(connectionFactory.getPassword());
        setHost(connectionFactory.getHost());
        setPort(connectionFactory.getPort());
    }
}

From source file:org.mule.transport.amqp.harness.TestConnectionManager.java

License:Open Source License

public Connection getConnection() throws IOException {
    if (connection != null && connection.isOpen()) {
        return connection;
    }//from w  w  w.  j  av  a  2 s. c  o m

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(System.getProperty("amqpHost"));
    factory.setPort(Integer.valueOf(System.getProperty("amqpPort")));
    factory.setUsername(System.getProperty("amqpUserName"));
    factory.setPassword(System.getProperty("amqpPassword"));
    factory.setVirtualHost(System.getProperty("amqpVirtualHost"));
    connection = factory.newConnection();

    return connection;
}