我建议引入至少一个新接口QueueTaskAble
,该接口将由您的输入对象实现,第二个 - 可选的,可能只是java.lang.Runnable
- 然后使用某种run
或execute
方法执行任务。
这是它的样子:
interface QueueTaskAble {
Runnable getQueueTask();
}
class InputObjectFooQueueTask implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
}
}
class InputObjectFooImpl implements QueueTaskAble {
@Override
public Runnable getQueueTask() {
return new InputObjectFooQueueTask();
}
}
void processQueueInputObject(QueueTaskAble queueObject) {
queueObject.getQueueTask().run();
}
编辑
不幸的是,不可能使用Gson将本机反序列化为接口。为此,您需要实现一个类型适配器,您可以将其传递给GsonBuilder#registerTypeAdapter以便您的对象正确序列化和反序列化。
您可以这样做:
类型适配器
public class GenericGsonTypeAdapter<T> implements JsonSerializer<T>,
JsonDeserializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject jo = new JsonObject();
jo.addProperty("className", src.getClass().getName());
jo.add("jsonData", context.serialize(src));
return jo;
}
@Override
public T deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
T obj = null;
if(json instanceof JsonObject) {
JsonObject jo = (JsonObject) json;
JsonElement jeJson = jo.get("jsonData");
if(jeJson != null) {
JsonElement jeClassName = jo.get("className");
try {
obj = context.deserialize(json,
Class.forName(jeClassName.getAsString()));
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
}
return obj;
}
}
自定义Gson构建器(不幸的是GsonBuilder是最终的,因此无法扩展,因此作为静态最终成员)
public class InputObjectGsonBuilder {
private final static GsonBuilder gb;
static {
gb = new GsonBuilder();
gb.registerTypeAdapter(QueueTaskAble.class,
new GenericGsonTypeAdapter<QueueTaskAble>());
}
public Gson create() {
return gb.create();
}
}
样本队列
public class InputObjectGsonQueue {
private Queue<String> queue = new ArrayDeque<>();
public boolean pushInputObject(String json) {
return queue.offer(json);
}
public void processQueue() {
InputObjectGsonBuilder gb = new InputObjectGsonBuilder();
String json;
while(null != (json = queue.poll())) {
QueueTaskAble queueTaskAble = gb.create().fromJson(json,
QueueTaskAble.class);
processQueueInputObject(queueTaskAble);
}
}
private void processQueueInputObject(QueueTaskAble queueObject) {
queueObject.getQueueTask().run();
// or for asynchronous processing
// new Thread(queueObject.getQueueTask()).start();
}
}
一些输入对象和任务实现
public class InputObjectFooImpl implements QueueTaskAble {
@Override
public Runnable getQueueTask() {
return new InputObjectFooTaksImpl();
}
}
public class InputObjectBarImpl implements QueueTaskAble {
@Override
public Runnable getQueueTask() {
return new InputObjectBarTaksImpl();
}
}
public class InputObjectFooTaksImpl implements Runnable {
@Override
public void run() {
System.out.println("Foo!");
}
}
public class InputObjectBarTaksImpl implements Runnable {
@Override
public void run() {
System.out.println("Bar!");
}
}
最后是一个示例应用程序
public class App {
public static void main(String... args) {
InputObjectGsonBuilder gb = new InputObjectGsonBuilder();
InputObjectGsonQueue gq = new InputObjectGsonQueue();
gq.pushInputObject(gb.create().toJson(new InputObjectFooImpl(),
QueueTaskAble.class));
gq.pushInputObject(gb.create().toJson(new InputObjectBarImpl(),
QueueTaskAble.class));
gq.processQueue();
}
}
输出
Foo!
Bar!