Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the truncation of Java string attribute value that begins with a single number

Shulou Source: shulou.com Published: 2022-06-01 11:49:26 09月12日 Update

Today, the editor will share with you how to solve the truncation of Java string attribute values starting with a single number. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Brief introduction

When I tried to parameterize the CRON plan of the Spring task with environment variables, I encountered a problem that I did not receive the full plan from the property, but only if the first value was a number. Google search gives me zero examples of my problem type, so I'm on my own.

The solution I found involved using separate environment variables for each part of the plan.

Use the code

The attached Maven-based project is a simple project where the Spring task prints the current time to the console. I use Intellij's IDEA, but you should be able to adjust it to Eclipse or any other IDE you like.

The task classes are:

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 initially set the timesheet from a single environment variable:

Cron.schedule = ${test_cron}

What if I use * / 5 *? The CRON of "" (every 5 seconds) set in the environment variable "test_cron" plans to run this program, and I will get the expected results:

2021-04-29 12 o.s.s.c.ThreadPoolTaskScheduler o.s.s.c.ThreadPoolTaskScheduler: Initializing ExecutorService 'taskScheduler'2021-04-29 12 INFO 22420-- [main] c.e.s.SchedulingTasksApplication: Started SchedulingTasksApplication in 2.441 seconds (JVM running for 3.931) 2021-04-29 12 INFO 55.008 INFO 22420-- [scheduling -1] c.e.schedulingtasks.ScheduledTasks: The time is now 12 c.e.schedulingtasks.ScheduledTasks 17 INFO 552021-04-29 12 INFO 1814 INFO 22420-[scheduling-1] c.e.schedulingtasks.ScheduledTasks: The time is now 12Vera 18Vera 002021-04-29 12Rd 18Vera 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 *?" (in the past 25 minutes), I get an error message indicating that my schedule is invalid and that there are only zeros in it:

Encountered invalid @ Scheduled method 'reportCurrentTime': For input string: "" 0 "

To solve this problem, I changed the application properties to use a variable in each plan section of the Environment, with a space in the middle:

Cron.schedule = ${cron_sec} ${cron_min} ${cron_hr} ${cron_day} ${cron_mth} ${cron_wk}

And run it with each environment variable set to the desired value. Similar to the above example of failure:

Cron_sec=0cron_min=30cron_hr=*cron_day=*cron_mth=*cron_wk=*

I got the expected output:

2021-04-29 12 c.e.s.SchedulingTasksApplication c.e.s.SchedulingTasksApplication: Started SchedulingTasksApplication in 3.677 seconds (JVM running for 5.441) 2021-04-29 12 c.e.s.SchedulingTasksApplication 30 Started SchedulingTasksApplication in 00.003 INFO 22896-[scheduling-1] c.e.schedulingtasks.ScheduledTasks above is "how to solve the truncation of Java string attribute values starting with a single number". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Variables environment time knowledge articles individual attributes numbers tasks schedules questions runs characters strings beginning examples content situations programs parts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Huawei Xiaomi MySQL