在我的 Spring MVC 项目中有一个 Quartz 调度程序,它一次运行两次如何修复它并一次执行一次。我的 web Initializer 类在哪里,@WebListenerpublic class QuartzListener extends QuartzInitializerListener {@Overridepublic void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServletContext ctx = sce.getServletContext(); StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY); try { Scheduler scheduler = factory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(TestJob.class).build(); Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule( CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).startNow().build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } catch (Exception e) { ctx.log("There was an error scheduling the job.", e); } }}和测试班, public class TestJob implements Job {@Overridepublic void execute(final JobExecutionContext ctx) throws JobExecutionException { System.out.println("Executing Job"); } }和 web 初始化器类,如, public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses(){ return new Class[]{WebSecurityConfig.class}; } @Override protected Class<?>[] getServletConfigClasses(){ return new Class[]{WebAppConfig.class, HibernateConfiguration.class}; } @Override protected String[] getServletMappings(){ return new String[]{"/"}; } }我的 WebConfig 是,@Configuration@EnableWebMvc@ComponentScan(basePackages="com.neron")public class WebAppConfig extends WebMvcConfigurerAdapter {@Beanpublic ViewResolver resourceViewResolver(){ ResourceBundleViewResolver viewResolver = new ResourceBundleViewResolver(); viewResolver.setOrder(0); viewResolver.setBasename("views"); return viewResolver;}............ ...............在这里我每分钟得到两次“执行工作”,但我每分钟只想要一次“执行工作”。 任何人都可以解决吗?提前致谢!
2 回答
慕森卡
TA贡献1806条经验 获得超8个赞
在这里,您的解决方案在作业类的顶部添加此注释
import org.quartz.DisallowConcurrentExecution;// import this @DisallowConcurrentExecution
如果你在集群环境中工作,那么不同的实现。如果你需要在这里发表评论。或者如果您的问题解决了,请接受它。
添加回答
举报
0/150
提交
取消