Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package SubsrciberStuff; import com.rabbitmq.client.*; import java.io.IOException; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author thor */ public abstract class BrokerSubscriber { public static final String SENSOR_EXCHANGE = "sensorExchange"; public static final String CONTROLER_EXCHANGE = "controllerExchange"; public static final String BIND_KEY_ALL_SENSOR = "sensor.#"; public static String NODE_DESCRIPTER; boolean autoAck = true; Channel topicChannel; static TopicTree topicTree; static ConnectionFactory factory; DefaultConsumer topicConsumer = new DefaultConsumer(topicChannel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { StringTokenizer st = new StringTokenizer(envelope.getRoutingKey(), "."); TopicTree topicHolder = topicTree.child(st.nextToken()); while (st.hasMoreTokens()) { topicHolder = topicHolder.child(st.nextToken()); } } }; public void turnOnTopicMonitoring() { try { Connection connection = factory.newConnection(); topicChannel = connection.createChannel(); topicChannel.exchangeDeclare(SENSOR_EXCHANGE, "topic"); String topChName = topicChannel.queueDeclare().getQueue(); topicChannel.queueBind(topChName, SENSOR_EXCHANGE, BIND_KEY_ALL_SENSOR); topicChannel.basicConsume(topChName, true, topicConsumer); System.out.println("Topic Monitoring ---ON---"); } catch (IOException ex) { Logger.getLogger(BrokerSubscriber.class.getName()).log(Level.SEVERE, null, ex); } } public void turnOffTopicMonitoring() { try { topicChannel.close(); System.out.println("Topic Monitoring ---OFF---"); } catch (IOException ex) { Logger.getLogger(BrokerSubscriber.class.getName()).log(Level.SEVERE, null, ex); } } public BrokerSubscriber(String nodeDescription) { NODE_DESCRIPTER = nodeDescription; topicTree = new TopicTree(NODE_DESCRIPTER); factory = new ConnectionFactory(); factory.setHost("localhost"); turnOnTopicMonitoring(); System.out.println("Subscriber up"); } public BrokerSubscriber() { } }