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);
}
}
}
}
}
的共享內存總是空。
您有一個Rest API,並且您需要每10秒完成一次任務,並且該作業對RestAPI有一些對象依賴關係。我的理解是正確的? –
是的,這是正確的 – AKR