{"id":2559,"date":"2018-05-02T12:26:17","date_gmt":"2018-05-02T10:26:17","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2559"},"modified":"2018-05-02T12:26:17","modified_gmt":"2018-05-02T10:26:17","slug":"domino-spring-boot-scheduledtasks","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2559","title":{"rendered":"Domino &#038; Spring Boot: ScheduledTasks"},"content":{"rendered":"<p>When developing Spring Boot applications running on Domino, there is a feature which runs out of the box and makes developers happy: <a href=\"https:\/\/spring.io\/guides\/gs\/scheduling-tasks\/\" target=\"_blank\" rel=\"noopener\">ScheduledTasks<\/a>.<\/p>\n<p>These are the equivalent for agents, but they are running directly in the HTTP task (which allows to access the complete Spring Application at runtime) and can be scheduled exactly to the milisecond.<\/p>\n<p>To enable a scheduled task, you first have to add the <em>@EnableScheduling<\/em> annotation in your <em>Application<\/em> class:<\/p>\n<pre><code>package domino_spring.plugin;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.scheduling.annotation.EnableScheduling;\r\n\r\n@SpringBootApplication\r\n<strong>@EnableScheduling<\/strong>\r\npublic class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {\r\n\r\n   public static void main(String[] args) {\r\n      SpringApplication.run(Application.class, args);\r\n   }\r\n\r\n}<\/code><\/pre>\n<p>Next step is to declare the <a href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/current\/javadoc-api\/org\/springframework\/scheduling\/TaskScheduler.html\" target=\"_blank\" rel=\"noopener\">TaskScheduler<\/a> used. Just add a bean to your <em>Application<\/em> class:<\/p>\n<pre><code>@Bean\r\n public TaskScheduler taskScheduler() {\r\n      return new ConcurrentTaskScheduler();\r\n }<\/code><\/pre>\n<p>After completing the setup you can define your scheduled task:<\/p>\n<pre><code>package domino_spring.plugin;\r\n\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.scheduling.annotation.Scheduled;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class ScheduledTasks {\r\n\r\n   private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);\r\n\r\n   private static final SimpleDateFormat dateFormat = new SimpleDateFormat(\"HH:mm:ss\");\r\n\r\n   @Scheduled(fixedRate = 5000)\r\n   public void reportCurrentTime() {\r\n      log.info(\"The time is now {}\", dateFormat.format(new Date()));\r\n   }\r\n \r\n}<\/code><\/pre>\n<p>When the Sprint Boot application is now started, the current time is\u00a0 printed to the logs every 5 seconds.<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2560\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19.png\" alt=\"\" width=\"1160\" height=\"362\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19.png 1160w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19-300x94.png 300w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19-768x240.png 768w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/05\/Greenshot-2018-05-02-12.23.19-1024x320.png 1024w\" sizes=\"auto, (max-width: 1160px) 100vw, 1160px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing Spring Boot applications running on Domino, there is a feature which runs out of the box and makes developers happy: ScheduledTasks. These are the equivalent for agents, but they are running directly in the HTTP task (which allows &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2559\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,89,82,126,81],"tags":[7,31,16,128,127,12],"class_list":["post-2559","post","type-post","status-publish","format-standard","hentry","category-agenten","category-java","category-server","category-spring","category-web","tag-domino","tag-java","tag-server","tag-spring","tag-spring-boot","tag-web"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2559"}],"version-history":[{"count":2,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2559\/revisions"}],"predecessor-version":[{"id":2562,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2559\/revisions\/2562"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}