0

I am using the following code to read JSON from the project folder and then execute kafka producer code.

Right now, it is in while loop so code execution is happening continuously.

If I want to replace a new "test.json" in the same path. Expecting this time, automatically the code should read the new "test.json" that is being updated in the same project folder. How to get this code automatically read if there is any new "test.json" being updated in the same path?

The existing below code is not reading if I copy/paste a new "test.json" in the path.

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

    while (true) {           
        try {           
            URL url = getClass().getResource("test.json");
            File file = new File(url.getPath());
            Properties props = new Properties();            
            props.put("bootstrap.servers", "yupsectory.selet.com:9092");
            props.put("client.id", CLIENT_ID);
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            String jsonData = readFile(file.getAbsolutePath());
            JSONObject jobj = new JSONObject(jsonData);    
            System.out.println("jarr: " + jobj.getJSONObject("data").toString());    
            Yupsectoryproducer<String, String> producer = new Yupsectoryproducer <>(props);      
            //Use this util to pull the context that needs to be propagated from the HttpServletRequest
            Map<String, String> headermap = YupsectoryContextUtil.buildContextMap(request);
            //Sending a message
            ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, jobj.getJSONObject("data").toString());
            producer.send(record, headermap);           
            producer.close();                 
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
Mickael Maison
  • 25,067
  • 7
  • 71
  • 68
Stella
  • 1,728
  • 5
  • 41
  • 95
  • A watch service could be help. https://docs.oracle.com/javase/tutorial/essential/io/notification.html – Top.Deck Apr 16 '18 at 16:54
  • 1
    If you would like an easier way to do this, use Kafka Connect : https://github.com/jcustenborder/kafka-connect-spooldir – Robin Moffatt Apr 16 '18 at 17:13
  • @Top.Deck, I tried sample code from this link, http://www.codejava.net/java-se/file-io/file-change-notification-example-with-watch-service-api – Stella Apr 17 '18 at 19:54
  • But, it is not notifying when the same file being overwritten in the path. I would like to get file updation notification when the same file being overwritten in the same path. For ex: I have a file called A.JSON in a path. then I have an updated same file (A.json) and overwrite in the same path. I want the notification to be detected now. – Stella Apr 17 '18 at 19:56
  • @Stella Here could be a good example: https://stackoverflow.com/questions/16251273/can-i-watch-for-single-file-change-with-watchservice-not-the-whole-directory – Top.Deck Apr 18 '18 at 15:04

0 Answers0