下面列出了怎么用org.apache.commons.lang3.concurrent.LazyInitializer的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void testLazyObjectHeader() throws ConcurrentException, IOException {
FileChannel spyFc = Mockito.spy(fc);
HdfFileChannel hdfFileChannel = new HdfFileChannel(spyFc, sb);
LazyInitializer<ObjectHeader> lazyObjectHeader = ObjectHeader.lazyReadObjectHeader(hdfFileChannel, 10904); // int8
// header
// Creating the lazy object header should not touch the file
Mockito.verifyNoInteractions(spyFc);
// Get the actual header should cause the file to be read
lazyObjectHeader.get();
// Check the file was read
verify(spyFc, Mockito.atLeastOnce()).read(any(ByteBuffer.class), anyLong());
// Ensure nothing else was done to the file
Mockito.verifyNoMoreInteractions(spyFc);
}
boolean canRepair(
RepairSegment segment,
String keyspace,
JmxProxy coordinator,
Cluster cluster,
LazyInitializer<Set<String>> busyHosts) {
if (RepairSegment.State.NOT_STARTED == segment.getState()) {
try {
Map<String, String> dcByNode = getDCsByNodeForRepairSegment(coordinator, cluster, segment, keyspace);
return !isRepairRunningOnNodes(segment, dcByNode, keyspace, cluster)
&& nodesReadyForNewRepair(coordinator, segment, dcByNode, busyHosts);
} catch (RuntimeException e) {
LOG.warn("SegmentRunner couldn't get token ranges from coordinator: ", e);
String msg = "SegmentRunner couldn't get token ranges from coordinator";
repairRunner.updateLastEvent(msg);
}
}
return false;
}
private void handlePotentialStuckRepairs(LazyInitializer<Set<String>> busyHosts, String hostName)
throws ConcurrentException {
if (!busyHosts.get().contains(hostName) && context.storage instanceof IDistributedStorage) {
try {
JmxProxy hostProxy = clusterFacade.connect(context.storage.getCluster(clusterName), Arrays.asList(hostName));
// We double check that repair is still running there before actually cancelling repairs
if (hostProxy.isRepairRunning()) {
LOG.warn(
"A host ({}) reported that it is involved in a repair, but there is no record "
+ "of any ongoing repair involving the host. Sending command to abort all repairs "
+ "on the host.",
hostName);
hostProxy.cancelAllRepairs();
}
} catch (ReaperException | RuntimeException | JMException e) {
LOG.debug("failed to cancel repairs on host {}", hostName, e);
}
}
}
public static LazyInitializer<ObjectHeader> lazyReadObjectHeader(HdfFileChannel hdfFc, long address) {
logger.debug("Creating lazy object header at address: {}", address);
return new LazyInitializer<ObjectHeader>() {
@Override
protected ObjectHeader initialize() {
logger.debug("Lazy initializing object header at address: {}", address);
return readObjectHeader(hdfFc, address);
}
};
}
public AttributesLazyInitializer(LazyInitializer<ObjectHeader> lazyObjectHeader) {
this.lazyObjectHeader = lazyObjectHeader;
}