You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2021/04/05 14:17:04 UTC

[isis-app-simpleapp] branch jdo-SNAPSHOT updated: adds quartz example

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch jdo-SNAPSHOT
in repository https://gitbox.apache.org/repos/asf/isis-app-simpleapp.git


The following commit(s) were added to refs/heads/jdo-SNAPSHOT by this push:
     new b0c4c28  adds quartz example
     new e299f00  Merge branch 'jdo' into jdo-SNAPSHOT
b0c4c28 is described below

commit b0c4c28983f471364227e072b587167920fe12bd
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Apr 5 15:16:39 2021 +0100

    adds quartz example
---
 webapp/pom.xml                                     | 20 +++++++
 .../main/java/domainapp/webapp/AppManifest.java    |  2 +
 .../java/domainapp/webapp/quartz/QuartzModule.java | 45 ++++++++++++++++
 .../domainapp/webapp/quartz/job/SampleJob.java     | 63 ++++++++++++++++++++++
 4 files changed, 130 insertions(+)

diff --git a/webapp/pom.xml b/webapp/pom.xml
index 0b27712..76cfa55 100644
--- a/webapp/pom.xml
+++ b/webapp/pom.xml
@@ -95,6 +95,26 @@
             <artifactId>isis-extensions-flyway-impl</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-quartz</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-logging</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-log4j2</artifactId>
+        </dependency>
     </dependencies>
 
     <profiles>
diff --git a/webapp/src/main/java/domainapp/webapp/AppManifest.java b/webapp/src/main/java/domainapp/webapp/AppManifest.java
index 3effeab..03a538e 100644
--- a/webapp/src/main/java/domainapp/webapp/AppManifest.java
+++ b/webapp/src/main/java/domainapp/webapp/AppManifest.java
@@ -18,6 +18,7 @@ import org.apache.isis.viewer.wicket.viewer.IsisModuleViewerWicketViewer;
 import domainapp.webapp.application.ApplicationModule;
 import domainapp.webapp.application.fixture.scenarios.DomainAppDemo;
 import domainapp.webapp.custom.CustomModule;
+import domainapp.webapp.quartz.QuartzModule;
 
 @Configuration
 @Import({
@@ -34,6 +35,7 @@ import domainapp.webapp.custom.CustomModule;
 
         ApplicationModule.class,
         CustomModule.class,
+        QuartzModule.class,
 
         // discoverable fixtures
         DomainAppDemo.class
diff --git a/webapp/src/main/java/domainapp/webapp/quartz/QuartzModule.java b/webapp/src/main/java/domainapp/webapp/quartz/QuartzModule.java
new file mode 100644
index 0000000..6bb080d
--- /dev/null
+++ b/webapp/src/main/java/domainapp/webapp/quartz/QuartzModule.java
@@ -0,0 +1,45 @@
+package domainapp.webapp.quartz;
+
+import org.quartz.JobDetail;
+import org.quartz.SimpleTrigger;
+import org.quartz.Trigger;
+import org.quartz.TriggerBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.quartz.JobDetailFactoryBean;
+import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
+
+import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
+
+import lombok.val;
+
+import domainapp.webapp.quartz.job.SampleJob;
+
+@Configuration
+@ComponentScan
+public class QuartzModule {
+
+    private static final int REPEAT_INTERVAL_SECS = 60;
+    private static final int START_DELAY_SECS = 20;
+    private static final int MILLIS_PER_SEC = 1000;
+
+    @Bean
+    public JobDetailFactoryBean jobDetail() {
+        val jobDetailFactory = new JobDetailFactoryBean();
+        jobDetailFactory.setJobClass(SampleJob.class);
+        jobDetailFactory.setDescription("Invoke Sample Job service...");
+        jobDetailFactory.setDurability(true);
+        return jobDetailFactory;
+    }
+
+    @Bean
+    public SimpleTriggerFactoryBean trigger(JobDetail job) {
+        val trigger = new SimpleTriggerFactoryBean();
+        trigger.setJobDetail(job);
+        trigger.setStartDelay(START_DELAY_SECS * MILLIS_PER_SEC);
+        trigger.setRepeatInterval(REPEAT_INTERVAL_SECS * MILLIS_PER_SEC);
+        trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
+        return trigger;
+    }
+}
diff --git a/webapp/src/main/java/domainapp/webapp/quartz/job/SampleJob.java b/webapp/src/main/java/domainapp/webapp/quartz/job/SampleJob.java
new file mode 100644
index 0000000..2524790
--- /dev/null
+++ b/webapp/src/main/java/domainapp/webapp/quartz/job/SampleJob.java
@@ -0,0 +1,63 @@
+package domainapp.webapp.quartz.job;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import javax.inject.Inject;
+
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import org.apache.isis.applib.services.user.UserMemento;
+import org.apache.isis.applib.services.xactn.TransactionalProcessor;
+import org.apache.isis.commons.functional.Result;
+import org.apache.isis.core.interaction.session.InteractionFactory;
+import org.apache.isis.core.security.authentication.Authentication;
+import org.apache.isis.core.security.authentication.standard.SimpleAuthentication;
+
+import lombok.RequiredArgsConstructor;
+
+import lombok.extern.log4j.Log4j2;
+
+import lombok.extern.slf4j.Slf4j;
+
+import domainapp.modules.simple.dom.so.SimpleObject;
+import domainapp.modules.simple.dom.so.SimpleObjects;
+
+@Component
+@RequiredArgsConstructor(onConstructor_ = {@Inject})
+@Slf4j
+public class SampleJob implements Job {
+
+    private final InteractionFactory interactionFactory;
+    private final TransactionalProcessor transactionalProcessor;
+    private final SimpleObjects simpleObjects;
+
+    public void execute(JobExecutionContext context) throws JobExecutionException {
+        final List<SimpleObject> all = all();
+        log.info("{} objects in the database", all.size());
+    }
+
+    List<SimpleObject> all() {
+        return callAuthenticated(newAuthentication(), () -> simpleObjects.listAll())
+                .optionalElseFail() // re-throws exception that has occurred, if any
+                .orElse(Collections.emptyList()); // handles null case, if required
+    }
+
+    private SimpleAuthentication newAuthentication() {
+        return SimpleAuthentication.validOf(UserMemento.ofName("sven"));
+    }
+
+    private <T> Result<T> callAuthenticated(
+            final Authentication authentication,
+            final Callable<T> task) {
+
+        return interactionFactory.callAuthenticated(
+                authentication,
+                () -> transactionalProcessor.callWithinCurrentTransactionElseCreateNew(task));
+    }
+}