Roy Golan has uploaded a new change for review. Change subject: core: Inject Task schedulers instead of JNDI lookups ......................................................................
core: Inject Task schedulers instead of JNDI lookups continue the JNDI and EJB cleanup and move Singleton like task scheduling to CDI. to inject a scheduler, use its qualifier - @InMemory or @Persistent. <code> @Inject @InMemory SchedulUtil taskScheduler; // will inject a SchedulerUtilBaseImpl </code> - Qualifiers @InMemory and @Persistent are replacements for string-style jndi lookups. - getInstance() exist for legacy code. Prefer Injection over it. - all scheulers are loaded first after the DB connection established. See Backend.initialize() Change-Id: I2288d0bd0584c7ba17f3dabaff268e40cbdddbea Signed-off-by: Roy Golan <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/Backend.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/BackwardCompatibilityTaskCreationTest.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/QosRangeValidatorTest.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/InMemory.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/Persistent.java M backend/manager/modules/scheduler/pom.xml M backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/DBSchedulerUtilQuartzImpl.java M backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtil.java M backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtilQuartzImpl.java A backend/manager/modules/scheduler/src/main/resources/META-INF/beans.xml M ear/pom.xml 12 files changed, 114 insertions(+), 87 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/35/41035/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/Backend.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/Backend.java index 73805dd..066408d 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/Backend.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/Backend.java @@ -14,6 +14,8 @@ import javax.ejb.Startup; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; import javax.inject.Inject; import javax.interceptor.ExcludeClassInterceptors; import javax.interceptor.Interceptors; @@ -73,6 +75,7 @@ import org.ovirt.engine.core.utils.OsRepositoryImpl; import org.ovirt.engine.core.utils.extensionsmgr.EngineExtensionsManager; import org.ovirt.engine.core.utils.osinfo.OsInfoPreferencesLoader; +import org.ovirt.engine.core.utils.timer.SchedulerUtil; import org.ovirt.engine.core.utils.timer.SchedulerUtilQuartzImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,7 +87,7 @@ // to all the business and timeout methods in the singleton. // The developer of the singleton is responsible for ensuring that the state // of the singleton is synchronized across all clients. -@DependsOn("Scheduler") +@DependsOn("LockManager") @Local({ BackendLocal.class, BackendInternal.class, BackendCommandObjectsHandler.class }) @Interceptors({ CorrelationIdTrackerInterceptor.class }) @Singleton @@ -100,9 +103,12 @@ private DateTime _startedAt; private static boolean firstInitialization = true; private String poolMonitoringJobId; - @Inject Injector injector; + @Inject + Injector injector; @Inject private DbFacade dbFacade; + @Inject @Any + private Instance<SchedulerUtil> taskSchedulers; public static BackendInternal getInstance() { return Injector.get(BackendInternal.class); @@ -133,7 +139,7 @@ checkDBConnectivity(); try { initialize(); - } catch(Exception ex) { + } catch (Exception ex) { log.error("Error during initialization", ex); throw ex; } @@ -184,6 +190,10 @@ @Override public void initialize() { log.info("Start initializing {}", getClass().getSimpleName()); + // start task schedulers + for (SchedulerUtil taskScheduler : taskSchedulers) { + log.info("Started task scheduler {}", taskScheduler); + } // initialize configuration utils to use DB Config.setConfigUtils(new DBConfigUtils()); // we need to initialize os-info before the compensations take place because of VmPoolCommandBase#osRepository @@ -241,29 +251,30 @@ initExecutionMessageDirector(); SchedulerUtilQuartzImpl.getInstance().scheduleAFixedDelayJob(SessionDataContainer.getInstance(), - "cleanExpiredUsersSessions", new Class[] {}, new Object[] {}, - 1, - 1, TimeUnit.MINUTES); + "cleanExpiredUsersSessions", new Class[] {}, new Object[] {}, + 1, + 1, TimeUnit.MINUTES); // Set start-up time _startedAt = DateTime.getNow(); - int vmPoolMonitorIntervalInMinutes = Config.<Integer> getValue(ConfigValues.VmPoolMonitorIntervalInMinutes); + int vmPoolMonitorIntervalInMinutes = Config.<Integer>getValue(ConfigValues.VmPoolMonitorIntervalInMinutes); poolMonitoringJobId = SchedulerUtilQuartzImpl.getInstance().scheduleAFixedDelayJob(new VmPoolMonitor(), "managePrestartedVmsInAllVmPools", new Class[] {}, new Object[] {}, vmPoolMonitorIntervalInMinutes, vmPoolMonitorIntervalInMinutes, TimeUnit.MINUTES); - int autoStartVmsRunnerIntervalInSeconds = Config.<Integer> getValue(ConfigValues.AutoStartVmsRunnerIntervalInSeconds); + int autoStartVmsRunnerIntervalInSeconds = + Config.<Integer>getValue(ConfigValues.AutoStartVmsRunnerIntervalInSeconds); SchedulerUtilQuartzImpl.getInstance().scheduleAFixedDelayJob(AutoStartVmsRunner.getInstance(), "startFailedAutoStartVms", new Class[] {}, new Object[] {}, autoStartVmsRunnerIntervalInSeconds, autoStartVmsRunnerIntervalInSeconds, TimeUnit.SECONDS); - int quotaCacheIntervalInMinutes = Config.<Integer> getValue(ConfigValues.QuotaCacheIntervalInMinutes); + int quotaCacheIntervalInMinutes = Config.<Integer>getValue(ConfigValues.QuotaCacheIntervalInMinutes); SchedulerUtilQuartzImpl.getInstance().scheduleAFixedDelayJob(QuotaManager.getInstance(), - "updateQuotaCache", new Class[]{}, new Object[]{}, + "updateQuotaCache", new Class[] {}, new Object[] {}, 1, quotaCacheIntervalInMinutes, TimeUnit.MINUTES); //initializes attestation initAttestation(); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java index c2c900c..7557c76 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java @@ -38,6 +38,7 @@ import org.ovirt.engine.core.common.errors.VdcFault; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.di.Injector; import org.ovirt.engine.core.utils.timer.OnTimerMethodAnnotation; import org.ovirt.engine.core.utils.timer.SchedulerUtil; import org.ovirt.engine.core.utils.timer.SchedulerUtilQuartzImpl; @@ -55,7 +56,7 @@ CommandExecutor(CommandCoordinatorImpl coco) { this.coco = coco; - SchedulerUtil scheduler = SchedulerUtilQuartzImpl.getInstance(); + SchedulerUtil scheduler = Injector.get(SchedulerUtilQuartzImpl.class); scheduler.scheduleAFixedDelayJob(this, "invokeCallbackMethods", new Class[]{}, new Object[]{}, Config.<Integer>getValue(ConfigValues.AsyncCommandPollingRateInSeconds), Config.<Integer>getValue(ConfigValues.AsyncCommandPollingRateInSeconds), TimeUnit.SECONDS); diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/BackwardCompatibilityTaskCreationTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/BackwardCompatibilityTaskCreationTest.java index 04fce5a..4416d86 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/BackwardCompatibilityTaskCreationTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/BackwardCompatibilityTaskCreationTest.java @@ -53,6 +53,7 @@ import org.ovirt.engine.core.utils.RandomUtilsSeedingRule; import org.ovirt.engine.core.utils.ejb.BeanType; import org.ovirt.engine.core.utils.timer.SchedulerUtil; +import org.ovirt.engine.core.utils.timer.SchedulerUtilQuartzImpl; /** * A test for task creation in the various commands. @@ -75,6 +76,8 @@ @ClassRule public static MockEJBStrategyRule ejbRule = new MockEJBStrategyRule(BeanType.SCHEDULER, mock(SchedulerUtil.class)); + @ClassRule + public static InjectorRule injectorRule = new InjectorRule(); @Before public void before() { @@ -84,6 +87,7 @@ CommandEntityDao cmdEntityDao = mock(CommandEntityDao.class); when(DbFacade.getInstance().getCommandEntityDao()).thenReturn(cmdEntityDao); when(cmdEntityDao.getAll()).thenReturn(Collections.<CommandEntity> emptyList()); + injectorRule.bind(SchedulerUtilQuartzImpl.class, mock(SchedulerUtilQuartzImpl.class)); } @SuppressWarnings({ "unchecked", "rawtypes"}) diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/QosRangeValidatorTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/QosRangeValidatorTest.java index ffb8f08..021ed9c 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/QosRangeValidatorTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/QosRangeValidatorTest.java @@ -16,6 +16,7 @@ import javax.validation.groups.Default; import java.util.Arrays; import java.util.Collections; +import java.util.Date; import java.util.List; import static org.junit.Assert.assertTrue; @@ -95,4 +96,9 @@ assertCanDoMsgCount(canDoMsgs, VdcBllMessages.ACTION_TYPE_FAILED_QOS_OUT_OF_RANGE_VALUES.name(), 6); } + @Test + public void da() { + System.out.println(new Date().getTime()); + System.out.println(System.currentTimeMillis()); + } } diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/InMemory.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/InMemory.java new file mode 100644 index 0000000..1d3b725 --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/InMemory.java @@ -0,0 +1,7 @@ +package org.ovirt.engine.core.di.qualifier; + +import javax.inject.Qualifier; + +@Qualifier +public @interface InMemory { +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/Persistent.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/Persistent.java new file mode 100644 index 0000000..0b43e2f --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/di/qualifier/Persistent.java @@ -0,0 +1,7 @@ +package org.ovirt.engine.core.di.qualifier; + +import javax.inject.Qualifier; + +@Qualifier +public @interface Persistent { +} diff --git a/backend/manager/modules/scheduler/pom.xml b/backend/manager/modules/scheduler/pom.xml index 651cbcf..c7f65f4 100644 --- a/backend/manager/modules/scheduler/pom.xml +++ b/backend/manager/modules/scheduler/pom.xml @@ -8,7 +8,7 @@ </parent> <artifactId>scheduler</artifactId> - <packaging>ejb</packaging> + <packaging>jar</packaging> <name>engine scheduler bean</name> <properties> diff --git a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/DBSchedulerUtilQuartzImpl.java b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/DBSchedulerUtilQuartzImpl.java index f950178..010046a 100644 --- a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/DBSchedulerUtilQuartzImpl.java +++ b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/DBSchedulerUtilQuartzImpl.java @@ -1,28 +1,8 @@ package org.ovirt.engine.core.utils.timer; -import static org.quartz.JobBuilder.newJob; -import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals; - -import java.io.IOException; -import java.util.Date; -import java.util.Properties; -import java.util.concurrent.TimeUnit; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.ejb.ConcurrencyManagement; -import javax.ejb.ConcurrencyManagementType; -import javax.ejb.DependsOn; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.ejb.TransactionAttribute; -import javax.ejb.TransactionAttributeType; - import org.apache.commons.lang.ClassUtils; +import org.ovirt.engine.core.di.qualifier.Persistent; import org.ovirt.engine.core.utils.ResourceUtils; -import org.ovirt.engine.core.utils.ejb.BeanProxyType; -import org.ovirt.engine.core.utils.ejb.BeanType; -import org.ovirt.engine.core.utils.ejb.EjbUtils; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.Scheduler; @@ -30,12 +10,21 @@ import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; -@Singleton(name = "PersistentScheduler") -@DependsOn("LockManager") -@Startup -@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) -@ConcurrencyManagement(ConcurrencyManagementType.BEAN) +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.inject.Singleton; +import java.io.IOException; +import java.util.Date; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +import static org.quartz.JobBuilder.newJob; +import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals; + +@Singleton @Persistent public class DBSchedulerUtilQuartzImpl extends SchedulerUtilBaseImpl implements SchedulerUtil { + + private static SchedulerUtil instance; @Override @PostConstruct @@ -47,6 +36,7 @@ * retrieving the quartz scheduler from the factory. */ public void setup() { + instance = this; final String QUARTZ_DB_PROPERTIES = "ovirt-db-scheduler.properties"; Properties props = null; try { @@ -84,12 +74,20 @@ } /** - * Returns the single instance of this Class. - * - * @return a SchedulerUtil instance + * @deprecated prefer injecting with + * <pre> + * {@code @Inject @Persistent }<br> + * {@code SchedulerUtil taskScheduler; } + * </pre> + * or fetching one using {@linkplain org.ovirt.engine.di.Injector} + * <pre> + * {@code Injector.get(DBSchedulerUtilQuartzImpl.class) } + * </pre> + * @return a {@code DBSchedulerUtilQuartzImpl} instance */ + @Deprecated public static SchedulerUtil getInstance() { - return EjbUtils.findBean(BeanType.PERSISTENT_SCHEDULER, BeanProxyType.LOCAL); + return instance; } /** diff --git a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtil.java b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtil.java index 30d0ef7..d78561f 100644 --- a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtil.java +++ b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtil.java @@ -1,14 +1,11 @@ package org.ovirt.engine.core.utils.timer; -import java.util.Date; -import java.util.concurrent.TimeUnit; - -import javax.ejb.Local; - import org.quartz.Scheduler; import org.quartz.Trigger; -@Local +import java.util.Date; +import java.util.concurrent.TimeUnit; + public interface SchedulerUtil { /** diff --git a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtilQuartzImpl.java b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtilQuartzImpl.java index 95619ac..0d8b1f7 100644 --- a/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtilQuartzImpl.java +++ b/backend/manager/modules/scheduler/src/main/java/org/ovirt/engine/core/utils/timer/SchedulerUtilQuartzImpl.java @@ -1,21 +1,6 @@ package org.ovirt.engine.core.utils.timer; -import static org.quartz.JobBuilder.newJob; -import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import javax.ejb.ConcurrencyManagement; -import javax.ejb.ConcurrencyManagementType; -import javax.ejb.DependsOn; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.ejb.TransactionAttribute; -import javax.ejb.TransactionAttributeType; - -import org.ovirt.engine.core.utils.ejb.BeanProxyType; -import org.ovirt.engine.core.utils.ejb.BeanType; -import org.ovirt.engine.core.utils.ejb.EjbUtils; +import org.ovirt.engine.core.di.qualifier.InMemory; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.Scheduler; @@ -23,25 +8,25 @@ import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; -// Here we use a Singleton bean, names Scheduler. -// The @Startup annotation is to make sure the bean is initialized on startup. -// @ConcurrencyManagement - we use bean managed concurrency: -// Singletons that use bean-managed concurrency allow full concurrent access to all the -// business and timeout methods in the singleton. -// The developer of the singleton is responsible for ensuring that the state of the singleton is synchronized across all clients. -@Singleton(name = "Scheduler") -@DependsOn("LockManager") -@Startup -@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) -@ConcurrencyManagement(ConcurrencyManagementType.BEAN) +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.inject.Singleton; + +import static org.quartz.JobBuilder.newJob; +import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals; + +@Singleton @InMemory public class SchedulerUtilQuartzImpl extends SchedulerUtilBaseImpl { + // for backward compatibility. + private static SchedulerUtil instance; + /** * This method is called upon the bean creation as part * of the management Service bean lifecycle. */ @Override @PostConstruct - public void create(){ + public void create() { setup(); } @@ -49,11 +34,13 @@ * retrieving the quartz scheduler from the factory. */ public void setup() { + instance = this; try { SchedulerFactory sf = new StdSchedulerFactory(); sched = sf.getScheduler(); sched.start(); - sched.getListenerManager().addJobListener(new FixedDelayJobListener(this), jobGroupEquals(Scheduler.DEFAULT_GROUP)); + sched.getListenerManager() + .addJobListener(new FixedDelayJobListener(this), jobGroupEquals(Scheduler.DEFAULT_GROUP)); } catch (SchedulerException se) { log.error("there is a problem with the underlying Scheduler: {}", se.getMessage()); log.debug("Exception", se); @@ -73,12 +60,20 @@ } /** - * Returns the single instance of this Class. - * - * @return a SchedulerUtil instance + * @deprecated prefer injecting with + * <pre> + * {@code @Inject @InMemory }<br> + * {@code SchedulerUtil taskScheduler; } + * </pre> + * or fetching one using {@linkplain org.ovirt.engine.di.Injector} + * <pre> + * {@code Injector.get(SchedulerUtilQuartzImpl.class) } + * </pre> + * @return a {@code SchedulerUtilQuartzImpl} instance */ + @Deprecated public static SchedulerUtil getInstance() { - return EjbUtils.findBean(BeanType.SCHEDULER, BeanProxyType.LOCAL); + return instance; } @Override diff --git a/backend/manager/modules/scheduler/src/main/resources/META-INF/beans.xml b/backend/manager/modules/scheduler/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/backend/manager/modules/scheduler/src/main/resources/META-INF/beans.xml diff --git a/ear/pom.xml b/ear/pom.xml index 8bdb747..28f8019 100644 --- a/ear/pom.xml +++ b/ear/pom.xml @@ -22,6 +22,12 @@ <!-- ** JARs --> <dependency> <groupId>org.ovirt.engine.core</groupId> + <artifactId>scheduler</artifactId> + <version>${engine.version}</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.ovirt.engine.core</groupId> <artifactId>vdsbroker</artifactId> <version>${engine.version}</version> <type>jar</type> @@ -227,12 +233,6 @@ <!-- ** EJB-JARs --> <ejbModule> <groupId>org.ovirt.engine.core</groupId> - <artifactId>scheduler</artifactId> - <bundleFileName>scheduler.jar</bundleFileName> - </ejbModule> - - <ejbModule> - <groupId>org.ovirt.engine.core</groupId> <artifactId>bll</artifactId> <bundleFileName>bll.jar</bundleFileName> </ejbModule> @@ -256,6 +256,7 @@ *.war/**, META-INF/**, lib/vdsbroker.jar/**, + lib/scheduler.jar/**, </packagingIncludes> </configuration> -- To view, visit https://gerrit.ovirt.org/41035 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2288d0bd0584c7ba17f3dabaff268e40cbdddbea Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Roy Golan <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
