Configuration for Image Resize Request Consumer

The Image Resize Request Consumer is responsible for listening for messages coming from Apache Kafka topic and doing the actual resizing of the images. Consider the following code:

@SpringBootApplication
public class SpringBootAsyncConsumerApplication {

...

@KafkaListener(id="server", topics = "asyncRequests")
@SendTo
public String listen(String input) {
try {
ImageResizeRequest imageResizeRequest = objectMapper().readValue(input, ImageResizeRequest.class);
File imageFile = new File(imageResizeRequest.getInputFile());
String[] nameParts = imageFile.getName().split("\.");
BufferedImage image = ImageIO.read(imageFile);
ImageIO.write(resize(image, imageResizeRequest.getWidth(), imageResizeRequest.getHeight()), "png", new File(imageFile.getParent() + "/Done", imageResizeRequest.getWidth() + "x" + imageResizeRequest.getHeight() + "-" + nameParts[0] + ".png"));
} catch (IOException e) {
LOGGER.error("Error while processing input {}", input, e);
}
return input;
}

@Bean
public NewTopic asyncRequests() {
return new NewTopic("asyncRequests", 10, (short) 2);
}

}

In the preceding configuration, two Apache Kafka topics by the name asyncRequests are created as a Spring Bean with a number of replications 10 and replication factor of 2. The listen method is responsible for consuming requests sent by the producer via the Apache Kafka topic ayncRequests. This method is annotated with @KafkaListener, which points to the asyncRequests topic and also has the @SendTo annotation to send a reply back to the asyncReplies topic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.111.1