All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.scheduler.runtime.SchedulerRecorder Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.scheduler.runtime;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import com.cronutils.model.CronType;

import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.common.runtime.ImmutableScheduledMethod;
import io.quarkus.scheduler.common.runtime.MutableScheduledMethod;
import io.quarkus.scheduler.common.runtime.ScheduledMethod;
import io.quarkus.scheduler.common.runtime.SchedulerContext;

@Recorder
public class SchedulerRecorder {

    public Supplier createContext(SchedulerConfig config,
            List scheduledMethods, boolean forceSchedulerStart, String autoImplementation) {
        // Defensive design - make an immutable copy of the scheduled method metadata
        List metadata = immutableCopy(scheduledMethods);
        return new Supplier() {
            @Override
            public Object get() {
                return new SchedulerContext() {

                    @Override
                    public CronType getCronType() {
                        return config.cronType;
                    }

                    @Override
                    public List getScheduledMethods() {
                        return metadata;
                    }

                    @Override
                    public boolean forceSchedulerStart() {
                        return forceSchedulerStart;
                    }

                    @Override
                    public List getScheduledMethods(String implementation) {
                        List ret = new ArrayList<>(metadata.size());
                        for (ScheduledMethod method : metadata) {
                            for (Scheduled scheduled : method.getSchedules()) {
                                if (matchesImplementation(scheduled, implementation)) {
                                    ret.add(method);
                                }
                            }
                        }
                        return ret;
                    }

                    @Override
                    public boolean matchesImplementation(Scheduled scheduled, String implementation) {
                        return scheduled.executeWith().equals(implementation) || ((autoImplementation.equals(implementation))
                                && scheduled.executeWith().equals(Scheduled.AUTO));
                    }

                    @Override
                    public String autoImplementation() {
                        return autoImplementation;
                    }

                };
            }
        };
    }

    private List immutableCopy(List scheduledMethods) {
        List metadata = new ArrayList<>(scheduledMethods.size());
        for (ScheduledMethod scheduledMethod : scheduledMethods) {
            metadata.add(new ImmutableScheduledMethod(scheduledMethod.getInvokerClassName(),
                    scheduledMethod.getDeclaringClassName(), scheduledMethod.getMethodName(), scheduledMethod.getSchedules()));
        }
        return List.copyOf(metadata);
    }
}