我从一个表中获取数据然后迭代该ResultSet以从另一个表中获取数据(executeAsync())。我想控制异步任务的数量。
for(Row row : resultSites) {
String siteName = row.getString("site_name");
System.out.println(siteName);
ResultSetFuture future = casCon.session.executeAsync(casCon.boundStatement.bind(siteName));
Futures.addCallback(future,fCR,MoreExecutors.listeningDecorator(executorService));
}
我的执行者和FutureCallback是
ExecutorService executorService = Executors.newFixedThreadPool(2);
FutureCallback<ResultSet> fCR = new FutureCallback<ResultSet>() {
@Override public void onSuccess(ResultSet result) {
System.out.println(result.one());
}
@Override public void onFailure(Throwable t) {
System.out.println("Error while reading Cassandra version: " + t.getMessage());
}
};
现在通过搜索我发现了这个并在我的代码中实现了它
PoolingOptions poolingOpts = new PoolingOptions();
poolingOpts.setCoreConnectionsPerHost(HostDistance.REMOTE, 2);
poolingOpts.setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
poolingOpts.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 10);
poolingOpts.setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.REMOTE, 2);
poolingOpts.setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 2);
poolingOpts.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 10);
cluster = Cluster.builder().addContactPoint(ip)
.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy())
.withPoolingOptions(poolingOpts)
.withCredentials(user, pass)
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.build();
但是当我打电话时
System.out.println(session.getCluster().getConfiguration().getPoolingOptions().getMaxConnectionsPerHost(HostDistance.REMOTE));
我得到空指针异常所以我不确定我是否已正确实现它?有任何帮助或建议吗?