Example usage for com.rabbitmq.client ConnectionFactory newConnection

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

Introduction

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

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

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 w w.j  a  va 2 s  .  co  m*/
    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);// w w  w .  j  a v a  2s.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 a 2 s .c  om
    connection.close();
}

From source file:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java

License:Apache License

public Channel declareChannel(ConnectionFactory factory, String key) throws Exception {
    Connection conn = factory.newConnection();
    Channel channel = conn.createChannel();
    Integer basicQos = conf.getInteger("channel_basic_qos");
    if (basicQos != null) {
        channel.basicQos(basicQos);/*from  ww w. j a  v  a  2  s  .  co  m*/
    } else {
        channel.basicQos(DEFAULT_QOS);
    }
    channel.exchangeDeclare(getExchangeName(key), getExchangeType(key), true);
    return channel;
}

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 www.j  a v a 2 s.c om
        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  a  v  a2 s  .  c  o  m
    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 {//from  w  w w. j a v a2s .  c  o m
        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/>//w ww  .  j  a va2s . c  o m
 * 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.impl.SdnController.java

License:Open Source License

public SdnController() {
    try {//ww w  .  jav  a 2s . c  om
        // Get a connection to the AMPQ broker (e.g. RabbitMQ server)
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        // Create the SDN controller queue, non-durable, non-exclusive, non-auto-delete, no other args
        channel.queueDeclare(Common.controllerRequestQueueName, false, false, false, null);
        channel.basicQos(1); // what does this do?
        // Set up a consumer who will read messages from the queue
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(Common.controllerRequestQueueName, false, consumer);

        // Create exchange for notifications
        channel.exchangeDeclare(Common.notificationExchangeName, "fanout");

        // XXX we need to do some error-checking here to handle the case that the AMPQ server is dead
        // or unreachable.

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

        // OpenFlow controller setup
        controller = Controller.getInstance();

        logger.info(SdnController.class.getName() + " ready");

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

From source file:net.es.netshell.rabbitmq.Consume.java

License:Open Source License

public void consumeMessage() throws Exception {
    if (queueName == null) {
        queueName = new UUIDManager(QUEUE_FILE).checkUUID();
    }//from ww w  .j a v  a  2 s. co m
    ConnectionFactory factory = new SSLConnection(info).createConnection();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(queueName, false, false, true, null);
    System.out.println(" [*] Waiting for messages.");

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

    //while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    ByteArrayInputStream bais = new ByteArrayInputStream(delivery.getBody());
    ObjectInputStream in = new ObjectInputStream(bais);
    DefaultListenableGraph g = (DefaultListenableGraph) in.readObject();
    System.out.println(" [x] Received Message");

    // GraphViewer view = new GraphViewer(g);
    // view.init();

    //         if (message.substring(0,13).equals("TOKEN_REQUEST")) {
    //            String[] token = new ProcessTokenRequest(message, channel).sendToken();
    //            permissions.put(token[0], token[1]);
    //            //String[] messageSplit = message.split(":");
    //            //sendToken(messageSplit[1], messageSplit[2], channel);
    //         } else {
    //            String[] messageSplit = message.split(":", 2);
    //            if (permissions.containsKey(messageSplit[0])) {
    //               System.out.println(" [x] Received '" + messageSplit[1] + "' from: " + permissions.get(messageSplit[0]));
    //            } else {
    //               System.out.println(" ERROR: INVALID TOKEN PROVIDED IN MESSAGE");
    //            }
    //         }
    //}

    channel.queueDelete(queueName);
    channel.close();
    connection.close();
}