seeusoon
seeusoon

Reputation: 11

How to make scheduled periodic task started by event in java (quarkus)?

I have the method A creating for example a new user in DB and returning user's id And after new user creation periodic task tracking his status must be started, after user's deletion this task must be stopped What java and quarkus tools can schedule periodic tasks by event and take user's id as a parameter?

I have tried using ScheduledExecutorService and @Scheduled quarkus annotation Could not find the way to make @Scheduled take parameters

Upvotes: 1

Views: 1042

Answers (2)

Martin
Martin

Reputation: 21

You can create a Scheduled Task with a start / stop parameter.

@Scheduled(every = maxLifetimeBearerToken + "s", skipExecutionIf = TestPredicate.class)

Here I created one for Bearer Token Check. If a bearer Toke is already generated, the @Scheduled skipExecutionIf will be false and the scheduler will be run. If the condition is true, it will skip.

So you have to set in your Methods to true and false. If user is deleted set myPredicate.setSkip(true);

On create user set

myPredicate.setSkip(false);

@Singleton
public class TestPredicate implements Scheduled.SkipPredicate {
    private boolean skip = true;
    public boolean isSkip() {
        return skip;
    }
    public void setSkip(boolean skip) {
        this.skip = skip;
    }
    @Override
    public boolean test(ScheduledExecution execution) {
        return skip;
    }
}

And

@Scheduled(cron ="yourCronTrigger", skipExecutionIf = TestPredicate.class)
Yourmethod(){......}

Sorry for my bad english.

Upvotes: 0

Claudio Weiler
Claudio Weiler

Reputation: 669

There is no such type of Scheduled tasks.


This design is awkward. Imagine you have 1.000 users, you will have 1.000 tasks, more users, more tasks and threads. Threads are a limited resource, so you will have problems.

What to do? Just create one common tasks, started with application (startup), and do what you need on your active users.

Upvotes: 1

Related Questions