请帮助我找出最佳解决方案。我需要处理一些事件并进行验证和处理。我已经实现了,并且可以使用下面提到的方法1正常工作。但我有评论评论使用Executors.newSingleThreadExecutor()。但是我不明白使用它的好处是什么?任何人都可以帮助我了解下面提到的方法2。
Approach 1:
===========
@Path("/webhook")
class A {
@Path("/order")
public void consumeMessage(String body) {
POEvent event = objectMapper.readValue(body, new TypeReference<POEvent>() {
handleEvent(event);
}
public void handleEvent(POEvent po) {
//validation on this event
processEvent(po);
}
processEvent(POEvent poEvent) {
//call external system
}
}
这是我的建议。
Approach 2:
===========
@Path("/webhook")
class A {
private ExecutorService executor = Executors.newSingleThreadExecutor();
@Path("/order")
public void consumeMessage(String body) {
POEvent event = objectMapper.readValue(body, new TypeReference<POEvent>() {
handleEvent(event);
}
public void handleEvent(POEvent po) {
//validation on this event
executor.submit(() -> {
processEvent(po);
});
}
processEvent(POEvent poEvent) {
//call external system
}
}