---
title: "springboot 定时任务重复执行"
author: "larryscale"
date: "2019-06-04T17:35:12Z"
tags: []
categories: ["编程语言"]
slug: "springboot"
draft: false
lastmod: "2019-06-04T17:35:45Z"
id: 2441609
url: "https://itsaga.iteye.com/blog/2441609"
---

开发一数据接收WebService，里面用到定时任务@Scheduled  

每次会重复运行三次

 

 

```
@Slf4j
@PropertySource("classpath:common-config.properties")
@Component
public class ScheduledController {
    @Scheduled(cron="${jobs.schedule.report}")
    public void dataReportScheduled() {
        try{
            log.info("...end report data scheduled!\r\n\");
        }catch (Exception e){
            log.error("定时任务出现异常："+e.getMessage());
        }
    }
}

```

最后查出来问题在下面的配置文件中红色字体部分，注释掉问题即可解决

```
package com.xxxxxxx.config;

import com.xxxxxxx.interceptor.AuthInterceptor;
import com.xxxxxxx.service.biz.IDataCenterBaseService;
import com.xxxxxxx.service.biz.impl.DataCenterBaseService;
import com.xxxxxxx.service.biz.impl.TransferDataServiceImpl;
import com.xxxxxxx.webservice.TransferDataService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.xml.ws.Endpoint;

@Configuration
public class CXFConfig {

    /*******************************************************
     * WebService配置
     * @return
     *******************************************************/
    @Bean
    public IDataCenterBaseService dataCenterService() {
        return new DataCenterBaseService();
    }

    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor(dataCenterService());
    }

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/service/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public TransferDataService transferDataService() {
        return new TransferDataServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), transferDataService());
        endpoint.publish("/transfer");
        endpoint.getInInterceptors().add(authInterceptor());
        return endpoint;
    }

    /*******************************************************
     * Web应用配置
     * @return
     *******************************************************/
    @Bean
    public ServletRegistrationBean restServlet(){
        //注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //base package
        applicationContext.scan("com.xxxxxxx");
        //通过构造函数指定dispatcherServlet的上下文
        DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

    @Bean
    public ServletRegistrationBean cooleadServlet(){
        //注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //base package
        applicationContext.scan("com.xxxxxxx");
        //通过构造函数指定dispatcherServlet的上下文
        DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        //指定urlmapping
        registrationBean.addUrlMappings("/xxxxxxx/*");
        //指定name，如果不指定默认为dispatcherServlet
        registrationBean.setName("xxxxxxx");
        return registrationBean;
    }

}
```

 

 

去网上搜了一下相关的帖子，有类似问题的

[https://blog.csdn.net/tly\_74125/article/details/80925599](https://blog.csdn.net/tly_74125/article/details/80925599)