MOTT介绍(4)Flume整合mqtt

package com.huhy.common;


import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDrivenSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.EventBuilder;
import org.apache.flume.source.AbstractSource;
import org.eclipse.paho.client.mqttv3.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author:huhy
 * @DATE:Created on 2018/1/25 14:33
 * @Modified By:
 * @Class Description: flume收集MQTT(Mosquitto)的数据。
 *                     方法就是flume自定义source,source中来订阅(subscribe)MQTT
 */
public class MQTTSource extends AbstractSource implements EventDrivenSource,
        Configurable {
  
    /**
    *源代码的初始化方法。上下文(context)包含了所有的内容。
    * Flume配置信息,可用于检索任何配置。
    *设置源所需的值。
    */
    @Override
    public void configure(Context arg0) {
        // TODO Auto-generated method stub

    }

    SimpleMqttClient client = null;

    /**
     * Start any dependent systems and begin processing events.
     * 启动任何依赖系统并开始处理事件。
     */
    @Override
    public void start() {
        // TODO Auto-generated method stub ||自动生成的方法
        // super.start();
        client = new SimpleMqttClient();
        client.runClient();
    }

    /**
     * Stop processing events and shut any dependent systems down.
     */
    @Override
    public void stop() {
        // TODO Auto-generated method stub || 自动生成的方法
        // super.stop();
        if (client != null) {
            client.closeConn();
        }
    }

    

    public class SimpleMqttClient implements MqttCallback {

        MqttClient myClient;
        MqttConnectOptions connOpt;
        /**
         * tcp连接
         */
        String BROKER_URL = "tcp://192.168.1.19:61613";
        /**
         * M2MIO_DOMAIN:ip
         * M2MIO_STUFF:域
         */
        /*String M2MIO_DOMAIN = "192.168.1.19";
        String M2MIO_STUFF = "mytest";*/
        // topic
         String M2MIO_THING = "huhy";
         String M2MIO_USERNAME = "admin";
         String M2MIO_PASSWORD_MD5 ="password";
      
        //发布   订阅
        Boolean subscriber = true;
        Boolean publisher = false;

        /**
         *
         * connectionLost This callback is invoked upon losing the MQTT
         * connection.
         * MQTT时调用connectionLost这个回调。
         *
         */
        @Override
        public void connectionLost(Throwable t) {
            System.out.println("Connection lost!");
            // code to reconnect to the broker would go here if desired
        }

        public void closeConn() {
            if (myClient != null) {
                if (myClient.isConnected()) {
                    try {
                        myClient.disconnect();
                    } catch (MqttException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

        /**
         *
         * deliveryComplete This callback is invoked when a message published by
         * this client is successfully received by the broker.
         *
         */
        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
            // System.out.println("Pub complete" + new
            // String(token.getMessage().getPayload()));
        }

        /**
         * 在接收到消息时调用messageArrived这个回调。
         * 一个订阅的主题。
         */
        @Override
        public void messageArrived(String topic, MqttMessage message)
                throws Exception {
            
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("id", "123");//数据包带有唯一标示
            headers.put("time", String.valueOf(System.currentTimeMillis()));//设备的时间戳
            Event flumeEvent = EventBuilder.withBody(message.getPayload(),
                                                     headers);
            try {
                getChannelProcessor().processEvent(flumeEvent);
                System.out.println("消息到达-------->");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }

        /**
         *
         * runClient The main functionality of this simple example. Create a
         * MQTT client, connect to broker, pub/sub, disconnect.
         *
         */
        public void runClient() {
            // setup MQTT Client
            String clientID = M2MIO_THING;
            connOpt = new MqttConnectOptions();

            connOpt.setCleanSession(true);
            connOpt.setKeepAliveInterval(3000);
            connOpt.setUserName(M2MIO_USERNAME);
            connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());

            // Connect to Broker
            try {
                myClient = new MqttClient(BROKER_URL, clientID);
                myClient.setCallback(this);
                myClient.connect(connOpt);
            } catch (MqttException e) {
                e.printStackTrace();
                System.exit(-1);
            }

            System.out.println("Connected to " + BROKER_URL);

            // setup topic
            // topics on m2m.io are in the form <domain>/<stuff>/<thing>
            /**
             * 方式一
             */
            /*String myTopic = M2MIO_DOMAIN + "/" + M2MIO_STUFF + "/"
                    + M2MIO_THING;
            System.out.println("myTopic:" + myTopic);
            MqttTopic topic = myClient.getTopic(myTopic);*/
            /**
             * 方式二
             */
            MqttTopic topic = myClient.getTopic(M2MIO_THING);
                
            // subscribe to topic if subscriber
            if (subscriber) {
                try {
                    int subQoS = 2;
                   // myClient.subscribe(myTopic, subQoS);
                    myClient.subscribe(M2MIO_THING, subQoS);

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

            // publish messages if publisher
            if (publisher) {
                for (int i = 1; i <= 10; i++) {
                    String pubMsg = "{\"pubmsg\":" + i + "}";
                    int pubQoS = 2;
                    MqttMessage message = new MqttMessage(pubMsg.getBytes());
                    message.setQos(pubQoS);
                    message.setRetained(false);

                    // Publish the message
                    System.out.println("Publishing to topic \"" + topic
                                               + "\" qos " + pubQoS);
                    MqttDeliveryToken token = null;
                    try {
                        // publish message to broker
                        token = topic.publish(message);
                        // Wait until the message has been delivered to the
                        // broker
                        token.waitForCompletion();
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            // disconnect
            try {
                // wait to ensure subscribed messages are delivered
                if (subscriber) {
                    while (true) {
                        Thread.sleep(5000);
                    }
                }
                // myClient.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }

    }

}

内部类是连接apollo服务器的java代码

posted @ 2018-01-30 17:28  陽66  阅读(1548)  评论(0)    收藏  举报