springmvc配置定时任务和问题解决

1、spring mvc 配置 定时任务:

首先:要在spring的配置文件中进行配置,我的名称名称为:spring-config.xml,此文件在web.xml中进行引入。

web.xml:

<!-- spring config -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring/config/spring-config.xml</param-value>
</context-param>

spring-config.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"  -- 需要
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/task   -- 需要
      http://www.springframework.org/schema/task/spring-task.xsd">  -- 需要

   <context:component-scan base-package="cn.j0.*" />
   <context:annotation-config />
   <task:annotation-driven />   -- 主要的配置定时任务
   
   <import resource="classpath:spring/core/spring-core-common.xml" />
   <import resource="classpath:spring/core/spring-core-cache.xml" />
   <import resource="classpath:spring/core/spring-core-database.xml" />
   <import resource="classpath:spring/core/spring-core-service.xml" />
   <import resource="classpath:spring/core/spring-core-shiro.xml" />
   <import resource="classpath:spring/project/spring-*-config.xml" />
   <!--<import resource="classpath:spring/rabbitmq/rabbitMQ.xml" />-->
   <import resource="spring-develop.xml" />
</beans>

第二创建定时任务类:

@Component("taskJob")
public class Quartz {
    @Autowired
    LogService logService;
    @Autowired
    private QuartzComponent quartzComponent;


    /**
     * CRON表达式                含义
     "0 0 12 * * ?"            每天中午十二点触发
     "0 15 10 ? * *"            每天早上10:15触发
     "0 15 10 * * ?"            每天早上10:15触发
     "0 15 10 * * ? *"        每天早上10:15触发
     "0 15 10 * * ? 2005"    2005年的每天早上10:15触发
     "0 * 14 * * ?"            每天从下午2点开始到2点59分每分钟一次触发
     "0 0/5 14 * * ?"        每天从下午2点开始到2:55分结束每5分钟一次触发
     "0 0/5 14,18 * * ?"        每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
     "0 0-5 14 * * ?"        每天14:00至14:05每分钟一次触发
     "0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发
     "0 15 10 ? * MON-FRI"   每个周一、周二、周三、周四、周五的10:15触发
     */

    @Scheduled(cron = "0 1 15 * * ?")
    public void classChartTask() throws InterruptedException {
        try {
            //获取本机的Ip地址
            String ip =  InetAddress.getLocalHost().getHostAddress();
            logService.insertLog( new Logs("taskJob", "定时任务-模拟考试成绩",ip+"定时任务开始-start",new Date(  )  ) );
            quartzComponent.classChart();
            logService.insertLog( new Logs("taskJob", "定时任务-模拟考试成绩",ip+"定时任务结束-end",new Date(  )  ) );
            System.out.println("执行完成定时任务...OK");
        } catch (UnknownHostException e) {
            logService.insertLog( new Logs("taskJob", "定时任务","定时任务结束获取IP地址失败",new Date(  )  ) );
        }

    }


}

问题1:

      开发环境上,配置的定时任务只执行了1次,而发布到服务器的tomcat上后,定时任务却执行了两次。而且查看日志的时候,两个定时任务的开始时间是完全一致。而在idea上没有问题,发布在tomcat上有问题。

原因为:tomcat启动时,对定时任务初始化两次所致。

解决办法为:

       在tomcat目录下,找到service.xml文件,原始文件如下:

<Host name="localhost"  appBase="webapps"  unpackWARs="true" autoDeploy="true">
         <Context docBase="admin" path=""/>

         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />

   </Host>

修改如下所示:

 <Host name="localhost"  appBase=""   unpackWARs="true" autoDeploy="true">    --  appBase清空
<Context docBase="D:\apache-tomcat-8.5.29\webapps\admin" path=""/>    -- docBase 设置为绝对路径

 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

 

问题2:

      问题1解决后,发布到nginx上后,因为是多个负载服务器,每个服务器都会执行定时任务,造成定时任务重复执行。

解决方法:

      只保留一个服务器上面的定时任务。其他服务器的定时任务取消。

方法:把spring-config.xml 定时任务的代码注释掉:

<context:component-scan base-package="cn.j0.*" />

<context:annotation-config />

<!-- <task:annotation-driven />  -->      -- 注释此代码

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值