public KafkaClient(String hosts, String group, String topic) {
        HashMap<String, Object> configs = new HashMap<>();
        configs.put("bootstrap.servers", hosts);
        configs.put("group.id", group);
        configs.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        configs.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        this.producer = new KafkaProducer<>(configs);
        log.debug("create kafka client. ");
        this.gson = new Gson();
    }
    
      public void sendMessage(String data) {
        String jsonMsg = gson.toJson(new Message(data, new Date().getTime()));
        log.debug("sendMessage [{}]", jsonMsg);
        producer.send(new ProducerRecord<>(topic, jsonMsg));
    }

+ Recent posts