Dynamic Job Data using Quartz
When my process gets a message, it needs to start a timer and execute some logic in X seconds. These开发者_如何学Python jobs need to be stored in a JDBC store, which as far as I can tell may be irrelevant to this question.
Based on what I've read, I should be able to assign a JobDataMap with different values for similar properties to a single Job class, but I'm unable to find any documentation or examples to back this use-case. Perhaps my Google-fu is weak.
Does that make sense? Have one Job class and somehow store a JobDataMap to populate that Job class and run it on a per-message basis?
org.quartz.Trigger
has both getJobDataMap()
(which will new
up one if necessary) and setJobDataMap()
to get the trigger's JobDataMap.
The simplest usage would be:
Trigger t = new SimpleTrigger(...);
t.getJobDataMap().put("foo", "bar");
To init with an existing Map of values:
Map data = new HashMap();
data.put("foo", "bar");
t.setJobDataMap(new JobDataMap(data));
To get the data when your job executes
public void execute(JobExecutionContext context) throws JobExecutionException
{
String fooValue = context.getMergedJobDataMap().get("foo");
}
Documentation on JobDataMap in the tutorial.
Hi cansando (you speak spanish???). I going to give you my example of Quartz.
public static void main(String[] args) {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
try {
Scheduler sched;
sched = schedFact.getScheduler();
sched.start();
JobDetail jobd = new JobDetail("instruccion", "instrucciones",
Mensaje.class);
// TriggerExample trigger = new TriggerExample("instruccion",
// "instrucciones");
jobd.setRequestsRecovery(true);
SimpleTrigger trigger2 = new SimpleTrigger("lanzador principal", "lanzadores", "instruccion","instrucciones" , Calendar.getInstance().getTime(), null, 100, 10000);
trigger2.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
sched.scheduleJob(jobd, trigger2);
//
// }
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
public class Mensaje implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Hola Mundo");
}
}
public class TriggerExample extends Trigger {
/**
*
*/
private static final long serialVersionUID = -4926331843008217753L;
Date fechaAEjecutar;
public TriggerExample(){
super();
}
public TriggerExample(String name){
super(name);
}
public TriggerExample(String name, String group){
super(name, group);
}
public TriggerExample(String name, String group,String jobname, String jobgroup){
super(name,group,jobname,jobgroup);
}
@Override
public Date computeFirstFireTime(Calendar arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int executionComplete(JobExecutionContext arg0,
JobExecutionException arg1) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Date getEndTime() {
// TODO Auto-generated method stub
return null;
}
@Override
public Date getFinalFireTime() {
// TODO Auto-generated method stub
return null;
}
@Override
public Date getFireTimeAfter(Date arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Date getNextFireTime() {
// TODO Auto-generated method stub
return null;
}
@Override
public Date getPreviousFireTime() {
// TODO Auto-generated method stub
return null;
}
@Override
public Date getStartTime() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean mayFireAgain() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setEndTime(Date arg0) {
// TODO Auto-generated method stub
}
@Override
public void setStartTime(Date arg0) {
this.fechaAEjecutar = arg0;
System.out.println("Cargue la fecha el data con valor\n");
System.out.println(this.fechaAEjecutar.getTime());
}
@Override
public void triggered(Calendar arg0) {
// TODO Auto-generated method stub
}
@Override
public void updateAfterMisfire(Calendar arg0) {
// TODO Auto-generated method stub
}
@Override
public void updateWithNewCalendar(Calendar arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
protected boolean validateMisfireInstruction(int arg0) {
// TODO Auto-generated method stub
return false;
}
}
In this line SimpleTrigger trigger2 = new SimpleTrigger("lanzador principal", "lanzadores", "instruccion","instrucciones" , Calendar.getInstance().getTime(), null, 100, 10000);
can you set the time to execute and repeat.
精彩评论