This commit is contained in:
wentch
2015-12-23 15:12:06 +08:00
parent 59f1d51d99
commit 6e78946d37
4 changed files with 36 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.*; import java.util.logging.*;
import javax.annotation.*; import javax.annotation.*;
import javax.persistence.*;
import org.redkale.net.*; import org.redkale.net.*;
import org.redkale.net.http.*; import org.redkale.net.http.*;
import org.redkale.service.*; import org.redkale.service.*;
@@ -209,6 +210,7 @@ public abstract class NodeServer {
Type genericType = field.getGenericType(); Type genericType = field.getGenericType();
ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null; ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null;
Type valType = pt == null ? null : pt.getActualTypeArguments()[1]; Type valType = pt == null ? null : pt.getActualTypeArguments()[1];
source.setNeedStore(field.getAnnotation(Transient.class) == null);
source.setStoreType(pt == null ? Serializable.class : (Class) pt.getActualTypeArguments()[0], valType instanceof Class ? (Class) valType : Object.class); source.setStoreType(pt == null ? Serializable.class : (Class) pt.getActualTypeArguments()[0], valType instanceof Class ? (Class) valType : Object.class);
application.cacheSources.add(source); application.cacheSources.add(source);
regFactory.register(resourceName, CacheSource.class, source); regFactory.register(resourceName, CacheSource.class, source);

View File

@@ -276,6 +276,28 @@ public abstract class Factory<R extends Reader, W extends Writer> {
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pts = (ParameterizedType) type; final ParameterizedType pts = (ParameterizedType) type;
clazz = (Class) (pts).getRawType(); clazz = (Class) (pts).getRawType();
} else if (type instanceof TypeVariable) { // e.g. <? extends E>
final TypeVariable tv = (TypeVariable) type;
Class cz = tv.getBounds().length == 0 ? Object.class : null;
for (Type f : tv.getBounds()) {
if (f instanceof Class) {
cz = (Class) f;
break;
}
}
clazz = cz;
if (cz == null) throw new ConvertException("not support the type (" + type + ")");
} else if (type instanceof WildcardType) { // e.g. <? extends Serializable>
final WildcardType wt = (WildcardType) type;
Class cz = null;
for (Type f : wt.getUpperBounds()) {
if (f instanceof Class) {
cz = (Class) f;
break;
}
}
clazz = cz;
if (cz == null) throw new ConvertException("not support the type (" + type + ")");
} else if (type instanceof Class) { } else if (type instanceof Class) {
clazz = (Class) type; clazz = (Class) type;
} else { } else {

View File

@@ -26,7 +26,7 @@ import org.redkale.util.*;
* @author zhangjx * @author zhangjx
*/ */
@AutoLoad(false) @AutoLoad(false)
public class CacheSourceService<K extends Serializable, V> implements CacheSource<K, V>, Service, AutoCloseable { public class CacheSourceService<K extends Serializable, V extends Object> implements CacheSource<K, V>, Service, AutoCloseable {
@Resource(name = "APP_HOME") @Resource(name = "APP_HOME")
private File home; private File home;
@@ -34,6 +34,8 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
@Resource @Resource
private JsonConvert convert; private JsonConvert convert;
private boolean needStore = true;
private Class keyType; private Class keyType;
private Type objValueType; private Type objValueType;
@@ -53,7 +55,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
public CacheSourceService() { public CacheSourceService() {
} }
public CacheSourceService setStoreType(Class keyType, Class valueType) { public final CacheSourceService setStoreType(Class keyType, Class valueType) {
this.keyType = keyType; this.keyType = keyType;
this.objValueType = valueType; this.objValueType = valueType;
this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType); this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType);
@@ -61,11 +63,16 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
return this; return this;
} }
public final void setNeedStore(boolean needStore) {
this.needStore = needStore;
}
@Override @Override
public void init(AnyValue conf) { public void init(AnyValue conf) {
final CacheSourceService self = this; final CacheSourceService self = this;
AnyValue prop = conf == null ? null : conf.getAnyValue("property"); AnyValue prop = conf == null ? null : conf.getAnyValue("property");
if (keyType == null && prop != null) { if (keyType == null && prop != null) {
this.needStore = prop.getBoolValue("store-value", true);
String storeKeyStr = prop.getValue("key-type"); String storeKeyStr = prop.getValue("key-type");
String storeValueStr = prop.getValue("value-type"); String storeValueStr = prop.getValue("value-type");
if (storeKeyStr != null && storeValueStr != null) { if (storeKeyStr != null && storeValueStr != null) {
@@ -111,7 +118,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
boolean datasync = false; //是否从远程同步过数据 boolean datasync = false; //是否从远程同步过数据
//----------同步数据……----------- //----------同步数据……-----------
// TODO // TODO
if (this.keyType == null) return; if (!this.needStore) return;
try { try {
CacheEntry.initCreator(); CacheEntry.initCreator();
File store = new File(home, "cache/" + name()); File store = new File(home, "cache/" + name());
@@ -149,7 +156,7 @@ public class CacheSourceService<K extends Serializable, V> implements CacheSourc
@Override @Override
public void destroy(AnyValue conf) { public void destroy(AnyValue conf) {
if (scheduler != null) scheduler.shutdownNow(); if (scheduler != null) scheduler.shutdownNow();
if (this.keyType == null || Sncp.isRemote(this) || container.isEmpty()) return; if (!this.needStore || Sncp.isRemote(this) || container.isEmpty()) return;
try { try {
CacheEntry.initCreator(); CacheEntry.initCreator();
File store = new File(home, "cache/" + name()); File store = new File(home, "cache/" + name());

View File

@@ -16,7 +16,7 @@ import java.util.*;
* @see http://www.redkale.org * @see http://www.redkale.org
* @author zhangjx * @author zhangjx
*/ */
public interface CacheSource<K extends Serializable, V> { public interface CacheSource<K extends Serializable, V extends Object> {
default boolean isOpen() { default boolean isOpen() {
return true; return true;