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:mailmonk.client.MailMonk.java

License:Apache License

/**
 * Connect to a Message Queue and open a channel for 
 * sending Mail Monk jobs.//w w  w. jav a 2 s.c o  m
 * */
public void connect() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
        factory.setHost(getHost());

        connection = factory.newConnection();

        channel = connection.createChannel();
        channel.queueDeclare(getQueueName(), false, false, false, null);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:main.TestMain.java

public static void sendMessage(String message) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/* ww w .j av a2s  .co  m*/
    connection.close();
}

From source file:mains.RaspiSensorModule_0.java

public static void main(String[] argv) throws Exception {

    //________ Basic subscribe direct (Routing) __________
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);//from  w  ww .j  a va 2 s  .com
    factory.setUsername("piadmin");
    factory.setPassword("3828");
    Connection connection = factory.newConnection();
    System.setProperty("java.rmi.server.hostname", thisIp);

    ControllerPublisher conPub = new ControllerPublisher(connection, NODE_ID);
    LED_Controller ledCon = new LED_Controller("ledControllerRealTest");
    LCD_Controller lcdCon = new LCD_Controller("lcdController_messeges");

    //   NodeController ledCon1 = new NodeController("ledControllerFloor");
    //   NodeController ledCon2 = new NodeController("ledControllerCabin");

    conPub.publishController(ledCon);
    conPub.publishController(lcdCon);

    //     conPub.publishController(ledCon1);
    //   conPub.publishController(ledCon2);

    NodeSensorHandler nodeSensors = new NodeSensorHandler(connection, SENSOR_EXCHANGE,
            SENSOR_ROUT_KEY_BASE + NODE_ID);
    System.out.println("Sensors up");
    //        BusRMIConnector rmiConnection = new BusRMIConnector(connection, SENSOR_EXCHANGE, SENSOR_ROUT_KEY_BASE + NODE_ID);
    //        System.out.println("RMI up");

    System.in.read();

    connection.close();
}

From source file:mapas.Mapas.java

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPassword("test");
    factory.setUsername("test");
    final Connection connection = factory.newConnection();

    final Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);//ww w  . j a v a2  s . co  m

    final Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            System.out.println(" [x] Received '" + message + "'");
            try {
                doWork(message);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println(" [x] Done");
            channel.basicAck(envelope.getDeliveryTag(), false);

        }
    };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
}

From source file:messaging.Worker.java

License:Apache License

public static void sendMessage(String message) throws java.io.IOException {

    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.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/*from   ww w  .  ja  v  a2  s .c  o m*/
    connection.close();
}

From source file:mobisocial.musubi.service.AMQPService.java

License:Apache License

private void initializeAMQP() {
    mIdentitiesManager = new IdentitiesManager(mDatabaseSource);
    mDeviceManager = new DeviceManager(mDatabaseSource);
    mEncodedMessageManager = new EncodedMessageManager(mDatabaseSource);

    mConnectionFactory = new ConnectionFactory();
    mConnectionFactory.setHost(AMQP_SERVICE);
    mConnectionFactory.setConnectionTimeout(30 * 1000);
    mConnectionFactory.setRequestedHeartbeat(30);

    mThread = new HandlerThread("AMQP");
    mThread.setPriority(Thread.MIN_PRIORITY);
    mThread.start();/* w ww . j av  a 2 s.  co  m*/

    mAMQPHandler = new Handler(mThread.getLooper());

    //start the connection
    mAMQPHandler.post(new Runnable() {
        @Override
        public void run() {
            initiateConnection();
        }
    });
}

From source file:Monitor.MonitorImplAbstract.java

License:Open Source License

public MonitorImplAbstract() {

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

    try {//from   w w w. j  ava2  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, "EncoderHealthSV");
        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("Monitor.xml");
        mEngine.startExecution();
    } catch (IOException e) {
        mLogger.error("Could not start SM execution: " + e.getMessage());
        return;
    }

    mIsRunning = true;
}

From source file:Monitor.SMActivity.java

License:Open Source License

public void openConnection() {
    try {//from  w  ww  .  j a va  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, "EncoderHealthSV");
        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:MotorHealthEstimator.MotorHealthEstimatorImplAbstract.java

License:Open Source License

public MotorHealthEstimatorImplAbstract() {

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

    try {//  ww w. ja v  a  2s  .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, "GetMotorHealthFlag");

        // 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("MotorHealthEstimator.xml");
        mEngine.startExecution();
    } catch (IOException e) {
        mLogger.error("Could not start SM execution: " + e.getMessage());
        return;
    }

    mIsRunning = true;
}

From source file:MotorHealthEstimator.SMActivity.java

License:Open Source License

public void openConnection() {
    try {/* w  ww.ja  v a  2 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, "GetMotorHealthFlag");

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

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