Siraj Syed
Siraj Syed

Reputation: 1361

@Schedule Doesnt trigger the Service at a fixed time

I am trying to schedule a service from spring-boot at a fixed rate with an initial delay,
After deploying, I was expecting that the read method should be executed after 5 secs and every 10 seconds, but nothing is displayed in the console. Here is my main application class

@SpringBootApplication
@EnableScheduling
public class ArgusAPIApplication {

    @Value("${proxy.host}")
    private String proxyHost;
    @Value(("${proxy.port}"))
    private int proxyPort;
    @Value(("${readTimeout}"))
    private int readTimeout;
    @Value(("${connectTimeout}"))
    private int connectTimeout;


    public static void main(String[] args) {
        SpringApplication.run(ArgusAPIApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress(proxyHost,proxyPort));
        requestFactory.setProxy(proxy);
        requestFactory.setReadTimeout(readTimeout);
        requestFactory.setConnectTimeout(connectTimeout);
        return new RestTemplate(requestFactory);
    }


}

And I want the service to be scheduled at a fixed interval The Service Class with a method annotated with @Scheduled is given below

@Service
    public class targusTractoscalingScheduler {

        private static final Logger logger = LogManager.getLogger(targusTractoscalingScheduler.class);

        @Autowired
        targusController targusController;

        @Autowired RestTemplate restTemplate;

        private boolean firstTime = true;

        @Value("aaaaaaaaaaaaaaaa")
        private String apiKey;

        @Value("xxxxxxxxxxxxxxxxxxxxxxx")
        private String applicationKey;

        @Value("${ddUrl}")
        private String ddUrl;

        @Value("/filter/instance")
        private String ddInstanceApiPath;

        @Value("/stop/job/{Tractoscalinggroup}")
        private String ddStopJobPath;

        /**
         * Scheduler method that will run at every predefined interval.
         */

        @Scheduled(initialDelayString = "5000", fixedRateString = "10000")
        public void targusAsgAndCmdbASgCompare() {
            List<targusData> targusDatas = targusController.gettargusData();
            if (CollectionUtils.isEmpty(targusDatas)) {
                logger.debug("Empty list obtained from targus, so simply exiting from method");
                return;
            }
            processtargusData(targusDatas);
            firstTime = false;
        }

But the service isnt triggering,the springboot documenattion also gives similar implementation.

Upvotes: 0

Views: 94

Answers (1)

Abder KRIMA
Abder KRIMA

Reputation: 3678

Here is an example :

@EnableScheduling
@SpringBootApplication
public class MyExampleCronApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyExampleCronApplication.class, args);
    }

    @Scheduled(initialDelay=5000,fixedRate=10000)
    public void test() {
        System.out.println(Date.from(Instant.now()).getSeconds());
    }
}

The output :

enter image description here

NB: i separate the code by putting the scheduled methode in a service and it work find.

Upvotes: 1

Related Questions