这个Spring块线程安全吗?

时间:2016-07-20 10:14:53

标签: java spring

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块是否是线程安全的?我该如何改进呢?

由于

0 个答案:

没有答案