2015-06-01 98 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(); 
     } 
    } 

} 

糾正我,如果我的理解是錯誤的。