private static final Logger log = LoggerFactory.getLogger(Application.class);
....
ListenableFuture<ResponseEntity<Data>> dataFuture = asyncRestTemplate.getForEntity(url, Data.class);
dataFuture.addCallback(new ListenableFutureCallback<ResponseEntity<Data>>() {
@Override
public void onFailure(Throwable throwable) {
log.error("Error: " + throwable.getMessage());
}
@Override
public void onSuccess(ResponseEntity<Data> dataResponseEntity) {
Data data = dataResponseEntity.getBody();
List<Event> event = data.getData();
for (Event ent: event) {
System.out.println(String.format("[%s] %s", new Timestamp(date.getTime()), ent.toString()));
insertEventToDB(eventCollection, ent);
}
}
});
我正在调用API,并且在成功回调时,我将数据插入数据库。我关心的问题是这个onSuccess
块是否是线程安全的?我该如何改进呢?
由于