我們目前使用Cassandra作爲NoSQL數據庫和GemFire作爲內存數據庫。我們一直使用GemFire CacheWriter在Cassandra中插入記錄。我希望得到您的反饋意見,說明在CacheWriter中使用Concurrent線程來插入/更新記錄是否是一個好的工程實踐。您對此的反饋將不勝感激。GemFire CacheWriter中的並行線程
public class GenericWriter<K, V> extends CacheWriterAdapter<K, V> implements Declarable {
private static Logger log = LoggerFactory.getLogger(GenericWriter.class);
@Autowired
private CassandraOperations cassandraOperations;
ExecutorService executor = null;
@Override
public void beforeCreate(EntryEvent<K, V> e) {
executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
if (eventOperation.equals("CREATE") || eventOperation.equalsIgnoreCase("PUTALL_CREATE")) {
try {
cassandraOperations.insert(e.getNewValue());
} catch (CassandraConnectionFailureException | CassandraWriteTimeoutException
| CassandraInternalException cassException) {
} catch (Exception ex) {
log.error("Exception in GenericCacheWriter->" + ExceptionUtils.getStackTrace(ex));
throw ex;
}
}
});
executor.shutdown();
}
@Override
public void init(Properties arg0) {
// TODO Auto-generated method stub
}
}
兩個答案都是正確的。任何人都可以告訴我接受這兩種方式 – Vigneshwaran