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:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java

License:Apache License

public ConnectionFactory createConnectionFactory(String key) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    String username = getValueOrDefault("amqp_username", key);
    if (username != null) {
        factory.setUsername(username);// w  ww  .  j av  a 2  s . c  om
    }
    String password = getValueOrDefault("amqp_password", key);
    if (password != null) {
        factory.setPassword(password);
    }
    String virtualHost = getValueOrDefault("amqp_virtual_host", key);
    if (virtualHost != null) {
        factory.setVirtualHost(virtualHost);
    }
    String host = getValueOrDefault("amqp_host", key);
    if (host != null) {
        factory.setHost(host);
    }
    Integer port = getIntegerOrDefault("amqp_port", key);
    if (port != null) {
        factory.setPort(port);
    }
    return factory;
}

From source file:NamedPositionController.NamedPositionControllerImplAbstract.java

License:Open Source License

public NamedPositionControllerImplAbstract() {

    mLogger = Logger.getLogger("NamedPositionController");
    PropertyConfigurator.configure("log4j.properties");

    try {/* ww  w . j ava 2  s . co  m*/
        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        /*
         * Defines that messages in this queue are persistent (i.e. to be saved on disk)    
         * so that even if RabbitMQ server quits the messages is not lost.
         * Note that when publishing a message, the msg itself has to be declared persistent.
         */
        boolean durable = false; // for commands we don't want it!

        /*
         * true if the server should consider messages acknowledged once delivered; 
         * false if the server should expect explicit acknowledgements.
         */
        boolean autoAck = true;

        // command queue      
        boolean exclusive = false; // restricted to this connection
        boolean autoDelete = false; // server will delete the queue when no longer in use
        mChannel.queueDeclare(CMD_QUEUE_NAME, durable, exclusive, autoDelete, null);

        // broadcasted commands (don't need to subscribe to a topic)
        mChannel.exchangeDeclare(CMD_EXCHANGE_NAME, "fanout", durable);
        String cmdFanoutQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(cmdFanoutQueueName, CMD_EXCHANGE_NAME, "");

        // data topics to subscribe
        durable = true; // for data we want persistance!
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String dataPubsubQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "MotorHealthSV");
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "NamedPositionSV");
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "Goal");

        // define which queues to get the messages from 
        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(CMD_QUEUE_NAME, autoAck, mConsumer);
        mChannel.basicConsume(cmdFanoutQueueName, autoAck, mConsumer);
        mChannel.basicConsume(dataPubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        mLogger.error("Could not create RMQ channel: " + e.getMessage());
        return;
    }

    try {
        mEngine = new SMEngine(mLogger, SMJavaInvoker.class);
        // The context has to be created before the actions/activities are executed
        mEngine.setContextVar("CHANNEL", mChannel);
        mEngine.setContextVar("CMD_QUEUE_NAME", CMD_QUEUE_NAME);
        mEngine.setContextVar("CMD_EXCHANGE_NAME", CMD_EXCHANGE_NAME);
        mEngine.setContextVar("DATA_EXCHANGE_NAME", DATA_EXCHANGE_NAME);
        mEngine.loadModel("NamedPositionController.xml");
        mEngine.startExecution();
    } catch (IOException e) {
        mLogger.error("Could not start SM execution: " + e.getMessage());
        return;
    }

    mIsRunning = true;
}

From source file:NamedPositionController.SMActivity.java

License:Open Source License

public void openConnection() {
    try {/*from   w w w. j  av  a2 s  . c  o m*/
        boolean durable = true; // for data we want persistance
        boolean autoAck = true;

        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        // data pub/sub
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String pubsubQueueName = mChannel.queueDeclare().getQueue();

        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "MotorHealthSV");
        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "NamedPositionSV");
        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "Goal");

        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(pubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        getLogger().error("Could not create RMQ channel: " + e.getMessage());
        stopRunning();
    }
}

From source file:NamedPositionEstimator.NamedPositionEstimatorImplAbstract.java

License:Open Source License

public NamedPositionEstimatorImplAbstract() {

    mLogger = Logger.getLogger("NamedPositionEstimator");
    PropertyConfigurator.configure("log4j.properties");

    try {//from w  w w  .j ava 2  s.  c o m
        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        /*
         * Defines that messages in this queue are persistent (i.e. to be saved on disk)    
         * so that even if RabbitMQ server quits the messages is not lost.
         * Note that when publishing a message, the msg itself has to be declared persistent.
         */
        boolean durable = false; // for commands we don't want it!

        /*
         * true if the server should consider messages acknowledged once delivered; 
         * false if the server should expect explicit acknowledgements.
         */
        boolean autoAck = true;

        // command queue      
        boolean exclusive = false; // restricted to this connection
        boolean autoDelete = false; // server will delete the queue when no longer in use
        mChannel.queueDeclare(CMD_QUEUE_NAME, durable, exclusive, autoDelete, null);

        // broadcasted commands (don't need to subscribe to a topic)
        mChannel.exchangeDeclare(CMD_EXCHANGE_NAME, "fanout", durable);
        String cmdFanoutQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(cmdFanoutQueueName, CMD_EXCHANGE_NAME, "");

        // data topics to subscribe
        durable = true; // for data we want persistance!
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String dataPubsubQueueName = mChannel.queueDeclare().getQueue();
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "GetActPos");
        mChannel.queueBind(dataPubsubQueueName, DATA_EXCHANGE_NAME, "EncoderHealthSV");

        // define which queues to get the messages from 
        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(CMD_QUEUE_NAME, autoAck, mConsumer);
        mChannel.basicConsume(cmdFanoutQueueName, autoAck, mConsumer);
        mChannel.basicConsume(dataPubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        mLogger.error("Could not create RMQ channel: " + e.getMessage());
        return;
    }

    try {
        mEngine = new SMEngine(mLogger, SMJavaInvoker.class);
        // The context has to be created before the actions/activities are executed
        mEngine.setContextVar("CHANNEL", mChannel);
        mEngine.setContextVar("CMD_QUEUE_NAME", CMD_QUEUE_NAME);
        mEngine.setContextVar("CMD_EXCHANGE_NAME", CMD_EXCHANGE_NAME);
        mEngine.setContextVar("DATA_EXCHANGE_NAME", DATA_EXCHANGE_NAME);
        mEngine.loadModel("NamedPositionEstimator.xml");
        mEngine.startExecution();
    } catch (IOException e) {
        mLogger.error("Could not start SM execution: " + e.getMessage());
        return;
    }

    mIsRunning = true;
}

From source file:NamedPositionEstimator.SMActivity.java

License:Open Source License

public void openConnection() {
    try {/*from  www.  j  ava 2  s  . co  m*/
        boolean durable = true; // for data we want persistance
        boolean autoAck = true;

        mFactory = new ConnectionFactory();
        mFactory.setHost("localhost");
        mConnection = mFactory.newConnection();
        mChannel = mConnection.createChannel();

        // data pub/sub
        mChannel.exchangeDeclare(DATA_EXCHANGE_NAME, "topic", durable);
        String pubsubQueueName = mChannel.queueDeclare().getQueue();

        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "GetActPos");
        mChannel.queueBind(pubsubQueueName, DATA_EXCHANGE_NAME, "EncoderHealthSV");

        mConsumer = new QueueingConsumer(mChannel);
        mChannel.basicConsume(pubsubQueueName, autoAck, mConsumer);

    } catch (IOException e) {
        getLogger().error("Could not create RMQ channel: " + e.getMessage());
        stopRunning();
    }
}

From source file:net.duckling.falcon.api.mq.DFMQManager.java

License:Apache License

public DFMQManager(String userName, String password, String host) {
    ConnectionFactory factory = new ConnectionFactory();
    try {/*from w  ww.j a v a 2 s .  com*/
        factory.setUsername(userName);
        factory.setPassword(password);
        factory.setHost(host);
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.duckling.falcon.api.mq.impl.DFMQBasePublisherClient.java

License:Apache License

public DFMQBasePublisherClient(String username, String password, String host) {
    this.host = host;
    this.password = password;
    this.username = username;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);//from  www.  j av a2  s. c om
    factory.setPassword(password);
    factory.setUsername(username);
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:net.duckling.falcon.api.mq.impl.DFMQBaseSubscriberClient.java

License:Apache License

public DFMQBaseSubscriberClient(String username, String password, String host) {
    this.host = host;
    this.username = username;
    this.password = password;
    this.ssm = new SimpleStringMatch();
    this.handlerMap = new HashMap<String, IDFMessageHandler>();
    ConnectionFactory factory = new ConnectionFactory();
    try {//  www.  j a va 2 s  .  c om
        factory.setHost(host);
        factory.setUsername(username);
        factory.setPassword(password);
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } catch (ShutdownSignalException e) {
        LOG.error(e.getMessage(), e);
    } catch (ConsumerCancelledException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:net.echinopsii.ariane.community.messaging.rabbitmq.Client.java

License:Open Source License

/**
 * Initialize RabbitMQ connection with provided properties and this client ServiceFactory.
 * <br/>//from  w  w w.j a  v a2 s.com
 * Following properties fields MUST be defined :
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_HOST}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_PORT}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_VHOST}
 * <br/>
 * Following properties fields MAY be defined:
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_USER}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_PSWD}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_CLI_MSG_DEBUG_ON_TIMEOUT}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_CLI_ROUTEES_NB_PER_SERVICE}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_CLI_RPC_TIMEOUT}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#MOM_CLI_RPC_RETRY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_INFORMATION_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_PRODUCT_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_PLATFORM_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_COPYRIGHT_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#RBQ_VERSION_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_APP_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_CMP_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_OSI_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_OTM_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_PGURL_KEY}
 * {@link net.echinopsii.ariane.community.messaging.api.MomClient#ARIANE_PID_KEY}
 * @param properties configuration properties
 * @throws IOException if problems to join NATS server
 */
@Override
public void init(Dictionary properties) throws IOException {
    if (properties.get(MomClient.RBQ_INFORMATION_KEY) != null)
        super.setClientID((String) properties.get(MomClient.RBQ_INFORMATION_KEY));
    if (properties.get(MOM_CLI_MSG_DEBUG_ON_TIMEOUT) != null
            && (((String) properties.get(MOM_CLI_MSG_DEBUG_ON_TIMEOUT)).toLowerCase().equals("true")))
        super.setMsgDebugOnTimeout(true);
    if (properties.get(MOM_CLI_ROUTEES_NB_PER_SERVICE) != null)
        super.setRouteesCountPerService(new Integer((String) properties.get(MOM_CLI_ROUTEES_NB_PER_SERVICE)));
    try {
        if (Class.forName("akka.osgi.ActorSystemActivator") != null
                && MessagingAkkaSystemActivator.getSystem() != null)
            super.setActorSystem(MessagingAkkaSystemActivator.getSystem());
        else
            super.setActorSystem(ActorSystem.create("MySystem"));
    } catch (ClassNotFoundException e) {
        super.setActorSystem(ActorSystem.create("MySystem"));
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost((String) properties.get(MOM_HOST));
    factory.setPort(new Integer((String) properties.get(MOM_PORT)));
    if (properties.get(RBQ_VHOST) != null)
        factory.setVirtualHost((String) properties.get(RBQ_VHOST));
    if (properties.get(MOM_USER) != null)
        factory.setUsername((String) properties.get(MOM_USER));
    if (properties.get(MOM_PSWD) != null)
        factory.setPassword((String) properties.get(MOM_PSWD));

    Map<String, Object> factoryProperties = factory.getClientProperties();
    if (properties.get(MomClient.RBQ_PRODUCT_KEY) != null)
        factoryProperties.put(RBQ_PRODUCT_KEY, properties.get(MomClient.RBQ_PRODUCT_KEY));
    if (properties.get(MomClient.RBQ_INFORMATION_KEY) != null)
        factoryProperties.put(RBQ_INFORMATION_KEY, super.getClientID());
    if (properties.get(MomClient.RBQ_PLATFORM_KEY) != null)
        factoryProperties.put(RBQ_PLATFORM_KEY, properties.get(MomClient.RBQ_PLATFORM_KEY));
    else
        factoryProperties.put(RBQ_PLATFORM_KEY, "Java " + System.getProperty("java.version"));
    if (properties.get(MomClient.RBQ_COPYRIGHT_KEY) != null)
        factoryProperties.put(RBQ_COPYRIGHT_KEY, properties.get(MomClient.RBQ_COPYRIGHT_KEY));
    if (properties.get(MomClient.RBQ_VERSION_KEY) != null)
        factoryProperties.put(RBQ_VERSION_KEY, properties.get(MomClient.RBQ_VERSION_KEY));

    Enumeration keys = properties.keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        if (key instanceof String && ((String) key).startsWith(MomClient.ARIANE_KEYS))
            factoryProperties.put((String) key, properties.get((String) key));
    }

    connection = factory.newConnection();

    super.setServiceFactory(new ServiceFactory(this));
}

From source file:net.es.netshell.controller.client.SdnControllerClient.java

License:Open Source License

public SdnControllerClient() {
    try {/*  w w  w  . j a v  a 2  s . c om*/
        // Get a connection to the AMPQ broker (RabbitMQ server)
        factory = new ConnectionFactory();
        String rabbitmqHost = "localhost";
        if (NetShellConfiguration.getInstance() != null
                && NetShellConfiguration.getInstance().getGlobal() != null) {
            rabbitmqHost = NetShellConfiguration.getInstance().getGlobal().getMessagingHost();
        }
        factory.setHost(rabbitmqHost);

        connection = factory.newConnection();
        replyChannel = connection.createChannel();

        // Set up to receive replies from the SDN controller when we get them
        replyQueueName = replyChannel.queueDeclare().getQueue();
        replyConsumer = new QueueingConsumer(replyChannel);
        replyChannel.basicConsume(replyQueueName, true, replyConsumer);

        // JSON parser setup
        mapper = new ObjectMapper();

        logger.info(SdnControllerClient.class.getName() + " ready with connection to " + rabbitmqHost);

    } catch (Exception e) {
        e.printStackTrace();
    }
}