泽西岛2.0:创建重复工作 [英] Jersey 2.0: Create repeating job
问题描述
在我们的REST服务中,我们想要实现一项工作,该工作每10秒检查一次.因此,我们认为可以使用Quartz来完成涵盖此任务的工作.但是问题是,我们需要注入一个单例,因为它是在作业中使用的,并且该作业似乎不在我们的服务上下文中,因此注入的类始终为null(NullPointerException).
In our REST-Service we want to implement a job that checks something every 10 seconds. So we thought we could use Quartz to make a Job that cover this. But the problem is, that we need to inject a singleton, because it is used in the job and the job seems to be not in the context of our service, so the injected class is always null (NullPointerException).
那么还有另一种可能的解决方案可以在不使用Quartz的情况下完成这项工作吗?已经尝试编写我们自己的JobFactory来将作业与BeanManager连接,但是它根本没有用.
So is there another possible solution to achieve such a job without using Quartz? Already tried to write our own JobFactory that connects the job with the BeanManager, but it didnt work at all.
这是无法正常工作的代码:
This is the code for the job that is not working:
@Stateless
public class GCEStatusJob implements Job, Serializable{
private Logger log = LoggerFactory.getLogger(GCEStatusJob.class);
@Inject
SharedMemory sharedMemory;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
GoogleComputeEngineFactory googleComputeEngineFactory = new GoogleComputeEngineFactory();
List<HeartbeatModel> heartbeatList = new ArrayList<>(sharedMemory.getAllHeartbeats());
List<GCE> gceList = googleComputeEngineFactory.listGCEs();
List<String> ipAddressList = gceList.stream().map(GCE::getIp).collect(Collectors.toList());
for(HeartbeatModel heartbeat : heartbeatList){
if(ipAddressList.contains(heartbeat.getIpAddress())){
long systemTime = System.currentTimeMillis();
if(systemTime-heartbeat.getSystemTime()>10000){
log.info("Compute Engine mit IP "+heartbeat.getIpAddress()+" antwortet nicht mehr. Wird neu gestartet!");
String name = gceList.stream().filter((i) -> i.getIp().equals(heartbeat.getIpAddress())).findFirst().get().getName();
googleComputeEngineFactory.resetGCE(name);
}
}
}
}
}
SharedMemory始终为空.
SharedMemory is always null.
推荐答案
我已经使用Scheduler
上下文映射来实现此目的.你可以试试看.
I have used Scheduler
context map to achive this. You can try this.
在REST API中,当我们创建一个Scheduler时,我们可以使用上下文映射将参数传递给Job
In REST API when we create a Scheduler we can use the Context map to pass the parameters to Job
@Path("job")
public class RESTApi {
private String _userID;
public String get_userID() {
return _userID;
}
public void set_userID(String _userID) {
this._userID = _userID;
}
@GET
@Path("/start/{userId}")
public void startJob(@PathParam("userId") String userID) {
_userID = userID;
try {
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("updateTrigger");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(1000);
JobDetail job = new JobDetail();
job.setName("updateJob");
job.setJobClass(GCEStatusJob.class);
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.getContext().put("apiClass", this);
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}
JOB实施
public class GCEStatusJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
RESTApi apiClass;
try {
apiClass = ((RESTApi) arg0.getScheduler().getContext().get("apiClass"));
System.out.println("User name is" + apiClass.get_userID());
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
如果我的理解是错误的,请纠正我.
Correct me, if my understanding is wrong.
这篇关于泽西岛2.0:创建重复工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!