001package var.mom.mqtt.log; 002 003import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; 004import org.eclipse.paho.client.mqttv3.MqttCallback; 005import org.eclipse.paho.client.mqttv3.MqttClient; 006import org.eclipse.paho.client.mqttv3.MqttException; 007import org.eclipse.paho.client.mqttv3.MqttMessage; 008 009/** 010 * asynchronous subscriber for the log service 011 * 012 * @author Sandro Leuchter 013 * 014 */ 015class Subscriber { 016 017 /** 018 * main routine and starting point of program 019 * 020 * @param args not used 021 */ 022 public static void main(String[] args) { 023 MqttClient client = null; 024 String clientId = MqttClient.generateClientId(); 025 try { 026 client = new MqttClient(Conf.BROKER, clientId); 027 client.setCallback(new MqttCallback() { 028 029 @Override 030 public void connectionLost(Throwable arg0) { 031 } 032 033 @Override 034 public void deliveryComplete(IMqttDeliveryToken arg0) { 035 } 036 037 @Override 038 public void messageArrived(String topic, MqttMessage m) throws Exception { 039 System.out.println("Topic: " + topic + ", Message: " + m.toString()); 040 } 041 }); 042 client.connect(); 043 client.subscribe(Conf.TOPIC); 044 while (true) { 045 Thread.sleep(1000); 046 } 047 } catch (MqttException | InterruptedException e) { 048 System.err.println(e.getMessage()); 049 } finally { 050 try { 051 client.disconnect(); 052 } catch (MqttException e) { 053 // unrecoverable 054 } 055 } 056 } 057}