All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jcustenborder.kafka.connect.mqtt.MqttSourceTask Maven / Gradle / Ivy

There is a newer version: 0.1.5
Show newest version
/**
 * Copyright © 2017 Jeremy Custenborder ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jcustenborder.kafka.connect.mqtt;

import com.github.jcustenborder.kafka.connect.utils.VersionUtil;
import com.github.jcustenborder.kafka.connect.utils.data.SourceRecordDeque;
import com.github.jcustenborder.kafka.connect.utils.data.SourceRecordDequeBuilder;
import com.google.common.base.Joiner;
import org.apache.kafka.common.utils.Time;
import org.apache.kafka.connect.errors.ConnectException;
import org.apache.kafka.connect.source.SourceRecord;
import org.apache.kafka.connect.source.SourceTask;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class MqttSourceTask extends SourceTask implements IMqttMessageListener {
  private static final Logger log = LoggerFactory.getLogger(MqttSourceTask.class);

  @Override
  public String version() {
    return VersionUtil.version(this.getClass());
  }

  Time time = Time.SYSTEM;
  MqttSourceConnectorConfig config;
  IMqttClient client;
  SourceRecordDeque records;
  MessageConverter messageConverter;


  @Override
  public void start(Map settings) {
    this.config = new MqttSourceConnectorConfig(settings);
    this.messageConverter = new MessageConverter(this.config, this.time);
    this.records = SourceRecordDequeBuilder.of()
        .batchSize(4096)
        .emptyWaitMs(100)
        .maximumCapacityTimeoutMs(60000)
        .maximumCapacity(50000)
        .build();
    try {
      this.client = new MqttClient(this.config.serverUri.get(0), this.config.clientId);
    } catch (MqttException e) {
      throw new ConnectException(e);
    }

    try {
      log.info("Connecting to Mqtt Server.");
      this.client.connect(this.config.connectOptions());
    } catch (MqttException e) {
      throw new ConnectException(e);
    }

    final String[] topicSubscriptions = this.config.mqttTopics.toArray(new String[this.config.mqttTopics.size()]);
    final int[] qosLevels = new int[]{this.config.mqttQos};
    try {
      log.info(
          "Subscribing to {} with QOS of {}",
          Joiner.on(',').join(topicSubscriptions),
          this.config.mqttQos
      );
      this.client.subscribe(topicSubscriptions, qosLevels, new IMqttMessageListener[]{this});
    } catch (MqttException e) {
      throw new ConnectException(e);
    }
  }

  @Override
  public List poll() throws InterruptedException {
    List result = this.records.newList();
    while (this.records.drain(result)) {

    }
    return result;
  }

  @Override
  public void stop() {
    try {
      this.client.disconnect();
    } catch (MqttException e) {
      log.error("Exception thrown while disconnecting client.", e);
    }
  }

  @Override
  public void messageArrived(String topic, MqttMessage message) throws Exception {
    log.trace("messageArrived() - topic = '{}' message = '{}'", topic, message);
    final SourceRecord record = this.messageConverter.convert(topic, message);
    this.records.add(record);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy