Java will truncate any property value string that starts with a number followed by a space. This tip has a small project with a Spring Scheduled task that uses a CRON schedule to demonstrate both the problem and the solution.
Introduction
When trying to parameterize a Spring Task's CRON Schedule using environment variables, I hit a problem where the full schedule was not being received from the properties, but only in the cases where the first value was a number. Searching Google gave me zero examples of my type of problem, so I was on my own.
The solution I found involves using a separate environment variable for each section of the schedule.
Using the Code
The attached Maven-based project is a simple one where a Spring Task will print out the current time to the console. I use Intellij's IDEA, but you should be able to adapt this to Eclipse, or really any other IDE of your liking.
The task class is:
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron ="${cron.schedule}")
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
In my application.properties
, I originally set the schedule from a single Environment Variable:
cron.schedule = ${test_cron}
If I run this with CRON schedule of "*/5 * * * * ?
" (every 5 seconds) set in the Environment Variable "test_cron
", I get the expected outcome:
2021-04-29 12:17:50.437 INFO 22420 --- [ main] o.s.s.c.ThreadPoolTaskScheduler
: Initializing ExecutorService 'taskScheduler'
2021-04-29 12:17:50.480 INFO 22420 --- [ main] c.e.s.SchedulingTasksApplication
: Started SchedulingTasksApplication in 2.441 seconds (JVM running for 3.931)
2021-04-29 12:17:55.008 INFO 22420 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks
: The time is now 12:17:55
2021-04-29 12:18:00.014 INFO 22420 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks
: The time is now 12:18:00
2021-04-29 12:18:05.005 INFO 22420 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks
: The time is now 12:18:05
But if I run it with a schedule of "0 25 * * * ?
" (at 25 minutes past the hour), I get an error saying my schedule is invalid and only has a zero in it:
Encountered invalid @Scheduled method 'reportCurrentTime': For input string: ""0"
To get around this, I altered the application properties to use one Environment
variable per schedule section, with a space between:
cron.schedule = ${cron_sec} ${cron_min} ${cron_hr} ${cron_day} ${cron_mth} ${cron_wk}
And ran this with each environment variable set to the needed value. Similar example to the failed one above:
cron_sec=0
cron_min=30
cron_hr=*
cron_day=*
cron_mth=*
cron_wk=*
With which I get the expected output:
2021-04-29 12:29:25.539 INFO 22896 --- [ main] c.e.s.SchedulingTasksApplication
: Started SchedulingTasksApplication in 3.677 seconds (JVM running for 5.441)
2021-04-29 12:30:00.003 INFO 22896 --- [ scheduling-1] c.e.schedulingtasks.ScheduledTasks
: The time is now 12:30:00
If this helps even one other person with this type of problem, then my work is done! :)
Points of Interest
As mentioned, Google was of completely zero help with this problem. I could find nothing related to truncation of Application.properties String
values that start with a number followed by a space. Is this a bug or "feature" of Java? I'll let you decide!
History