This commit is contained in:
wentch
2015-12-18 17:03:20 +08:00
parent b4ffc61014
commit ef1bce449d
3 changed files with 18 additions and 6 deletions

View File

@@ -207,7 +207,7 @@ public abstract class NodeServer {
}
CacheSourceService source = Sncp.createLocalService(resourceName, getExecutor(), CacheSourceService.class, this.sncpAddress, sncpDefaultGroups, sameGroupTransports, diffGroupTransports);
CacheStore store = field.getAnnotation(CacheStore.class);
if (store != null) source.setStoreType(store.keyType(), store.valueType());
if (store != null) source.setStoreType(store.keyType(), store.valueType(), store.entryType());
application.cacheSources.add(source);
regFactory.register(resourceName, CacheSource.class, source);
field.set(src, source);

View File

@@ -33,7 +33,7 @@ public class CacheSourceService implements CacheSource, Service, AutoCloseable {
private Class storeKeyType;
private Class storeValueType;
private Type storeValueType;
private ScheduledThreadPoolExecutor scheduler;
@@ -46,9 +46,15 @@ public class CacheSourceService implements CacheSource, Service, AutoCloseable {
public CacheSourceService() {
}
public CacheSourceService setStoreType(Class storeKeyType, Class storeValueType) {
public CacheSourceService setStoreType(Class storeKeyType, Class storeValueType, CacheStore.CacheEntryType entryType) {
this.storeKeyType = storeKeyType;
this.storeValueType = storeValueType;
if (entryType == CacheStore.CacheEntryType.SET) {
this.storeValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, storeValueType);
} else if (entryType == CacheStore.CacheEntryType.LIST) {
this.storeValueType = TypeToken.createParameterizedType(null, CopyOnWriteArrayList.class, storeValueType);
} else {
this.storeValueType = storeValueType;
}
return this;
}
@@ -59,10 +65,10 @@ public class CacheSourceService implements CacheSource, Service, AutoCloseable {
if (storeKeyType == null && prop != null) {
String storeKeyStr = prop.getValue("store-key-type");
String storeValueStr = prop.getValue("store-value-type");
String storeEntryStr = prop.getValue("store-entry-type", CacheStore.CacheEntryType.OBJECT.name()).toUpperCase();
if (storeKeyStr != null && storeValueStr != null) {
try {
this.storeKeyType = Class.forName(storeKeyStr);
this.storeValueType = Class.forName(storeValueStr);
this.setStoreType(Class.forName(storeKeyStr), Class.forName(storeValueStr), CacheStore.CacheEntryType.valueOf(storeEntryStr));
} catch (Exception e) {
logger.log(Level.SEVERE, self.getClass().getSimpleName() + " load key & value store class (" + storeKeyStr + ", " + storeValueStr + ") error", e);
}

View File

@@ -20,7 +20,13 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RUNTIME)
public @interface CacheStore {
public static enum CacheEntryType {
OBJECT, SET, LIST;
}
Class keyType(); //key对应的class
Class valueType(); //value 对应的class
CacheEntryType entryType() default CacheEntryType.OBJECT;
}