我已經寫了一個線程類,它的run方法將從數據庫中提取某些提交的作業並執行它們。我已經在會話監聽器的上下文初始化方法中啓動了此線程,以便線程在我的服務器重新啓動後立即啓動。 在運行方法中,第一步是拾取數據庫中處於「已提交」狀態的所有任務,然後逐個執行它們。 我在所有任務完成後提交新任務時遇到問題執行。原本我的線程應該選擇這個,但它不是。我已經檢查了線程的狀態,它是「RUNNABLE」。一旦我重新啓動服務器,線程正在完成這個任務,但其他明智的。讓一個線程運行來執行任務
public void contextInitialized(ServletContextEvent sce) {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = new AnnotationConfiguration().
buildSessionFactory(serviceRegistry);
logger.debug("sessionFactory created in HibernateSessionFactoryListener.contextInitialized() : " + sessionFactory);
// get servletcontext
context = sce.getServletContext();
// start session factory
context.setAttribute("datasource", sessionFactory);
isSessionActive = true;
//SubmitTaskThread t = new SubmitTaskThread();
t.start();
}
以上是在語境中的代碼運行方法初始化
代碼:
public void run(){
//logger1.debug("in run method of thread class");
//SubmitTask st = new SubmitTask();
//st.runJobs();
//TASKS THAT ARE SUBMITTED
try {
pendingList=getPendingList();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (pendingList.size()>0){
boolean isObjStorage=false;
CornicheVO runVO;
Transfer newTransferJob;
List<String> locations;
JobVO jobSubmitVo;
for (int listSize=0;listSize<pendingList.size();listSize++){
locations = new ArrayList<String>();
runVO=pendingList.get(listSize);
newTransferJob=new Transfer();
jobSubmitVo=new JobVO();
newTransferJob.setTask_id(runVO.getTask_Id());
locations.add(runVO.getSrcLocDesc());
locations.add(runVO.getDstLocDesc());
try {
drivesInfo = getLocationInfo(locations);
} catch (CornicheException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
for (NasDrive drive : drivesInfo) {
if (drive.getSrc_loc_desc().equals(
runVO.getSrcLocDesc())) {
if ("OBJSTORAGE".equals(drive.getType())) {
isObjStorage = true;
newTransferJob.setSrc_server_type("OOSS");
} else {
newTransferJob.setSrc_server_type("NAS");
}
newTransferJob.setSrc_server(drive.getSrc_server());
newTransferJob.setSrc_server_dflt_loc(drive.getSrc_Loc());
newTransferJob.setSrc_server_id(drive.getUsr_id());
newTransferJob.setSrc_server_pswd(drive.getPswd());
} else {
if ("OBJSTORAGE".equals(drive.getType())) {
newTransferJob.setDst_server_type("OOSS");
} else {
newTransferJob.setDst_server_type("NAS");
}
newTransferJob.setDst_server(drive.getSrc_server());
newTransferJob.setDst_server_dflt_loc(drive.getSrc_Loc());
newTransferJob.setDst_server_id(drive.getUsr_id());
newTransferJob.setDst_server_pswd(drive.getPswd());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jobSubmitVo.setClientName(runVO.getClientName());
jobSubmitVo.setSrcServer(runVO.getSrcLoc());
jobSubmitVo.setDstServer(runVO.getDstLoc());
jobSubmitVo.setSrcServerDesc(runVO.getSrcLocDesc());
jobSubmitVo.setDstServerDesc(runVO.getDstLocDesc());
jobSubmitVo.setFileName(runVO.getFileName());
jobSubmitVo.setSizeOfFile("32");
jobSubmitVo.setUsrName(runVO.getLstUpdtIdCd());
jobSubmitVo.setMetaTag(runVO.getMetaTag());
jobSubmitVo.setRententionPeriod(runVO.getRetentionPrdcurrent());
jobSubmitVo.setTask_Id(runVO.getTask_Id());
if (!isObjStorage) {
//CURLProcessor uploader = new CURLProcessor();
// Export the scripts first
util.exportFiles(newTransferJob.getSrc_server(), newTransferJob.getSrc_server_id(),newTransferJob.getSrc_server_pswd(),newTransferJob.getSrc_server_dflt_loc());
uploadtrasnfer(newTransferJob, runVO.getFileName(), jobSubmitVo);
} else {
util.exportFiles(newTransferJob.getDst_server(), newTransferJob.getDst_server_id(),newTransferJob.getDst_server_pswd(),newTransferJob.getDst_server_dflt_loc());
downloadTrasnfer(newTransferJob, runVO.getFileName(), jobSubmitVo);
}
try {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想你需要在這裏發佈相關的代碼。沒有它就很難理解什麼是錯的。 – Serge
很難知道沒有例子的問題 – BaneDad
如果你真的想要一個答案,你需要構建一個[mcve],這將需要很長時間,我敢肯定,因爲你需要重寫代碼,以便我們不需要數據庫來運行它並查看您的問題。但是,如果不付出努力,我看不到你的答案。 – RealSkeptic