2015-05-25 5 views
15

मैं very simple examples वसंत में एक क्रॉन जॉब फिर भी मैं हर बार लॉग ऑन मेरी बिलाव स्टार्टअप में यह त्रुटि मिलती रहती स्थापित करने के लिए ऑनलाइन का पालन किया है:@Scheduled का उपयोग करना और @EnableScheduling लेकिन देता NoSuchBeanDefinitionException

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:191 - 
Could not find default TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined 

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:202 - Could not  
find default ScheduledExecutorService bean 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying  
bean of type [org.springframework.scheduling.TaskScheduler] is defined 

और

1) @Configuration वर्ग: 2 जावा वर्गों क्रॉन लागू करने के लिए इस्तेमाल किया

@Configuration 
@EnableScheduling 
public class ClearTokenStoreCronEnable {  
    final static Logger log = 
    LoggerFactory.getLogger(ClearTokenStoreCronEnable.class); 
    private @Autowired TokenStoreRepository tokenStoreRepository; 
} 

और क्रॉन नौकरी वर्ग:

@Service 
public class ClearTokenStoreWorkerService { 

    final static Logger log = LoggerFactory.getLogger(ClearTokenStoreWorkerService.class); 
    private @Autowired TokenStoreRepository tokenStoreRepository; 

    //@Scheduled(fixedDelay=5000) 
    //run daily at midnight 
    @Scheduled(cron = "0 0 * * * *") 
    public void tokenStoreTable() { 
     log.debug("tokenstore table truncated - start"); 
     tokenStoreRepository.deleteAll(); 
     log.debug("tokenstore table truncated - end"); 
    } 
} 

एक साइड नोट के रूप में, क्रॉन नौकरी मध्यरात्रि में चलती है लेकिन यह दूसरी बार यादृच्छिक रूप से चलती है। सुनिश्चित नहीं हैं कि अगर यह एक बग है या मेरे क्रॉन अभिव्यक्ति गलत है: @Scheduled(cron = "0 0 * * * *")

मेरे इस समय मुख्य चिंता है कि मैं क्यों ScheduledAnnotationBeanPostProcessor त्रुटियों मिल रहा है? यह एक टास्कशेड्यूलर और अनुसूचित एक्सेक्टर सेवा की तलाश में है। मुझे दिन में एक बार इसे आग लगाना होगा। मैं कोई समवर्ती प्रसंस्करण नहीं कर रहा हूं या जहां मुझे कई धागे की आवश्यकता है। आखिरकार ये त्रुटियां हानिकारक हैं या क्या मुझे उन्हें ठीक करने की ज़रूरत है?

+0

मेरे ऊपर प्रारंभिक क्रॉन अभिव्यक्ति का उपयोग कर रहा था, मुझे सही नहीं मिला। मध्यरात्रि में दिन में एक बार फायरिंग के लिए सही अभिव्यक्ति है: @ शेड्यूल्ड (क्रॉन = "0 0 * * *?") – logixplayer

उत्तर

14

संपादित करें: सर्वश्रेष्ठ उत्तर here है और यह एक निर्वाहक बनाने होते हैं:

@Configuration 
@EnableAsync 
public class AppContext extends WebMvcConfigurationSupport { 
    @Bean 
    public Executor taskExecutor() { 
     return new SimpleAsyncTaskExecutor(); 
    } 
} 

पिछला (अभी भी मान्य है):

NoSuchBeanDefinitionException एक डीबग गंभीरता के साथ लॉग ऑन है और सुरक्षित रूप से अनदेखा किया जा सकता। आप ScheduledAnnotationBeanPostProcessor के लिए स्रोत कोड को देखो, तो आप देखते हैं कि यह पहली बार एक TaskScheduler, तो एक ScheduledExecutorService निकलने की कोशिश करता है, तो उस पर "डिफ़ॉल्ट अनुसूचक पर वापस गिरने" चलती है:

if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) { 
     Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type"); 
     try { 
      // Search for TaskScheduler bean... 
      this.registrar.setScheduler(this.beanFactory.getBean(TaskScheduler.class)); 
     } 
     catch (NoUniqueBeanDefinitionException ex) { 
      throw new IllegalStateException("More than one TaskScheduler exists within the context. " + 
        "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " + 
        "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex); 
     } 
     catch (NoSuchBeanDefinitionException ex) { 
      logger.debug("Could not find default TaskScheduler bean", ex); 
      // Search for ScheduledExecutorService bean next... 
      try { 
       this.registrar.setScheduler(this.beanFactory.getBean(ScheduledExecutorService.class)); 
      } 
      catch (NoUniqueBeanDefinitionException ex2) { 
       throw new IllegalStateException("More than one ScheduledExecutorService exists within the context. " + 
         "Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " + 
         "ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex); 
      } 
      catch (NoSuchBeanDefinitionException ex2) { 
       logger.debug("Could not find default ScheduledExecutorService bean", ex); 
       // Giving up -> falling back to default scheduler within the registrar... 
      } 
     } 
    } 

आप अपवाद को हटा सकते हैं

<logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/> 

की तरह कम से कम org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor पर एक जानकारी गंभीरता की स्थापना, जब logback का उपयोग करके।

second (0-59), minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (1-7, 1 = Sunday) 

वाक्य रचना quartz docs में पाया जा सकता:

क्रॉन अभिव्यक्ति छः फ़ील्ड होते हैं। मुझे "?" के बारे में निश्चित नहीं है चरित्र, क्योंकि यद्यपि पेज का कहना है

The '?' दिन-दर-दिन और सप्ताह के दिनों के लिए चरित्र की अनुमति है। इसका उपयोग "कोई विशिष्ट मान" निर्दिष्ट करने के लिए किया जाता है। यह तब उपयोगी होता है जब आपको दो क्षेत्रों में से किसी एक में कुछ निर्दिष्ट करने की आवश्यकता होती है, लेकिन दूसरी नहीं।

उस पृष्ठ पर उदाहरण वास्तव में प्रयोग करते हैं? यहां तक ​​कि जब दूसरा क्षेत्र * है। IMHO सभी आदेश हर आधी रात को निष्पादित करने में सिर्फ * के साथ काम करना चाहिए, ताकि, अभिव्यक्ति होना चाहिए

0 0 0 * * * 
2

मैं मानता हूँ कि आप इसे अनदेखा कर सकते हैं लेकिन सिर्फ गंभीरता से बदल रहा यह ठीक नहीं होगा। मेरे पास एक ही समस्या थी लेकिन मैं एनोटेशन के बजाय एक्सएमएल का उपयोग कर रहा हूं, और मेरे मामले में ऐसा इसलिए हुआ क्योंकि मैंने निष्पादक को मेरी बीन परिभाषा में शामिल नहीं किया था। इसलिए इसे ठीक करना:

<task:annotation-driven executor="myExecutor" 
    scheduler="myScheduler" /> 
<task:executor id="myExecutor" pool-size="5" /> 
<task:scheduler id="myScheduler" pool-size="10" /> 

मुझे आशा है कि इससे मदद मिलती है।

सम्मान।

11

अपवाद जानकारी "डिफ़ॉल्ट TaskScheduler सेम नहीं खोजा जा सका" के अनुसार, config इस समस्या को हल सिर्फ काम अनुसूचक सेम बनाने के लिए "TaskScheduler" के बजाय "निर्वाहक"

@Configuration 
public class AppContext extends WebMvcConfigurationSupport { 
    @Bean 
    public TaskScheduler taskScheduler() { 
     return new ConcurrentTaskScheduler(); 
    } 

    // Of course , you can define the Executor too 
    @Bean 
    public Executor taskExecutor() { 
     return new SimpleAsyncTaskExecutor(); 
    } 

}

1

परिभाषित करना चाहिए कॉन्फ़िगरेशन में

@Bean 
    public TaskScheduler taskScheduler() { 
     return new ConcurrentTaskScheduler(); 
    } 
संबंधित मुद्दे