0

I am converting ant build to gradle build. All of a sudden @schedule is not working which used to configure from xml. i am trying to initialize a method to get the values from xml. i tried

xml configuration as follows

<interface name="appnotification">      
        <property name="retry_interval" value="0 0/5 * * * ?" /> <!-- every 5 minutes -->
</interface>

java file is as follows

@Component
@EnableScheduling
public class Task {
private static final String INTERFACE_NAME = "appnotification";
@Bean
    public String getConfigRetryValue() {
        Map interfaceProps = Config.getInterfaceProperties(INTERFACE_NAME);     
        return (String) interfaceProps.getOrDefault("retry_interval", DELAY_TIME_IN_MINS);
    }
@Scheduled(cron = "#{@getConfigRetryValue}")
public void processNoti() {}
}

error is as follows

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Task' defined in URL [jar:file:abc.jar!/abc/report/Task.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'processNotifications': Cron expression must consist of 6 fields (found 1 in "#{@getConfigRetryValue}")
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503)
    ... 41 more
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'processNotifications': Cron expression must consist of 6 fields (found 1 in "#{@getConfigRetryValue}")
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:300)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.access$000(ScheduledAnnotationBeanPostProcessor.java:76)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:164)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:495)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:473)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:160)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1520)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    ... 50 more

vbr
  • 13
  • 1
  • 5
  • You can only use value expressions `${..}` not SpEL expression `#{..}`. So in short this isn't going to work. – M. Deinum May 29 '20 at 17:55
  • Hi Deinum, This was working in ant build and not in gradle build. i tried to check all the libraries used in and build and look all of them are same. this can be used as per https://stackoverflow.com/questions/37113327/spring-boot-getting-scheduled-cron-value-from-database?lq=1. @Vignesh can you help me regarding this. – vbr May 30 '20 at 10:53
  • @vbr did you found any solution. I am also facing same problem. – Jaspreet Jolly Jan 12 '22 at 07:46

0 Answers0