美文网首页
java maven quartz exampe 实战攻略2

java maven quartz exampe 实战攻略2

作者: 零点145 | 来源:发表于2019-06-13 16:53 被阅读0次

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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.net.simpv.task</groupId>

  <artifactId>cn.net.simpv.task2</artifactId>

  <version>1.0-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>cn.net.simpv.task2</name>

  <url>http://maven.apache.org</url>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <slf4j.version>1.7.6</slf4j.version>

<logback.version>1.1.1</logback.version>

<spring.version>4.0.2.RELEASE</spring.version>

<maven.compiler.source>1.7</maven.compiler.source>

<maven.compiler.target>1.7</maven.compiler.target>

  </properties>

  <build>

<plugins>

<plugin>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-maven-plugin</artifactId>

<version>9.1.4.v20140401</version>

<configuration> 

              <encoding>UTF-8</encoding> 

            </configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

<executions>

<execution>

<id>attach-sources</id>

<goals>

<goal>jar</goal>

</goals>

</execution>

</executions>

<configuration> 

              <encoding>UTF-8</encoding> 

            </configuration> 

</plugin>

</plugins>

</build>

  <dependencies>

  <dependency> 

        <groupId>org.springframework</groupId> 

        <artifactId>spring-core</artifactId> 

        <version>${spring.version}</version> 

    </dependency> 

        <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

  <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

<scope>test</scope>

</dependency>

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>3.8.1</version>

    <scope>test</scope>

    </dependency>

<dependency>

<groupId>org.opensymphony.quartz</groupId>

<artifactId>quartz-all</artifactId>

<version>1.6.1</version>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>3.2.1</version>

</dependency>

  </dependencies>

</project>

App.java

package cn.net.simpv.task;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* Hello world!

*

*/

public class App

{

    public static void main( String[] args )

    {

        System.out.println( "Hello World!" );

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");

    }

}

Job1.java

package cn.net.simpv.task;

import org.quartz.JobExecutionContext; 

import org.quartz.JobExecutionException; 

import org.springframework.scheduling.quartz.QuartzJobBean; 

public class Job1 extends QuartzJobBean { 

private int timeout; 

private static int i = 0; 

//调度工厂实例化后,经过timeout时间开始执行调度 

public void setTimeout(int timeout) { 

this.timeout = timeout; 

/**

* 要调度的具体任务

*/ 

@Override 

protected void executeInternal(JobExecutionContext context) 

throws JobExecutionException { 

  System.out.println("定时任务执行中…"); 

Job2.java

package cn.net.simpv.task;

public class Job2 {

public void doJob2() { 

System.out.println("不继承QuartzJobBean方式-调度进行中..."); 

}

spring-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:tx="http://www.springframework.org/schema/tx"

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">

<context:annotation-config />

    <bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> 

    <property name="jobClass" value="cn.net.simpv.task.Job1" /> 

    <property name="jobDataAsMap"> 

    <map> 

    <entry key="timeout" value="0" /> 

    </map> 

    </property> 

    </bean> 

    <bean id="job2" 

    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 

    <property name="targetObject"> 

    <bean class="cn.net.simpv.task.Job2" /> 

    </property> 

    <property name="targetMethod" value="doJob2" /> 

    <property name="concurrent" value="false" /><!-- 作业不并发调度 --> 

    </bean> 

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 

    <property name="jobDetail" ref="job1" /> 

    <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 --> 

    <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 --> 

    </bean> 

    <bean id="simpleTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 

    <property name="jobDetail" ref="job2" /> 

    <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 --> 

    <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 --> 

    </bean> 

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 

    <property name="triggers"> 

    <list> 

    <ref bean="simpleTrigger" /> 

    </list> 

    </property> 

    </bean> 

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 

    <property name="triggers"> 

    <list> 

    <ref bean="simpleTrigger2" /> 

    </list> 

    </property> 

    </bean> 

</beans>

相关文章

网友评论

      本文标题:java maven quartz exampe 实战攻略2

      本文链接:https://www.haomeiwen.com/subject/xgnsfctx.html