zoukankan      html  css  js  c++  java
  • 我的Quartz笔记

    代码:

    package com.smt.autorun;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.apache.log4j.Logger;
    import org.apache.poi.util.IOUtils;
    import org.ehcache.Cache;
    import org.ehcache.config.builders.CacheConfigurationBuilder;
    import org.ehcache.config.builders.CacheManagerBuilder;
    import org.ehcache.config.builders.ResourcePoolsBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    import static org.quartz.CronScheduleBuilder.*;
    import static org.quartz.JobBuilder.newJob;
    
    import com.smt.jobs.CheckJob;
    import com.smt.pojo.Table;
    
    public class AutoRun implements ServletContextListener {
    
        private static final Logger LOGGER = Logger.getLogger(AutoRun.class);
        
        private Scheduler scheduler = null;
    
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
            // TODO Auto-generated method stub
            LOGGER.info("end");
            EhcacheUtils.closeCache();
            try {
                scheduler.shutdown(true);
            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
            // TODO Auto-generated method stub
            LOGGER.info("begin");
            EhcacheUtils.openCache();
            Cache<String, Object> myCache = EhcacheUtils.cacheManager.createCache("myCache", CacheConfigurationBuilder
                    .newCacheConfigurationBuilder(String.class, Object.class, ResourcePoolsBuilder.heap(100)).build());
            myCache.put("test", "test");
    
            // 加载所有的pdm
            // PdmParser parser = new PdmParser();
            // List<Table> list = new ArrayList<Table>();
            // File file = new File("D:\work\powerdesigner");
            // File[] files = file.listFiles();
            // for(File f : files){
            // if(f.getName().contains(".pdm")){
            // Table[] tbs =
            // parser.parsePDM_VO("D:\work\powerdesigner\"+f.getName(),f.getName().replaceAll(".pdm",
            // ""));
            // for(Table tb : tbs){
            // list.add(tb);
            // }
            // }
            // }
            // myCache.put("tables", list);
    
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            try {
                scheduler = schedulerFactory.getScheduler();
                JobDetail job = newJob(CheckJob.class) //定义Job类为HelloQuartz类,这是真正的执行逻辑所在
                        .withIdentity("job1", "group2") //定义name/group
                        .usingJobData("name", "quartz") //定义属性
                        .build();
                Trigger trigger = TriggerBuilder.newTrigger()  
                        .withIdentity("myTrigger")
                        .withSchedule(dailyAtHourAndMinute(19,00)) 
                        .build();
                scheduler.scheduleJob(job, trigger);
                scheduler.start();
            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    package com.smt.jobs;
    
    import org.apache.log4j.Logger;
    import org.quartz.DisallowConcurrentExecution;
    import org.quartz.Job;
    import org.quartz.JobDetail;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import com.smt.controller.DatabaseController;
    
    public class CheckJob implements Job {
    
        private static final Logger LOGGER = Logger.getLogger(CheckJob.class);
        
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            JobDetail detail = arg0.getJobDetail();
            //获取参数
            //String name = detail.getJobDataMap().getString("name");
            LOGGER.info("执行了!!!!!!!!!!!!!!!!!!!!!!!");
        }
    
    }

    pom.xml

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>  
        <groupId>org.quartz-scheduler</groupId>  
        <artifactId>quartz-jobs</artifactId>  
        <version>2.2.1</version>  
    </dependency> 

    spring-quartz.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    
        <!-- =========JDBC版=========== -->
        <!-- 
            持久化数据配置,需要添加quartz.properties
         -->
        <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
            <property name="configLocation" value="classpath:quartz.properties"/>   
        </bean>
    
    </beans>

    quartz.properties:

    # Default Properties file for use by StdSchedulerFactory
    # to create a Quartz Scheduler Instance, if a different
    # properties file is not explicitly specified.
    #
    
    #============================================================================
    # Configure Main Scheduler Properties
    #============================================================================
    org.quartz.scheduler.instanceName: ShinhoQuarze
    org.quartz.scheduler.instanceId = AUTO
    
    org.quartz.scheduler.rmi.export: false
    org.quartz.scheduler.rmi.proxy: false
    org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
    #============================================================================
    # Configure ThreadPool
    #============================================================================
    org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount: 2
    org.quartz.threadPool.threadPriority: 5
    org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
    
    org.quartz.jobStore.misfireThreshold: 60000
    #============================================================================
    # Configure JobStore
    #============================================================================
    
    #default config
    #org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
    #u6301u4E45u5316u914Du7F6E
    org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
    org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
    org.quartz.jobStore.useProperties:true
    
    #============================================================================
    #havent cluster spring
    #============================================================================
    org.quartz.jobStore.isClustered = false  
    
    #u6570u636Eu5E93u8868u524Du7F00
    org.quartz.jobStore.tablePrefix:qrtz_
    org.quartz.jobStore.dataSource:qzDS
    
    #============================================================================
    # Configure Datasources
    #============================================================================
    #JDBCu9A71u52A8
    org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
    org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/shbi
    org.quartz.dataSource.qzDS.user:root
    org.quartz.dataSource.qzDS.password:root
    org.quartz.dataSource.qzDS.maxConnection:10
  • 相关阅读:
    匿存函数,内存函数,递归函数,二分法查找
    内置函数
    生成器函数,推导式,生成器表达式
    函数名的应用,闭包,迭代器
    动态参数,作用域
    函数,返回值,参数
    文件操作
    什么是协程
    MYSQL允许远程访问
    phpstorm+xdebug搭建
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/8572591.html
Copyright © 2011-2022 走看看