CachingFlowManager这部分为Azkaban在管理job中cache管理这一部分。
[java] 、
public CachingFlowManager(FlowManager baseManager, final int cacheSize)
{
this.baseManager = baseManager;
this.flowCache = Collections.synchronizedMap(
new LinkedHashMap((int) (cacheSize * 1.5), 0.75f, true){
@Override
protected boolean removeEldestEntry(Map.Entry eldest)
{
final boolean tooManyElements = super.size() > cacheSize;
if (tooManyElements) {
final Status status = eldest.getValue().getFlow().getStatus();
if (status != Status.RUNNING) {
return true;
}
else {
log.warn(String.format(
"Cache is at size[%s] and should have evicted an entry, but the oldest entry wasn't completed[%s]. Perhaps the cache size is too small",
super.size(),
status
));
}
}
return false;
}
}
);
}
public CachingFlowManager(FlowManager baseManager, final int cacheSize)
{
this.baseManager = baseManager;
this.flowCache = Collections.synchronizedMap(
new LinkedHashMap((int) (cacheSize * 1.5), 0.75f, true){
@Override
protected boolean removeEldestEntry(Map.Entry eldest)
{
final boolean tooManyElements = super.size() > cacheSize;
if (tooManyElements) {
final Status status = eldest.getValue().getFlow().getStatus();
if (status != Status.RUNNING) {
return true;
}
else {
log.warn(String.format(
"Cache is at size[%s] and should have evicted an entry, but the oldest entry wasn't completed[%s]. Perhaps the cache size is too small",
super.size(),
status
));
}
}
return false;
}
}
);
}
在这个方法中涉及到几个地方是之前没有学习到过、也没有遇到过的。
下面都一一来研究下。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1、Collections.synchronizedMap
先来看下在JDK中对Collections的解释:
接下来synchronizedMap
顾名思义,synchronize是同步,那则为同步的map。
public static Map synchronizedMap(Map m) 这是一个返回由指定映射支持的同步(线程安全的)映射的方法。也就是返回一个线程安全的map
2、new LinkedHashMap((int) (cacheSize * 1.5), 0.75f, true)
LinkedHashMap是JDK1.4引入的新的集合类,相信很多人都没有用到过LinkedHashMap。
LinkedHashMap相当于是一个对HashMap的扩展,它在Hash的实现有加上了对Linked的支持。
在我们平常使用的时候几乎所有情况下都会使用这些集合类无参的构造方