Skip to content

[Use Case]: Automatic start of Scheduled methods and more concise usage of @Schedule #624

@njr-11

Description

@njr-11

As a ...

  • Application user/user of the configuration itself
  • API user (application developer)
  • SPI user (container or runtime developer)
  • Specification implementer

I need to be able to ...

arrange for an Asynchronous method to be automatically submitted/scheduled after a configurable delay upon application startup

Which enables me to ...

more conveniently code applications that have business logic that should run on a schedule

Additional information

This requirement was discussed in a topic titled "Defining Jakarta EE 12 Scope in Program Plan" on the jakarta.ee-community mailing list.

It is already possible to have an Asynchronous method immediately start running on a schedule with code like this:

 @Asynchronous(runAt = @Schedule(daysOfWeek = DayOfWeek.TUESDAY, hours = 1))
 public void weeklyTask(@Observes Startup event) {
     ...
 }

There has also been some interest in being able to impose a delay before the Asynchronous method will automatically start running. One way that this can already by achieved is with 2 separate methods,

@Inject
ManagedScheduledExecutorService executor;

public void autoStart(@Observes Startup event) {
    executor.schedule(this::every10Minutes, 1, TimeUnit.HOURS);
}

@Asynchronous(runAt = @Schedule(cron = "*/10 * * * *"))
public void every10Minutes() {
    ...
}

If the above usage is good enough, we could document similar examples in the spec and add some TCK tests.

The following is updated after recent discussion in this issue:

Otherwise, if the above is not considered concise enough, a different approach could be expanding usage of the existing @Schedule to allow it to also be used on methods, where we would define its behavior to mean automatic schedule,

 @Schedule(cron = "*/10 * * * *")
 public void every10Minutes() {
     ...
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions