Quartz 调度程序无法获取 JNDI DB 资源但仍然启动
Posted
技术标签:
【中文标题】Quartz 调度程序无法获取 JNDI DB 资源但仍然启动【英文标题】:Quartz scheduler failed to get JNDI DB resource but starts anyway 【发布时间】:2014-12-15 08:53:23 【问题描述】:我正在使用 QuartzInitializerListener 在我的 tomcat 8 中初始化 Quartz 调度程序。
当 Quartz 无法连接到我的数据库(JNDI 资源)时:
15-Dec-2014 09:26:58.201 INFO [http-nio-8080-exec-10] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/.../ org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'DBConnectionPool': java.sql.SQLException: Could not retrieve datasource via JNDI url 'java:comp/env/jdbc/DBConnectionPool' java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure
它无论如何都会根据quartz.propertiesn 启动。它没有做任何事情......但如果发生这种情况,我希望 Quartz 停止!
我怎样才能做到这一点?
问候 塞巴斯蒂安
【问题讨论】:
您希望石英停止或整个应用程序停止 @dev 这两个选项对我来说都可以。如果有任何选项可以处理 QuartzInitializerListener 的此错误,它也将是合适的。 【参考方案1】:您可以像这样使用自定义类,而不是使用quartzlistner。 将其用作满足您要求的骨架
@WebServlet(value="/QuartzTest",loadOnStartup=1)
public class QuartzInit extends HttpServlet
private static final long serialVersionUID = 1L;
public static int count=0;
/**
* @see HttpServlet#HttpServlet()
*/
public QuartzInit()
super();
// TODO Auto-generated constructor stub
try
System.out.println("TRying to start Quartz");
JobDetail job = JobBuilder.newJob(AppCheckerJob.class)
.withIdentity("AppCrawlerJob", "InfraStatus_Group").build();
JobDetail job_email = JobBuilder.newJob(EmailJob.class)
.withIdentity("AppCrawlerEmailJob", "InfraStatus_Group").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("AppCrawlerJobTRigger", "InfraStatus_Group")
.withSchedule(
CronScheduleBuilder.cronSchedule("0 /15 * * * ?")
).build();
Trigger trigger_email = TriggerBuilder
.newTrigger()
.withIdentity("AppCrawlerEmailJobTRigger", "InfraStatus_Group")
.withSchedule(
CronScheduleBuilder.dailyAtHourAndMinute(13,16)
).build();
// schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
//if (sched.checkExists(job.getKey())) sched.rescheduleJob(trigger.getKey(), trigger); else sched.scheduleJob(job, trigger);
if(scheduler.checkExists(job.getKey()))
System.out.println("deleting old job netry");
scheduler.deleteJob(job.getKey());
//scheduler.rescheduleJob(trigger.getKey(), trigger);
if(scheduler.checkExists(job_email.getKey()))
System.out.println("deleting old email job netry");
scheduler.deleteJob(job_email.getKey());
//scheduler.rescheduleJob(trigger.getKey(), trigger);
scheduler.scheduleJob(job, trigger);
scheduler.scheduleJob(job_email, trigger_email);
catch (Exception se)
se.printStackTrace();
throw new RuntimeException("Error while initializing quartz!!!");
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
【讨论】:
这对我来说似乎更像是一种解决方法。没有改变石英行为的选项吗? Quartz 有很多未知的行为,这就是为什么我总是自己初始化石英的原因。我希望你能找到一些标准的方法来处理它。 最后我几分钟前在网上找到了一个解决方案:link你可以使用调度器监听器 感谢您提供的信息,我也会尝试使用它。以上是关于Quartz 调度程序无法获取 JNDI DB 资源但仍然启动的主要内容,如果未能解决你的问题,请参考以下文章