I'm writing a task executing framework structured like below:
@XmlRoot("")
class PayloadPojo {
String cron;
String name;
int id;
String user;
}
@Task(payload = PayloadPojo.class)
class MyTask {
String cron;
String user;
int id;
...
// other fields;
// Task execution logic
}
@CallBack(task = MyTask.class)
class MyCallBack {...}
I'm trying to write a simple framework here that users can define their own payload POJOs to bind data from remote events, and define tasks to be run on that data with the provided annotations. Suppose when the callback is called, the String
payload is passed in, I can reflectively get the callback's target task class the task's payload POJO.
The question is how can I instantiate MyTask
from PayloadPojo
instance by, say, comparing the field names and copy over identical ones. My logic would look like:
// In MyCallBack class
public MyTask getTask(String payloadStr) {
Class callbackCls = AopUtils.getTargetClass(this);
if (clazz.isAnnotationPresent(CallBack.class)) {
// Get task param, which is MyTask.class, and then get its payload: PayloadPojo.class
// Deserialize to PayloadPojo instance
Class taskCls = callbackCls .getAnnotation(CallBack.class).task();
Class payloadCls = taskCls.getAnnotation(Task.class).payload();
Object payload = payloadCls.cast(ummarshaller.ummarshall(payloadStr));
// Here is my question:
// Need to create instance of taskCls from payload object
// Note the payload is of type Object
return myTaskInstance;
}
return null;
}
The logic here is that POJO class and Task class should have some identical fields (because task execution relies on data in the callback). But to have a better modularity, a dedicated POJO class is still needed.
Aucun commentaire:
Enregistrer un commentaire