Monday 17 December 2018

Spring+Quartz Scheduler Integration Example (SimpleTriggerFactoryBean) | Spring Tutorial


Click here to watch on Youtube:
https://www.youtube.com/watch?v=yopHUJqOgsA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge:
Spring+Quartz Scheduler Integration Example (SimpleTriggerFactoryBean) | Spring Tutorial

Spring+Quartz Scheduler Integration Example (SimpleTriggerFactoryBean) | Spring Tutorial

Spring+Quartz Scheduler Integration Example (SimpleTriggerFactoryBean) | Spring Tutorial
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ram.core</groupId>
    <artifactId>SpringDemo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringDemo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <springframework.version>5.1.2.RELEASE</springframework.version>
        <quartz.version>2.3.0</quartz.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Transaction dependency is required with Quartz integration -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
   
        <!-- Quartz framework -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>

    </dependencies>
</project>

ReportCurrentTime.java

package com.ram.scheduling;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

@Component("reportCurrentTime")
public class ReportCurrentTime
{

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
            "HH:mm:ss");

    public void printCurrentTime()
    {
        System.out.println(
                "Current time = " + dateFormat.format(new Date()));
    }

}

quartz-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <context:component-scan base-package="com.ram" />

    <!-- For times when you just need to invoke a method on a specific object -->
    <bean id="simpleJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="reportCurrentTime" />
        <property name="targetMethod" value="printCurrentTime" />
    </bean>

    <!-- Run the job every 2 seconds with initial delay of 1 second -->
    <bean id="simpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="2000" />
    </bean>

    <!-- Scheduler factory bean to glue together jobDetails and triggers to
        Configure Quartz Scheduler -->
    <bean
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="simpleJobDetail" />
            </list>
        </property>
   
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
            </list>
        </property>
    </bean>
</beans>

App.java

package com.ram;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main(String args[])
    {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "quartz-context.xml");
    }

}

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Current time = 09:48:37
Current time = 09:48:39
Current time = 09:48:41
Current time = 09:48:43
Current time = 09:48:45

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/SpringDemo_Quartz_SimpleTriggerFactoryBean.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/javaee/tree/master/Spring_2018/SpringDemo_Quartz_SimpleTriggerFactoryBean

Bitbucket Link:
https://bitbucket.org/ramram43210/spring/src/0989e369f77ed192b0b224588ebfcdeb2019a6a2/Spring_2018/SpringDemo_Quartz_SimpleTriggerFactoryBean/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment