SpringCloud(第 009 篇)簡單 Quartz 微服務,不支持分佈式

SpringCloud(第 009 篇)簡單 Quartz 微服務,不支持分佈式

-java

1、大體介紹

一、本章節僅僅只是爲了測試 Quartz 在微服務中的使用狀況;
二、其實若只是簡單的實現任務調用而言的話,SpringBoot 的 Schedule 這個註解便可知足需求,可是注意該註解不支持分佈式;

三、注意:配置文件中的 mysql 數據庫連接配置你們就各自配置本身的哈;

2、實現步驟

2.1 添加 maven 引用包

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springms-simple-quartz</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <!-- 訪問數據庫模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- web模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- MYSQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
               
        <!-- quartz模塊 -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 添加應用配置文件(springms-simple-quartz/src/main/resources/application.properties)

################################################################################
# mysql com.springms.cloud-test
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:port/hmilyylimh
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.jpa.hibernate.ddl-auto=update
spring.datasource.jpa.show-sql=true

################################################################################
# embedded servlet container
server.port=8390
# sessionTimeout in seconds
server.sessionTimeout=30000

2.3 添加Spring上下文配置文件(springms-simple-quartz/src/main/resources/applicationContext.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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <bean id="properties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
    
    <!-- 使用MethodInvokingJobDetailFactoryBean,任務類能夠不實現Job接口,經過targetMethod指定調用方法-->
    <bean id="taskJob" class="com.springms.cloud.task.TestTask"/>
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="group" value="job_work"/>
        <property name="name" value="job_work_name"/>
        <!--false表示等上一個任務執行完後再開啓新的任務-->
        <property name="concurrent" value="false"/>
        <property name="targetObject">
            <ref bean="taskJob"/>
            </property>
        <property name="targetMethod">
            <value>run</value>
        </property>
    </bean>

    <!--  調度觸發器 -->
    <bean id="myTrigger"
          class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="name" value="work_default_name"/>
        <property name="group" value="work_default"/>
        <property name="jobDetail">
            <ref bean="jobDetail" />
        </property>
        <property name="cronExpression">
            <value>0/10 * * * * ?</value>
        </property>
    </bean>

    <!-- 調度工廠 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>
    
    <bean id="springContextUtil" class="com.springms.cloud.util.SpringApplicationContextUtil"/>

</beans>

2.4 添加任務調度對象類(springms-simple-quartz/src/main/java/com/springms/cloud/domain/ScheduleJob.java)

package com.springms.cloud.domain;

/**
 * 任務調度對象。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/9/18
 *
 */
public class ScheduleJob {
    
    private String jobName;
    private String jobGroup;
    private String desc;
    private String jobStatus;
    private String cronExpression;
    
    public String getJobName() {
        return jobName;
    }
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }
    public String getJobGroup() {
        return jobGroup;
    }
    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String getJobStatus() {
        return jobStatus;
    }
    public void setJobStatus(String jobStatus) {
        this.jobStatus = jobStatus;
    }
    public String getCronExpression() {
        return cronExpression;
    }
    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }
}

2.5 添加QuartzJobFactory(springms-simple-quartz/src/main/java/com/springms/cloud/service/QuartzJobFactory.java)

package com.springms.cloud.service;

import com.springms.cloud.domain.ScheduleJob;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

@DisallowConcurrentExecution
public class QuartzJobFactory implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("任務成功運行");
        ScheduleJob scheduleJob = (ScheduleJob)context.getMergedJobDataMap().get("scheduleJob");
        System.out.println("任務名稱 = [" + scheduleJob.getJobName() + "]");
    }
}

2.6 添加定時任務服務(springms-simple-quartz/src/main/java/com/springms/cloud/service/ScheduleJobService.java)

package com.springms.cloud.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.springms.cloud.task.TestTask;
import com.springms.cloud.util.SpringApplicationContextUtil;
import com.springms.cloud.domain.ScheduleJob;
import org.quartz.CronTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 定時任務服務。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/9/18
 *
 */
public class ScheduleJobService {
    
    private static final Logger Logger = LoggerFactory.getLogger(TestTask.class);
    
    public void getScheduleJob(){
         
        try {
            SchedulerFactoryBean schedulerFactoryBean = SpringApplicationContextUtil.getBean("scheduler");
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
            Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);        
            List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
            for (JobKey jobKey : jobKeys) {
                List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
                for (Trigger trigger : triggers) {
                    ScheduleJob job = new ScheduleJob();
                    job.setJobName(jobKey.getName());
                    job.setJobGroup(jobKey.getGroup());
                    job.setDesc("觸發器:" + trigger.getKey());
                    Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
                    job.setJobStatus(triggerState.name());
                    if (trigger instanceof CronTrigger) {
                        CronTrigger cronTrigger = (CronTrigger) trigger;
                        String cronExpression = cronTrigger.getCronExpression();
                        job.setCronExpression(cronExpression);
                    }
                    jobList.add(job);                                       
                }
            }
            
            for (ScheduleJob job : jobList) {
                Logger.info("計劃列表,name:{},group:{},desc:{},status:{}",job.getJobName(),job.getJobGroup(),job.getDesc(),job.getJobStatus());
            }
            
        } catch (SchedulerException e) {
            Logger.error("SchedulerException", e);
        }
    }
}

2.7 添加測試任務類(springms-simple-quartz/src/main/java/com/springms/cloud/task/TestTask.java)

package com.springms.cloud.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 測試任務類(被任務調度後執行該任務類)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/9/18
 *
 */
public class TestTask {
    
    /** 日誌對象 */
    private static final Logger LOG = LoggerFactory.getLogger(TestTask.class);
    
    public void run() {
        if (LOG.isInfoEnabled()) {
            LOG.info("測試任務線程開始執行");
            
            //new ScheduleJobService().getScheduleJob();
        }
    }

}

2.8 添加 Spring 上下文工具類(springms-simple-quartz/src/main/java/com/springms/cloud/util/SpringApplicationContextUtil.java)

package com.springms.cloud.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring 上下文工具類。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/9/18
 *
 */
@Component
public class SpringApplicationContextUtil implements ApplicationContextAware{
    
    // 聲明一個靜態變量保存   
    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringApplicationContextUtil.applicationContext=applicationContext;
    }
    
    public static ApplicationContext getContext(){
        
        return applicationContext;   
    }  
    
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
               return (T) applicationContext.getBean(name);
     }

}

2.9 添加 Quartz 啓動類(springms-simple-quartz/src/main/java/com/springms/cloud/SimpleQuartzApplication.java)

package com.springms.cloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import java.util.concurrent.TimeUnit;

/**
 * 簡單 Quartz 微服務,不支持分佈式。
 *
 * 其實若只是簡單的實現任務調用而言的話,SpringBoot 的 Schedule 這個註解便可知足需求,可是注意該註解不支持分佈式;
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/18
 *
 */
@ComponentScan
@Configuration
@ImportResource("applicationContext.xml")
public class SimpleQuartzApplication {
    
    private static final Logger Logger = LoggerFactory.getLogger(SimpleQuartzApplication.class);
    
    @Value("${server.port}")
    private int port;
    @Value("${server.sessionTimeout}")
    private int sessionTimeout;    

    public static void main(String[] args) {
        Logger.info("簡單Quartz微服務入口函數編碼-" +System.getProperty("file.encoding"));
                
        SpringApplication.run(SimpleQuartzApplication.class, args);

        System.out.println("【【【【【【 簡單Quartz微服務 】】】】】】已啓動.");
    }
    
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(port);
        factory.setSessionTimeout(sessionTimeout, TimeUnit.SECONDS);
        return factory;
    }
}

3、測試

/****************************************************************************************
 1、簡單Quartz微服務:

 一、添加 Quartz 相關配置文件;
 二、啓動 springms-simple-quartz 模塊服務,啓動1個端口;
 三、而後查看日誌, TestTask 類的日誌不斷被定時打印出來;

 總結:其實若只是簡單的實現任務調用而言的話,SpringBoot 的 Schedule 這個註解便可知足需求,可是注意該註解不支持分佈式;
 ****************************************************************************************/

4、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.gitmysql

SpringCloudTutorial交流QQ羣: 235322432git

SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接web

歡迎關注,您的確定是對我最大的支持!!!spring

相關文章
相關標籤/搜索