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

fr.figarocms.flume.rabbitmq.RabbitMQSink Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
package fr.figarocms.flume.rabbitmq;

import com.cloudera.flume.conf.SinkFactory;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.handlers.text.output.OutputFormat;
import com.cloudera.util.Pair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RabbitMQSink extends EventSink.Base {

  protected static Logger LOG = LoggerFactory.getLogger(RabbitMQSink.class);

  private Producer producer;
  private OutputFormat format;
  private String mediaType;

  public RabbitMQSink(Producer producer, OutputFormat format, String mediaType) {
    this.producer = producer;
    this.format = format;
    this.mediaType = mediaType;
  }

  @Override
  public void append(Event e) throws IOException, InterruptedException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    format.format(bos, e);
    producer.publish(bos.toByteArray(), mediaType);
  }

  @Override
  public void close() throws IOException, InterruptedException {
    producer.close();
  }

  @Override
  public void open() throws IOException, InterruptedException {
    producer.open();
  }

  private static SinkFactory.SinkBuilder builder() {
    return new RabbitMQSinkBuilder();
  }

  private static SinkFactory.SinkBuilder failover() {
    return new RabbitMQFailoverSinkBuilder();
  }

  /**
   * This is a special function used by the SourceFactory to pull in this class as a RabbitMQSink decorator.
   *
   * @return a list of RabbitMQSink
   */
  public static List> getSinkBuilders() {
    List> builders = new ArrayList>();
    builders.add(new Pair("rabbit", builder()));
    builders.add(new Pair("rabbitFailover", failover()));
    return builders;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy