@RefreshScope stops @Scheduled task

前端 未结 4 713
心在旅途
心在旅途 2020-12-11 22:48

I have a monitoring app wherein I am running a fixedRate task. This is pulling in a config parameter configured with Consul. I want to pull in updated configuration, so I ad

4条回答
  •  萌比男神i
    2020-12-11 23:19

    I'm successfully get & override the values from consul config server using RefreshScopeRefreshedEvent

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    @RefreshScope
    public class AlertSchedulerCron implements ApplicationListener {
    
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        @Value("${pollingtime}")
        private String pollingtime;
        
        /*
         * @Value("${interval}") private String interval;
         */
        @Scheduled(cron = "${pollingtime}")
        //@Scheduled(fixedRateString = "${interval}" )
        public void task() {
    
            System.out.println(pollingtime);
            System.out.println("Scheduler (cron expression) task with duration : " + sdf.format(new Date()));
        }
    
        @Override
        public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
            // TODO Auto-generated method stub
            
        }
    
        
    }
    

提交回复
热议问题