(Turn) Solve the problem that the Quartz timed task is triggered twice

Abstract: To solve the problem that the quartz timed task is triggered twice Method 1. By changing the TOMCAT configuration file server.xml configuration method 2. First extract the quartz configuration information and save it as a separate file, such as applicationContext-quartz.xml and then modify web.xml, which can be loaded when the web container starts. (This article does not explore this method in depth)

When many people use spring's Quartz to configure timed tasks, they will find that the specified timed method is executed twice at the same time after each timed time arrives.

(PS The Jfinal construction used in my project also has the same problem.)

 

 Solve the problem that the quartz timed task is triggered twice:

Among them, <Host/> tells tomcat to load all project project files under webapps at startup, and <Context/> makes tomcat load it again (usually, <Context/> is configured , mainly because when you want to access the domain name, you will The reason for removing the project name is configured), in this case, the quartz timer in the project will be triggered twice and executed twice.

 

Change the autoDeploy="false" deployOnStartup="false"    in <Host/>  to avoid the quartz timing task being triggered twice due to self-startup in the tomcat server. (Of course, it can also be modified according to needs, after all, the needs of each project are different.)

    1. The value of the autodeploy attribute is set to false. If this item is set to true, it means that when the Tomcat service is running, it can monitor the files under the appBase. If a new web application is added, it will automatically publish the web application. false will not.

    2. Increase deployOnStartup="false", which means that when the Tomcat server starts, all web applications in the appBase directory will not be automatically published.

    In this case, when tomcat starts, applications under appBase will not be automatically released, and applications under appBase will not be automatically released after startup.

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="false" deployOnStartup="false">
	<Context path="" docBase="/eugeneqiu_test" debug="0" reloadable="true"/>
      </Host>

 

  

Of course, there is another configuration method on the network:

 It is to modify the docBase and add the "/webapps" path in <Context/>. Of course, the project access path becomes http://localhost/eugeneqiu_test  . It is really uncomfortable to see the project name, so I don't use this method.

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
	<Context path="" docBase="/webapps/eugeneqiu_test" debug="0" reloadable="true"/>
      </Host>

 

 

Solve the problem that the quartz timed task is triggered twice

Method 1. By changing the TOMCAT configuration file server.xml configuration ( available after testing )

Method 2. First extract the quartz configuration information and save it as a separate file, such as applicationContext-quartz.xml, and then modify the web.xml so that the file can be loaded when the web container starts. (This article does not explore this method in depth) ( not tested )

 

Comes with: test case for quartz

java test case code 1:

package com.eugeneqiu_test.test;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 每5秒自动运行
 * @author EugeneQiu
 */
public class Every5s{
    public static void orderEveryhour(String[] args) throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        JobDetail jobDetail = JobBuilder.newJob()
                .ofType(Test.class)
                .usingJobData("Test1","Quartz")
                .withIdentity("Test1","Group1")
                .build();//通过JobBuilder构建JobDetailImpl 实例,也可以直接new JobDetailImpl

        Trigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))  //还有更多时间格式,详情可以百度一下
                .forJob("Test1","Group1")       //Trigger找到对应的名称为Test1组为Group1的Job,如果不存在则会在执行scheduler.scheduleJob(jobDetail,trigger);报错
                .build();//通过TriggerBuilder构建CronTriggerImpl实例,也可以直接new CronTriggerImpl

        scheduler.scheduleJob(jobDetail,trigger);//任务每5秒触发一次

        scheduler.start();
    }
}

 

 

@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		 System.out.println("Hello 要开始自动运行喽~~~~~~" );
		 //testSomething(); //这里可以写一下想定时运行的方法
		 System.out.println("Hello 自动运行结束~~~~~");
		
	}


 

REFS:https://my.oschina.net/EugeneQiu/blog/265040?p=1

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326663178&siteId=291194637