Example usage for com.rabbitmq.client Channel exchangeDeclare

List of usage examples for com.rabbitmq.client Channel exchangeDeclare

Introduction

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

Prototype

Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;

Source Link

Document

Actively declare a non-autodelete, non-durable exchange with no extra arguments

Usage

From source file:rabbitmqapp.EmitLog.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(Cons.RABBIT_MQ_URL);
    factory.setConnectionTimeout(3000);/*from   w  w  w  .  ja v  a  2  s .c  om*/
    factory.setRequestedHeartbeat(30);

    //Create a connection
    Connection connection = factory.newConnection();
    //create a channel from connection
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(Cons.EXCHANGE_NAME, EXCHANGE_TYPE);
    String message = "Hello Dolly";

    channel.basicPublish(Cons.EXCHANGE_NAME, "", null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();
}

From source file:raspisensormodule.BusRMIConnector.java

@Override
public String register(String Sensor_name, HashMap<String, Integer> sensors) throws RemoteException {

    try {/*from   www.  ja  v a 2 s .c  om*/
        System.out.println("Connection from " + RemoteServer.getClientHost());
    } catch (ServerNotActiveException ex) {
        Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

    UseRMI sensorCall = null;
    Channel busChannel = null;
    try {
        busChannel = busConnection.createChannel();
        busChannel.exchangeDeclare(exchange, "topic");
    } catch (IOException ex) {
        Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        System.out.println(RemoteServer.getClientHost());

        sensorCall = (UseRMI) Naming.lookup("rmi://" + RemoteServer.getClientHost() + "/UseRMI");
    } catch (ServerNotActiveException | NotBoundException | MalformedURLException | RemoteException ex) {
        Logger.getLogger(BusRMIConnector.class.getName()).log(Level.SEVERE, null, ex);
    }

    ArrayList<String> sens = new ArrayList<>();
    Integer oldKey = sensors.entrySet().iterator().next().getValue();

    for (Map.Entry<String, Integer> entry : sensors.entrySet()) {
        if (entry.getValue().equals(oldKey)) {
            sens.add(entry.getKey());
        } else {
            RMI_sensor s = new RMI_sensor(oldKey, sens, sensorCall, busChannel, exchange,
                    baseRoutingKey + '.' + Sensor_name + funkyTopic, this);
            Thread t = new Thread(s);
            addThread(t);
            sens = new ArrayList<>();
            sens.add(entry.getKey());
            oldKey = entry.getValue();
        }

    }
    RMI_sensor s = new RMI_sensor(oldKey, sens, sensorCall, busChannel, exchange,
            baseRoutingKey + '.' + Sensor_name + funkyTopic, this);
    Thread t = new Thread(s);
    addThread(t);

    return "" + sensorCount;
}

From source file:raspisensormodule.NodeSensorHandler.java

public NodeSensorHandler(Connection busConnection, String exchange, String baseRoutingKey) throws Exception {
    super(busConnection, exchange, baseRoutingKey);

    initSensors();/*from   w ww .ja  va 2s  .com*/

    Channel channelTemp = null;
    Channel channelPress = null;
    Channel channelLight = null;

    try {
        channelTemp = busConnection.createChannel();
        channelTemp.exchangeDeclare(exchange, "topic");
        channelPress = busConnection.createChannel();
        channelPress.exchangeDeclare(exchange, "topic");
        channelLight = busConnection.createChannel();
        channelLight.exchangeDeclare(exchange, "topic");
    } catch (IOException ex) {
        Logger.getLogger(NodeSensorHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    NodeSensor tempUpdater = new NodeSensor(6000, tempHumSensors, channelTemp, exchange,
            baseRoutingKey + ".temp", this);
    Thread t = new Thread(tempUpdater);
    addThread(t);

    NodeSensor pressUpdater = new NodeSensor(3000, pressureSensors, channelPress, exchange,
            baseRoutingKey + ".pressure", this);
    t = new Thread(pressUpdater);
    addThread(t);

    NodeSensor lightUpdater = new NodeSensor(12000, lightSensors, channelLight, exchange,
            baseRoutingKey + ".light", this);
    t = new Thread(lightUpdater);
    addThread(t);

}

From source file:Release1.Controllers.AlarmController.java

/**
 * mtodo para recibir mensajes de los sensores de alarmas de ventana
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException/* w  ww  .  j  a  v  a2 s  . c o m*/
 * @throws TimeoutException 
 */
private synchronized void receiveAlamrWindowSensorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ID_CHANNEL_AWINDOW_SENSOR, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ID_CHANNEL_AWINDOW_SENSOR, "");

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM WINDOW Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {//si el valor obtenido es mayor al permitido para simular las alarmas se activa la alarma
                try {
                    sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_ON);//envio de mensaje para encender alarma la alarma de ventnaa
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_OFF);//envio de mensaje apra apagar la alarma de ventana
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release1.Controllers.AlarmController.java

/**
 * mtodo para recibir mensajes de los sensores de alarmas de puerta
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException/*  w ww.j  a  va 2s  .  c  o  m*/
 * @throws TimeoutException 
 */
private void receiveAlarmDoorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ID_CHANNEL_ADOOR_SENSOR, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ID_CHANNEL_ADOOR_SENSOR, "");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class Alarm DOOR Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {
                try {
                    sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_ON);//envio de mensaje para encender la alarma
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_OFF);//envio de mensajes para apagar la alarma
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release1.Controllers.AlarmController.java

/**
 * * mtodo para recibir mensajes de los sensores de alarmas de movimiento
 * dependiendo del mensaje recibido se envia la respuesta a los sensores
 * @throws IOException//from  w w w.j  a v  a 2s . c om
 * @throws TimeoutException 
 */
private void receiveMoveMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ID_CHANNEL_AMOVE_SENSOR, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ID_CHANNEL_AMOVE_SENSOR, "");

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM MOVE Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {
                try {
                    sendMessage(ID_CHANNEL_AMOVE_CONTROLLER, ID_AMOVE_ON);//envio de mensajes para encender la alarma de movimiento
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AMOVE_CONTROLLER, ID_AMOVE_OFF);//envio de mensajes para apagar la alarma de movimientos
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:Release2.Controllers.AlarmFireController.java

private synchronized void receivedAlarmFireSensorMessage() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);/*w ww .  j av  a  2s  .  c om*/

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ID_CHANNEL_AFIRE_SENSOR, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ID_CHANNEL_AFIRE_SENSOR, "");

    Consumer consumer;
    consumer = new DefaultConsumer(channel) {
        @Override
        public synchronized void handleDelivery(String consumerTag, Envelope envelope,
                AMQP.BasicProperties properties, byte[] body) throws IOException {
            setMessage(new String(body, "UTF-8"));
            logger.info("Class ALARM FIRE Controller --- RECEIVED from Sensor --- Value: "
                    + new String(body, "UTF-8"));
            if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {//si el valor obtenido es mayor al permitido para simular las alarmas se activa la alarma
                try {
                    sendMessage(ID_CHANNEL_AFIRE_CONTROLLER, ID_AFIRE_ON);//envio de mensaje para encender alarma la alarma de ventnaa
                    logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW");
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            } else {
                try {
                    sendMessage(ID_CHANNEL_AFIRE_CONTROLLER, ID_AFIRE_OFF);//envio de mensaje apra apagar la alarma de ventana
                } catch (TimeoutException ex) {
                    logger.error(ex);
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:se.kodiak.logging.appenders.amqp.AmqpFactory.java

License:Open Source License

private void setupChannel(Channel channel, String exchange) throws IOException {
    channel.exchangeDeclare(exchange, "topic");
}

From source file:se.kodiak.logging.appenders.util.MQListener.java

License:Open Source License

public MQListener() {
    try {/*from  www  .j  av a  2  s .c  o  m*/
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.basicQos(1);
        channel.exchangeDeclare("test", "topic");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, "test", "log.*");
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:sonata.kernel.vimadaptor.messaging.RabbitMqProducer.java

License:Open Source License

@Override
public boolean sendMessage(ServicePlatformMessage message) {
    boolean out = true;

    // TODO maps the specific Adaptor message to the proper SP topic

    try {/* ww  w .j ava2s .c o  m*/
        Channel channel = connection.createChannel();
        String exchangeName = brokerConfig.getProperty("exchange");
        channel.exchangeDeclare(exchangeName, "topic");
        BasicProperties properties = new BasicProperties().builder().appId(AdaptorCore.APP_ID)
                .contentType(message.getContentType()).replyTo(message.getReplyTo())
                .correlationId(message.getSid()).build();
        channel.basicPublish(exchangeName, message.getTopic(), properties, message.getBody().getBytes("UTF-8"));
        // Logger.info("Sending message: " + message + "\n\r - Properties:" + properties);
    } catch (Exception e) {
        Logger.error(e.getMessage(), e);
        out = false;
    }
    return out;
}