cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

@schedule in spring-boot does not work in Java instance in sap cloud platform

ziyi_jiang
Advisor
Advisor
0 Likes
2,268

Dear all,

I found that In SAP Cloud Platform, using spring-boot version 1.3.5 if I want to have a scheduled task using @schedule annotation, it doesn't work in HCP.

However, the same code works in localhost using my own computer.

I'm wondering whether there is anything wrong with my code or configuration file in the project or HCP simply doesn't support schedule in Spring-boot?

Here are the code snippets:

Main function:

package com.sap.job;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}


Scheduled Job

package com.sap.job;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {
	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Scheduled(fixedRate = 10000)
	public void updateDateAvailable() throws ParseException {
		log.info("The time is now {}", dateFormat.format(new Date()));
		long time = System.currentTimeMillis();
		Date date = new Date();
		long minus = 86400000L;
		long endTime = new Date().getTime();
		long startTime = endTime-minus;
		log.info("endTime:"+endTime);
		log.info("startTime:"+startTime);
		log.info("date:"+date.toString());
	}	
}

The result is that I cannot find any log in HCP.

Also trying to insert a record to DB in a schedule doesn't work while it works in localhost.

Has anyone met the issue before? Thanks.

View Entire Topic
ziyi_jiang
Advisor
Advisor
0 Likes

I found the solution. It seems that in log information defined in code is not contained in HCP's log file. But the schedule function works fine.