Apache DeltaSpikeã«ã¯ãScheduler Moduleã¨ãããã®ãããããã¡ãã使ã£ã¦å®æçã«åããã¸ã§ããä½æãããã¨ãã§ãã¾ãã
ã¸ã§ãã®ã¹ã±ã¸ã¥ã¼ã«ã¯CRONå½¢å¼ã§æå®ã§ããå®è£ ã¨ãã¦ã¯Quartzã使ã£ã¦ããããã§ãã
Quartz Enterprise Job Scheduler
ãã¡ããå©ç¨ããã¨ãEJBã®TimerServiceã使ããªãã¦ãCDIã§ã¸ã§ãèµ·åãã§ãããã¨ã
â»EJB TimerServiceã®Persistenceã¿ãããªã®ã¯ãªãã¿ããã§ãã
ã§ã¯ã試ãã¦ã¿ã¾ãããã
æºå
ã¾ãã¯ããã«ãå®ç¾©ããã
build.sbt
name := "cdi-deltaspike-scheduler" organization := "org.littlewings" scalaVersion := "2.11.8" scalacOptions ++= Seq("-Xlint", "-deprecation", "-unchecked", "-feature") updateOptions := updateOptions.value.withCachedResolution(true) enablePlugins(JettyPlugin) webappWebInfClasses := true artifactName := { (scalaVersion: ScalaVersion, module: ModuleID, artifact: Artifact) => // artifact.name + "." + artifact.extension "ROOT." + artifact.extension } fork in Test := true libraryDependencies ++= Seq( "javax" % "javaee-web-api" % "7.0" % Provided, "org.apache.deltaspike.core" % "deltaspike-core-api" % "1.7.1" % Compile, "org.apache.deltaspike.core" % "deltaspike-core-impl" % "1.7.1" % Runtime, "org.apache.deltaspike.modules" % "deltaspike-scheduler-module-api" % "1.7.1" % Compile, "org.apache.deltaspike.modules" % "deltaspike-scheduler-module-impl" % "1.7.1" % Runtime, "org.apache.deltaspike.cdictrl" % "deltaspike-cdictrl-api" % "1.7.1" % Compile, "org.apache.deltaspike.cdictrl" % "deltaspike-cdictrl-weld" % "1.7.1" % Runtime, "org.quartz-scheduler" % "quartz" % "2.2.1" % Compile )
Webããã¸ã§ã¯ãã¨ãã¦ä½æãã¾ãã
project/plugins.sbt
logLevel := Level.Warn addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "2.1.0")
ä¾åé¢ä¿ã¨ãã¦ã¯ãApache DeltaSpikeã®Coreã¢ã¸ã¥ã¼ã«ã
"org.apache.deltaspike.core" % "deltaspike-core-api" % "1.7.1" % Compile, "org.apache.deltaspike.core" % "deltaspike-core-impl" % "1.7.1" % Runtime,
Schedulerã¢ã¸ã¥ã¼ã«ã
"org.apache.deltaspike.modules" % "deltaspike-scheduler-module-api" % "1.7.1" % Compile, "org.apache.deltaspike.modules" % "deltaspike-scheduler-module-impl" % "1.7.1" % Runtime,
ããã¦ãContainer-Controlã¢ã¸ã¥ã¼ã«ãå¿ è¦ã§ãã
"org.apache.deltaspike.cdictrl" % "deltaspike-cdictrl-api" % "1.7.1" % Compile, "org.apache.deltaspike.cdictrl" % "deltaspike-cdictrl-weld" % "1.7.1" % Runtime,
Quartzã«ã¤ãã¦ã¯ãããã©ã«ãã®ã¹ã±ã¸ã¥ã¼ã©ã¼ã¨ãã¦Apache DeltaSpikeã®Schedule Moduleãæ¡ç¨ãã¦ããã®ã§ãããQuartzèªä½ã¸ã®ä¾åé¢ä¿ã¯æ示çã«è¨è¿°ããå¿ è¦ãããã¾ãã
"org.quartz-scheduler" % "quartz" % "2.2.1" % Compile
ã¢ããªã±ã¼ã·ã§ã³ã®å®è¡ã¯ãWildFlyã¸ãããã¤ãã¦ç¢ºèªãããã®ã¨ãã¾ããã¨ãããããWildFlyãèµ·åãã¦ããã¾ãããã
$ bin/standalone.sh
Jobãä½æãã
Apache DeltaSpikeã®Schedule Moduleã§Jobãä½æããæ¹æ³ã¯ã以ä¸ã®2ã¤ãããã¾ãã
- org.quartz.Jobã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããã¯ã©ã¹ãä½æãã
- java.lang.Runnableã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããã¯ã©ã¹ãä½æãã
ãããã«ãããQuartzã®ä¸ã§åä½ãã¾ãã
@Scheduled with org.quartz.Job or java.lang.Runnable
Jobã®èµ·åã¿ã¤ãã³ã°ã«ã¤ãã¦ã¯ãCRONå½¢å¼ã§è¨è¿°ãã¾ãã
Configurable CRON expressions
ããã§ã¯ãã¾ãã¯Quartzã®Jobã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ããæ¹æ³ã§å®è£
ãã¦ã¿ã¾ãããã
src/main/scala/org/littlewings/javaee7/cdi/QuartzBasedJob.scala
package org.littlewings.javaee7.cdi import javax.enterprise.context.ApplicationScoped import javax.inject.Inject import org.apache.deltaspike.scheduler.api.Scheduled import org.quartz.{Job, JobExecutionContext} import org.slf4j.{Logger, LoggerFactory} @Scheduled(cronExpression = "0 0/1 * * * ?") @ApplicationScoped class QuartzBasedJob extends Job { @Inject var applicationScopedMessageService: ApplicationScopedMessageService = _ @Inject var sessionScopedMessageService: SessionScopedMessageService = _ @Inject var requestScopedMessageService: RequestScopedMessageService = _ @Inject var pseudoScopedMessageService: PseudoScopedMessageService = _ val logger: Logger = LoggerFactory.getLogger(getClass) override def execute(context: JobExecutionContext): Unit = { logger.info("[{}] startup job", getClass.getSimpleName) applicationScopedMessageService.loggingMessage() sessionScopedMessageService.loggingMessage() requestScopedMessageService.loggingMessage() pseudoScopedMessageService.loggingMessage() logger.info("[{}] end job", getClass.getSimpleName) } }
@Scheduleã¢ããã¼ã·ã§ã³ããã³cronExpressionã§ãèµ·åã¿ã¤ãã³ã°ãæå®ãã¾ããã¾ããCDI管çBeanã¨ãã¦å®£è¨ãã¦ããã¾ããã¯ã©ã¹èªä½ã¯ãQuartzã®Jobã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¦ä½æãã¾ãã
@Scheduled(cronExpression = "0 0/1 * * * ?") @ApplicationScoped class QuartzBasedJob extends Job {
ä»åã¯ã1åæ¯ã«èµ·åããJobãä½æãã¾ããã
ãã¨ã¯ãJob#executeã¡ã½ãããå®è£ ããã°OKã§ãã
override def execute(context: JobExecutionContext): Unit = { logger.info("[{}] startup job", getClass.getSimpleName) applicationScopedMessageService.loggingMessage() sessionScopedMessageService.loggingMessage() requestScopedMessageService.loggingMessage() pseudoScopedMessageService.loggingMessage() logger.info("[{}] end job", getClass.getSimpleName) }
ã¨æ¸ãã¨ãQuartzã®Jobãä½æããã®ã¨ä½ãéãï¼ã¨ãã話ã«ãªãã¾ããããã®Jobã¸ã¯@Injectã§CDI管çBeanãã¤ã³ã¸ã§ã¯ã·ã§ã³ãããã¨ãã§ãã¾ãã
@Inject var applicationScopedMessageService: ApplicationScopedMessageService = _ @Inject var sessionScopedMessageService: SessionScopedMessageService = _ @Inject var requestScopedMessageService: RequestScopedMessageService = _ @Inject var pseudoScopedMessageService: PseudoScopedMessageService = _
ããã§å©ç¨ãã¦ããCDI管çBeanã¯ãå称ããèªæããããã¾ããããåã¹ã³ã¼ãã«å¿ããCDI管çBeanã§ãã
src/main/scala/org/littlewings/javaee7/cdi/MessageService.scala
package org.littlewings.javaee7.cdi import java.time.LocalDateTime import javax.enterprise.context.{ApplicationScoped, Dependent, RequestScoped, SessionScoped} import org.slf4j.{Logger, LoggerFactory} trait MessageServiceSupport { val logger: Logger = LoggerFactory.getLogger(getClass) def loggingMessage(): Unit = { logger.info(s"Hello ${getClass.getSimpleName}@${hashCode}, now = ${LocalDateTime.now}") } } @ApplicationScoped class ApplicationScopedMessageService extends MessageServiceSupport @SessionScoped @SerialVersionUID(1L) class SessionScopedMessageService extends MessageServiceSupport with Serializable @RequestScoped class RequestScopedMessageService extends MessageServiceSupport @Dependent class PseudoScopedMessageService extends MessageServiceSupport
ä½æ°ã«ãSessionScopedãRequestScopedãªCDI管çBeanãæ··ãã£ã¦ãã¾ãããããåãã®ã§ããããâ¦ï¼
ã¨ãããããããã±ã¼ã¸ã³ã°ãã¦
> package
ãããã¤ã
$ cp target/scala-2.11/ROOT.war /path/to/wildfly-10.0.0.Final/standalone/deployments
ããã¨ã1åããã«ã¸ã§ããå®è¡ããã¾ãã
01:05:00,041 INFO [org.littlewings.javaee7.cdi.QuartzBasedJob] (DefaultQuartzScheduler_Worker-1) [QuartzBasedJob] startup job 01:05:00,130 INFO [org.littlewings.javaee7.cdi.ApplicationScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello ApplicationScopedMessageService@1340878689, now = 2016-08-12T01:05:00.127 01:05:00,131 INFO [org.littlewings.javaee7.cdi.SessionScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello SessionScopedMessageService@1552885928, now = 2016-08-12T01:05:00.131 01:05:00,131 INFO [org.littlewings.javaee7.cdi.RequestScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello RequestScopedMessageService@1988410949, now = 2016-08-12T01:05:00.131 01:05:00,131 INFO [org.littlewings.javaee7.cdi.PseudoScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello PseudoScopedMessageService@157109538, now = 2016-08-12T01:05:00.131 01:05:00,131 INFO [org.littlewings.javaee7.cdi.QuartzBasedJob] (DefaultQuartzScheduler_Worker-1) [QuartzBasedJob] end job 01:06:00,001 INFO [org.littlewings.javaee7.cdi.QuartzBasedJob] (DefaultQuartzScheduler_Worker-3) [QuartzBasedJob] startup job 01:06:00,004 INFO [org.littlewings.javaee7.cdi.ApplicationScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello ApplicationScopedMessageService@1340878689, now = 2016-08-12T01:06:00.004 01:06:00,005 INFO [org.littlewings.javaee7.cdi.SessionScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello SessionScopedMessageService@1186961072, now = 2016-08-12T01:06:00.005 01:06:00,005 INFO [org.littlewings.javaee7.cdi.RequestScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello RequestScopedMessageService@1215852567, now = 2016-08-12T01:06:00.005 01:06:00,005 INFO [org.littlewings.javaee7.cdi.PseudoScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello PseudoScopedMessageService@157109538, now = 2016-08-12T01:06:00.005 01:06:00,006 INFO [org.littlewings.javaee7.cdi.QuartzBasedJob] (DefaultQuartzScheduler_Worker-3) [QuartzBasedJob] end job
ä¸ç·ã«ããã·ã¥ã³ã¼ããåºåãã¦ãã¾ãããããããè¦ãã¨SessionScopedãRequestScopedãªCDI管çBeanã¯ãé½åº¦ã¤ã³ã¹ã¿ã³ã¹ãä½æããã¦ã¤ã³ã¸ã§ã¯ã·ã§ã³ããã¦ããããã§ããã
In such scheduled-tasks CDI based dependency-injection is enabled. Furthermore, the request- and session-scope get started (and stopped) per job-execution. Therefore, the container-control module (of DeltaSpike) is required. That can be controlled via @Scheduled#startScopes (possible values: all scopes supported by the container-control module as well as {} for 'no scopes').
https://deltaspike.apache.org/documentation/scheduler.html#@Scheduledwithorg.quartz.Joborjava.lang.Runnable
ã¨ãããããæä½éã®åä½ã¯ç¢ºèªã§ãã¾ããã
ã§ã¯ãç¶ãã¦Runnableã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ããã¯ã©ã¹ã§ãJobãä½æãã¦ã¿ã¾ãããã
src/main/scala/org/littlewings/javaee7/cdi/RunnableBasedJob.scala
package org.littlewings.javaee7.cdi import javax.enterprise.context.ApplicationScoped import javax.inject.Inject import org.apache.deltaspike.scheduler.api.Scheduled import org.slf4j.{Logger, LoggerFactory} @Scheduled(cronExpression = "0 0/1 * * * ?") @ApplicationScoped class RunnableBasedJob extends Runnable { @Inject var applicationScopedMessageService: ApplicationScopedMessageService = _ @Inject var sessionScopedMessageService: SessionScopedMessageService = _ @Inject var requestScopedMessageService: RequestScopedMessageService = _ @Inject var pseudoScopedMessageService: PseudoScopedMessageService = _ val logger: Logger = LoggerFactory.getLogger(getClass) override def run(): Unit = { logger.info("[{}] startup job", getClass.getSimpleName) applicationScopedMessageService.loggingMessage() sessionScopedMessageService.loggingMessage() requestScopedMessageService.loggingMessage() pseudoScopedMessageService.loggingMessage() logger.info("[{}] end job", getClass.getSimpleName) } }
Quartzã®Jobã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããå ´åã¨ããã£ã¦ãããã¨ã¯ã»ã¨ãã©åãã§ããå®è£ ãã¦ããã¡ã½ããããRunnable#runãªãããã§ããã
ãããã¤ãã¦åãããæã®ãã°ã¯ããã®ããã«ã
01:08:00,445 INFO [org.littlewings.javaee7.cdi.RunnableBasedJob] (DefaultQuartzScheduler_Worker-1) [RunnableBasedJob] startup job 01:08:00,552 INFO [org.littlewings.javaee7.cdi.ApplicationScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello ApplicationScopedMessageService@1897461758, now = 2016-08-12T01:08:00.550 01:08:00,552 INFO [org.littlewings.javaee7.cdi.SessionScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello SessionScopedMessageService@1805686172, now = 2016-08-12T01:08:00.552 01:08:00,553 INFO [org.littlewings.javaee7.cdi.RequestScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello RequestScopedMessageService@1407251698, now = 2016-08-12T01:08:00.553 01:08:00,553 INFO [org.littlewings.javaee7.cdi.PseudoScopedMessageService] (DefaultQuartzScheduler_Worker-1) Hello PseudoScopedMessageService@1527663713, now = 2016-08-12T01:08:00.553 01:08:00,553 INFO [org.littlewings.javaee7.cdi.RunnableBasedJob] (DefaultQuartzScheduler_Worker-1) [RunnableBasedJob] end job 01:09:00,002 INFO [org.littlewings.javaee7.cdi.RunnableBasedJob] (DefaultQuartzScheduler_Worker-3) [RunnableBasedJob] startup job 01:09:00,003 INFO [org.littlewings.javaee7.cdi.ApplicationScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello ApplicationScopedMessageService@1897461758, now = 2016-08-12T01:09:00.003 01:09:00,004 INFO [org.littlewings.javaee7.cdi.SessionScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello SessionScopedMessageService@1975586858, now = 2016-08-12T01:09:00.004 01:09:00,005 INFO [org.littlewings.javaee7.cdi.RequestScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello RequestScopedMessageService@2064048354, now = 2016-08-12T01:09:00.005 01:09:00,006 INFO [org.littlewings.javaee7.cdi.PseudoScopedMessageService] (DefaultQuartzScheduler_Worker-3) Hello PseudoScopedMessageService@1527663713, now = 2016-08-12T01:09:00.005 01:09:00,006 INFO [org.littlewings.javaee7.cdi.RunnableBasedJob] (DefaultQuartzScheduler_Worker-3) [RunnableBasedJob] end job
Jobã¨Runnableã¯åæã«ä½¿ããªã
ã¨ãããããããã£ã¨åãããããã«æ¸ãã¾ããããå®ã¯ä¸è¨ã¾ã§ãå¿ å®ã«å®è¡ããã¨ããã®ã¢ããªã±ã¼ã·ã§ã³ã¯åä½ãã¾ããã
ã©ããããã¨ãã¨ããã¨ãä¸è¨ã¾ã§ã®ã³ã¼ãã両æ¹æ¸ãã¨ãã@ScheduledãªCDI管çBeanãJobã¨Runnableã®2ã¤ã®å®è£ æ¹æ³ã§ä¸¡æ¹ã¨ãåå¨ãã¦ãããã¨ããç¶æ ã«ãªãã¾ãã
Apache DeltaSpikeã®Scheduler Moduleã¯ããã®ç¶æ ã許容ãã¦ãã¾ãããJobããã³Runnableãå®è£ ãã¦ä½æããCDI管çBeanã¨ãã¦ã®Jobãåå¨ããã¨ããããã¤æã«ä»¥ä¸ã®ãããªã¨ã©ã¼ãè¦ããã¨ã«ãªãã¾ãã
java.lang.IllegalStateException: Please only annotate classes with @org.apache.deltaspike.scheduler.api.Scheduled of type org.quartz.Job or of type java.lang.Runnable, but not both!
ãªãã@ScheduledãªCDI管çBeanã®ç»é²ã¯ãApache DeltaSpikeãå®è£ ãã¦ããCDI Extensionsã«ãã£ã¦å®ç¾ããã¦ãã¾ãã
Jobããã³Runnableã®ä½¿ã£ãJobãããããå®ç¾©ããã¦ããå ´åã¯ããã®ã¯ã©ã¹ãæ¤åºãã¦ã¨ã©ã¼ã¨ãã¾ãã
ããã¥ã¡ã³ãã«ããä¸å¿ããã£ã½ããã¨ãæ¸ãã¦ããã¾ãã
Behind the scenes DeltaSpike registers an adapter for Quartz which just delegates to the run-method once the adapter gets called by Quartz. Technically you end up with almost the same, just with a reduced API for implementing (all) your scheduled jobs. Therefore the main difference is that your code is independent of Quartz-classes. However, you need to select org.quartz.Job or java.lang.Runnable for all your scheduled-tasks, bot not both!
https://deltaspike.apache.org/documentation/scheduler.html#@Scheduledwithorg.quartz.Joborjava.lang.Runnable
ãªããJobãããã¯Runnableã®ã©ã¡ããã使ç¨ãããã¨ã§çµ±ä¸ãã¦ããã°ã@ScheduledãªCDI管çBeanã¯ãè¤æ°ç»é²å¯è½ã§ãã
ãã®ããã«ã
@Scheduled(cronExpression = "0 0/1 * * * ?") @ApplicationScoped class QuartzBasedJob extends Job { // çç¥ } @Scheduled(cronExpression = "10 0/1 * * * ?") @ApplicationScoped class QuartzBasedJob2 extends Job { // çç¥ }
ãããã¯ãããã
@Scheduled(cronExpression = "0 0/1 * * * ?") @ApplicationScoped class RunnableBasedJob extends Runnable { // çç¥ } @Scheduled(cronExpression = "10 0/1 * * * ?") @ApplicationScoped class RunnableBasedJob2 extends Runnable { // çç¥ }
æåã§Jobç»é²ãã
ããã¾ã§ã¯ãã³ã³ããã®èµ·åæã«Jobãç»é²ãã¦ãå³æã«ã¹ã±ã¸ã¥ã¼ãªã³ã°ãã¦ãã¾ããã
ãããæåç»é²ãããã¨ãã§ãã¾ãã
@Scheduled with org.quartz.Job or java.lang.Runnable
Manual Scheduler Control
ããã§æ¸ããã¦ããæ¹æ³ã¯ãQuartzã®Jobã®ã¿ã«ãªãããã§ããRunnableã§ãããããã°ãManagedExecutorServiceã使ããã¨ã«ãªããããªï¼
Execute java.lang.Runnable with ManagedExecutorService
ã§ã話ãæ»ãã¦æåã§Quartzã®Jobãå®è¡ããæ¹æ³ã§ããã@AlternativesãªCDI管çBeanãQuartzSchedulerProducerãbeans.xmlã«æå®ãã¾ãã
src/main/resources/META-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="annotated"> <alternatives> <class>org.apache.deltaspike.scheduler.impl.QuartzSchedulerProducer</class> </alternatives> </beans>
ããã¦ãããã¾ã§ã¨åãããã«Jobãä½æãã¾ãã@Injectããµã¤ãã«ä½¿ãã¾ãã
src/main/scala/org/littlewings/javaee7/cdi/ManualQuartzBasedJob.scala
package org.littlewings.javaee7.cdi import javax.enterprise.context.ApplicationScoped import javax.inject.Inject import org.apache.deltaspike.scheduler.api.Scheduled import org.quartz.{Job, JobExecutionContext} import org.slf4j.{Logger, LoggerFactory} @Scheduled(cronExpression = "30 0/1 * * * ?", onStartup = false) @ApplicationScoped class ManualQuartzBasedJob extends Job { @Inject var applicationScopedMessageService: ApplicationScopedMessageService = _ @Inject var sessionScopedMessageService: SessionScopedMessageService = _ @Inject var requestScopedMessageService: RequestScopedMessageService = _ @Inject var pseudoScopedMessageService: PseudoScopedMessageService = _ val logger: Logger = LoggerFactory.getLogger(getClass) override def execute(context: JobExecutionContext): Unit = { logger.info("[{}] startup job", getClass.getSimpleName) applicationScopedMessageService.loggingMessage() sessionScopedMessageService.loggingMessage() requestScopedMessageService.loggingMessage() pseudoScopedMessageService.loggingMessage() logger.info("[{}] end job", getClass.getSimpleName) } }
å ã»ã©ã¾ã§ã®Jobã¨ã»ã¨ãã©åãã§ãããéãã¯@Scheudledã®onStarupãfalseã«ãã¦ãããã¨ã§ãã
@Scheduled(cronExpression = "30 0/1 * * * ?", onStartup = false) @ApplicationScoped class ManualQuartzBasedJob extends Job {
ãããã¦ããã¨ãã³ã³ããèµ·åæã«ã¹ã±ã¸ã¥ã¼ã©ã¼ã«ç»é²ãããªããªãã¾ãã
ãã ãããããã¨Jobãç»é²ããå¦çãå¿ è¦ã§ããããã¡ãã¯JAX-RSã§ä½æãã¾ããã
JAX-RSã®æå¹åã
src/main/scala/org/littlewings/javaee7/rest/JaxrsApplication.scala
package org.littlewings.javaee7.rest import javax.ws.rs.ApplicationPath import javax.ws.rs.core.Application @ApplicationPath("rest") class JaxrsApplication extends Application
Jobãã¹ã±ã¸ã¥ã¼ã©ã¼ã«ç»é²ãããJAX-RSãªã½ã¼ã¹ã¯ã©ã¹ã
src/main/scala/org/littlewings/javaee7/rest/ManualJobResource.scala
package org.littlewings.javaee7.rest import javax.enterprise.context.RequestScoped import javax.inject.Inject import javax.ws.rs.core.MediaType import javax.ws.rs.{GET, Path, Produces} import org.apache.deltaspike.scheduler.spi.Scheduler import org.littlewings.javaee7.cdi.ManualQuartzBasedJob import org.quartz.Job @Path("job") @RequestScoped class ManualJobResource { @Inject var scheculer: Scheduler[Job] = _ @GET @Path("start-job") @Produces(Array(MediaType.TEXT_PLAIN)) def startJob: String = { scheculer.registerNewJob(classOf[ManualQuartzBasedJob]) "Register Job!!" } }
ããã§ã®ãã¤ã³ãã¯ãApache DeltaSpikeã®Schedulerã@Injectãã¦
@Inject
var scheculer: Scheduler[Job] = _
ä½æããJobã®Classã¯ã©ã¹ãScheduler#registerNewJobãããã¨ã§ããã
scheculer.registerNewJob(classOf[ManualQuartzBasedJob])
ããã§ãJobç»é²å®äºã§ãã
ãããã¤ãã¦ç¢ºèªãã¦ã¿ã¾ãããããã¤å¾ã以ä¸ã®ã³ãã³ããå®è¡ããã¨Jobãç»é²ãã
$ curl http://localhost:8080/rest/job/start-job Register Job!!
å®è¡ãããããã«ãªãã¾ãã
01:32:30,012 INFO [org.littlewings.javaee7.cdi.ManualQuartzBasedJob] (DefaultQuartzScheduler_Worker-5) [ManualQuartzBasedJob] startup job 01:32:30,013 INFO [org.littlewings.javaee7.cdi.ApplicationScopedMessageService] (DefaultQuartzScheduler_Worker-5) Hello ApplicationScopedMessageService@1293488989, now = 2016-08-12T01:32:30.013 01:32:30,014 INFO [org.littlewings.javaee7.cdi.SessionScopedMessageService] (DefaultQuartzScheduler_Worker-5) Hello SessionScopedMessageService@1057457274, now = 2016-08-12T01:32:30.014 01:32:30,015 INFO [org.littlewings.javaee7.cdi.RequestScopedMessageService] (DefaultQuartzScheduler_Worker-5) Hello RequestScopedMessageService@2047151614, now = 2016-08-12T01:32:30.015 01:32:30,016 INFO [org.littlewings.javaee7.cdi.PseudoScopedMessageService] (DefaultQuartzScheduler_Worker-5) Hello PseudoScopedMessageService@818428427, now = 2016-08-12T01:32:30.015 01:32:30,016 INFO [org.littlewings.javaee7.cdi.ManualQuartzBasedJob] (DefaultQuartzScheduler_Worker-5) [ManualQuartzBasedJob] end job
ãã®ä»ãæ°ã«ãªããã¨
ä»åã¯ç¹ã«è©¦ãã¾ããã§ããããCRONã®ã¹ã±ã¸ã¥ã¼ã«ãæå®ãã¦ããé¨åã«ã¯è¨å®ãã¡ã¤ã«ã®å 容ãåç §ãããã¨ãã§ããããã§ãã
@Scheduled(cronExpression = "{myCronExpression}")
ãã¡ãã使ãã¨ãJobã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ãProjectStageãã¨ã«ç®¡çãããã¨ãã§ããããã§ããã
ã¾ããQuartzã§ã¯ãªããç¬èªã®ã¹ã±ã¸ã¥ã¼ã©ã¼ãå®è£ ãããã¨ãå¯è½ã§ã¯ããããã§ãã
ã¾ã¨ã
Apache DeltaSpikeã®Schedule Moduleã試ãã¦ã¿ã¾ããã
å é¨å®è£ ã¯Quartzã§ãããCDI管çBeanãã¤ã³ã¸ã§ã¯ã·ã§ã³ã§ããJobãä½æã§ãããªã©ãã¡ãã£ã¨ä¾¿å©ããã ãªãã¨æãã¾ããã
ä»åä½æããã³ã¼ãã¯ããã¡ãã«ç½®ãã¦ãã¾ãã
https://github.com/kazuhira-r/javaee7-scala-examples/tree/master/cdi-deltaspike-scheduler