Package com.cloudbreakers.producers

Source Code of com.cloudbreakers.producers.SoProducer

package com.cloudbreakers.producers;

import kafka.javaapi.producer.Producer;
import kafka.javaapi.producer.ProducerData;
import kafka.producer.ProducerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.Properties;

/**
* Created by kalasd on 8/1/2014.
*/
public class SoProducer
{
    final static Logger LOGGER = LoggerFactory.getLogger(SoProducer.class);
    final static String kafkaDefaultConfig = "kafka.properties";
    final static Properties prop;

    static {
        prop = new Properties();
        try (InputStream is = SoProducer.class.getClassLoader().getResourceAsStream(kafkaDefaultConfig))
        {
            prop.load(is);
        } catch (IOException e) {
            LOGGER.error("Fail to load properties", e);
        }
    }
    final Producer<String, String> kafkaProducer;

    public SoProducer()  {

        kafkaProducer = new Producer<String, String>(new ProducerConfig(prop));
    }

    public void send(String topic, String message)
    {
        LinkedList<String> data = new LinkedList<String>();
        data.add(message);
        String timestamp = Long.toString((new Date()).getTime());
        System.out.println(message);
        kafkaProducer.send(new ProducerData<String, String>(topic, timestamp, data));

    }

    public void close()
    {
        kafkaProducer.close();
    }

}
TOP

Related Classes of com.cloudbreakers.producers.SoProducer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.