diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 25401f6c13..cd7c6e3477 100755 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -40,7 +40,6 @@ import org.keycloak.connections.jpa.util.JpaUtils; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.provider.ServerInfoAwareProviderFactory; -import org.keycloak.services.scheduled.ScheduledTaskRunner; import org.keycloak.timer.TimerProvider; /** @@ -274,7 +273,7 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide protected void startGlobalStats(KeycloakSession session, int globalStatsIntervalSecs) { logger.debugf("Started Hibernate statistics with the interval %s seconds", globalStatsIntervalSecs); TimerProvider timer = session.getProvider(TimerProvider.class); - timer.schedule(new ScheduledTaskRunner(session.getKeycloakSessionFactory(), new HibernateStatsReporter(emf)), globalStatsIntervalSecs * 1000, "ReportHibernateGlobalStats"); + timer.scheduleTask(new HibernateStatsReporter(emf), globalStatsIntervalSecs * 1000, "ReportHibernateGlobalStats"); } diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/HibernateStatsReporter.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/HibernateStatsReporter.java index 349bd40ab5..116aaaef90 100644 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/HibernateStatsReporter.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/HibernateStatsReporter.java @@ -27,7 +27,7 @@ import org.hibernate.stat.QueryStatistics; import org.hibernate.stat.Statistics; import org.jboss.logging.Logger; import org.keycloak.models.KeycloakSession; -import org.keycloak.services.scheduled.ScheduledTask; +import org.keycloak.timer.ScheduledTask; /** * @author Marek Posolda diff --git a/services/src/main/java/org/keycloak/services/scheduled/ScheduledTask.java b/server-spi/src/main/java/org/keycloak/timer/ScheduledTask.java similarity index 95% rename from services/src/main/java/org/keycloak/services/scheduled/ScheduledTask.java rename to server-spi/src/main/java/org/keycloak/timer/ScheduledTask.java index 3446765744..ba092ac695 100644 --- a/services/src/main/java/org/keycloak/services/scheduled/ScheduledTask.java +++ b/server-spi/src/main/java/org/keycloak/timer/ScheduledTask.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.keycloak.services.scheduled; +package org.keycloak.timer; import org.keycloak.models.KeycloakSession; diff --git a/server-spi/src/main/java/org/keycloak/timer/TimerProvider.java b/server-spi/src/main/java/org/keycloak/timer/TimerProvider.java index 2963a679b7..5dbf69bff9 100644 --- a/server-spi/src/main/java/org/keycloak/timer/TimerProvider.java +++ b/server-spi/src/main/java/org/keycloak/timer/TimerProvider.java @@ -24,7 +24,9 @@ import org.keycloak.provider.Provider; */ public interface TimerProvider extends Provider { - public void schedule(Runnable runnable, long interval, String taskName); + public void schedule(Runnable runnable, long intervalMillis, String taskName); + + public void scheduleTask(ScheduledTask scheduledTask, long intervalMillis, String taskName); public void cancelTask(String taskName); diff --git a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredEvents.java b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredEvents.java index 2a854ddd3e..efef0f391c 100755 --- a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredEvents.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredEvents.java @@ -20,6 +20,7 @@ package org.keycloak.services.scheduled; import org.keycloak.events.EventStoreProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; +import org.keycloak.timer.ScheduledTask; /** * @author Stian Thorgersen diff --git a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java index 06cc1ecfe4..5935eb08cb 100755 --- a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java @@ -20,6 +20,7 @@ package org.keycloak.services.scheduled; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.UserSessionProvider; +import org.keycloak.timer.ScheduledTask; /** * @author Stian Thorgersen diff --git a/services/src/main/java/org/keycloak/services/scheduled/ClusterAwareScheduledTaskRunner.java b/services/src/main/java/org/keycloak/services/scheduled/ClusterAwareScheduledTaskRunner.java index 7f60891e33..94db9c971e 100644 --- a/services/src/main/java/org/keycloak/services/scheduled/ClusterAwareScheduledTaskRunner.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ClusterAwareScheduledTaskRunner.java @@ -23,6 +23,7 @@ import org.keycloak.cluster.ClusterProvider; import org.keycloak.cluster.ExecutionResult; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.timer.ScheduledTask; /** * Ensures that there are not concurrent executions of same task (either on this host or any other cluster host) diff --git a/services/src/main/java/org/keycloak/services/scheduled/ScheduledTaskRunner.java b/services/src/main/java/org/keycloak/services/scheduled/ScheduledTaskRunner.java index 33dc91aebd..b49300f8a1 100644 --- a/services/src/main/java/org/keycloak/services/scheduled/ScheduledTaskRunner.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ScheduledTaskRunner.java @@ -17,13 +17,10 @@ package org.keycloak.services.scheduled; -import java.util.concurrent.Callable; - -import org.keycloak.cluster.ClusterProvider; -import org.keycloak.cluster.ExecutionResult; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.services.ServicesLogger; +import org.keycloak.timer.ScheduledTask; /** * @author Stian Thorgersen diff --git a/services/src/main/java/org/keycloak/timer/basic/BasicTimerProvider.java b/services/src/main/java/org/keycloak/timer/basic/BasicTimerProvider.java index 5a58c88855..29a736f47d 100644 --- a/services/src/main/java/org/keycloak/timer/basic/BasicTimerProvider.java +++ b/services/src/main/java/org/keycloak/timer/basic/BasicTimerProvider.java @@ -18,6 +18,9 @@ package org.keycloak.timer.basic; import org.jboss.logging.Logger; +import org.keycloak.models.KeycloakSession; +import org.keycloak.services.scheduled.ScheduledTaskRunner; +import org.keycloak.timer.ScheduledTask; import org.keycloak.timer.TimerProvider; import java.util.Timer; @@ -30,16 +33,18 @@ public class BasicTimerProvider implements TimerProvider { private static final Logger logger = Logger.getLogger(BasicTimerProvider.class); + private final KeycloakSession session; private final Timer timer; private final BasicTimerProviderFactory factory; - public BasicTimerProvider(Timer timer, BasicTimerProviderFactory factory) { + public BasicTimerProvider(KeycloakSession session, Timer timer, BasicTimerProviderFactory factory) { + this.session = session; this.timer = timer; this.factory = factory; } @Override - public void schedule(final Runnable runnable, final long interval, String taskName) { + public void schedule(final Runnable runnable, final long intervalMillis, String taskName) { TimerTask task = new TimerTask() { @Override public void run() { @@ -53,8 +58,14 @@ public class BasicTimerProvider implements TimerProvider { existingTask.cancel(); } - logger.debugf("Starting task '%s' with interval '%d'", taskName, interval); - timer.schedule(task, interval, interval); + logger.debugf("Starting task '%s' with interval '%d'", taskName, intervalMillis); + timer.schedule(task, intervalMillis, intervalMillis); + } + + @Override + public void scheduleTask(ScheduledTask scheduledTask, long intervalMillis, String taskName) { + ScheduledTaskRunner scheduledTaskRunner = new ScheduledTaskRunner(session.getKeycloakSessionFactory(), scheduledTask); + this.schedule(scheduledTaskRunner, intervalMillis, taskName); } @Override diff --git a/services/src/main/java/org/keycloak/timer/basic/BasicTimerProviderFactory.java b/services/src/main/java/org/keycloak/timer/basic/BasicTimerProviderFactory.java index 335ecb01cd..ea0da94b22 100755 --- a/services/src/main/java/org/keycloak/timer/basic/BasicTimerProviderFactory.java +++ b/services/src/main/java/org/keycloak/timer/basic/BasicTimerProviderFactory.java @@ -39,7 +39,7 @@ public class BasicTimerProviderFactory implements TimerProviderFactory { @Override public TimerProvider create(KeycloakSession session) { - return new BasicTimerProvider(timer, this); + return new BasicTimerProvider(session, timer, this); } @Override