2015-06-01 45 views
1

在我们的REST服务中,我们希望实现一项每10秒钟检查一次的作业。所以我们认为我们可以使用Quartz来制作一个覆盖这个的Job。但问题是,我们需要注入一个单例,因为它用于作业,而作业似乎不在我们的服务的上下文中,所以注入的类始终为空(NullPointerException)。泽西岛2.0:创建重复作业

那么有没有另一种可能的解决方案来实现这样的工作,而不使用Quartz?已经尝试编写我们自己的JobFactory,它将作业与BeanManager连接起来,但它根本不起作用。

这是工作不工作的代码:

@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); 
       } 
      } 
     } 
    } 
} 

的共享内存总是空。

+0

您有一个Rest API,并且您需要每10秒完成一次任务,并且该作业对RestAPI有一些对象依赖关系。我的理解是正确的? –

+0

是的,这是正确的 – AKR

回答

0

我已经使用Scheduler上下文映射来实现这一点。你可以试试这个。

在REST API,当我们创建了一个调度程序,我们可以使用语境映射传递参数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(); 
     } 
    } 

} 

纠正我,如果我的理解是错误的。