Boa noite, o valor da propriedade Amout é enviado corretamente no NewOrderService, mas quando chega no FraudDetector service, está null, ocorrendo null pointer. Falta configurar alguma cosia nesse meio-termo?
public class NewOrderMain {
public static void main(String[] args) throws ExecutionException, InterruptedException {
var orderDispatcher = new KafkaDispatcher<Order>();
var emailDispatcher = new KafkaDispatcher<String>();
var userId = UUID.randomUUID().toString(); //UUID faz com que a mensagem seja diferente, então o costumer será uma partição distinta
var orderId = UUID.randomUUID().toString();
var amount = new BigDecimal(Math.random() * 5000 + 1);
var order = new Order(userId, orderId, amount);
orderDispatcher.send("ECOMMERCE_NEW_ORDER", userId, order);
var emailKey = "welcome to email service!";
emailDispatcher.send("ECOMMERCE_SEND_EMAIL", userId, emailKey);
}
}
public class FraudDetectorService {
public static void main(String[] args) {
var fraudDetectorService = new FraudDetectorService();
try(var service = new KafkaService<>(FraudDetectorService.class.getSimpleName(),
"ECOMMERCE_NEW_ORDER",
fraudDetectorService::parse,
Order.class,
new HashMap<String, String>())) {
service.run();
}
}
private void parse(ConsumerRecord<String, Order> record) throws ExecutionException, InterruptedException {
System.out.println("--------------------");
System.out.println("Processing new order, checking for fraud");
System.out.println(record.key());
System.out.println(record.value());
System.out.println(record.partition());
System.out.println(record.offset());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
var order = record.value();
if (isFraud(order)){
//fingindo que uma fraude é qualquer compra acima de 4500
System.out.println("Order is a Fraud!"+ order);
orderDispatcher.send("ECOMMERCE_ORDER_REJECTED", order.getUserId(), order);
} else {
System.out.println("Approved: " + order);
orderDispatcher.send("ECOMMERCE_ORDER_APPROVED", order.getUserId(), order);
}
}
private final KafkaDispatcher<Order> orderDispatcher = new KafkaDispatcher<>();
private boolean isFraud(Order order) {
return order.getAmout().compareTo(new BigDecimal(4500)) >= 0;
}