首页 > 代码库 > java maven quartz exampe 实战攻略

java maven quartz exampe 实战攻略

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.task1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>cn.net.simpv.task1</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-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>

SimpleJob.java

package cn.net.simpv.task;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public  class SimpleJob implements Job {
    
    public void execute(JobExecutionContext jobCtx)throws JobExecutionException
    {
      System.out.println(jobCtx.getTrigger().getClass().getName()+"triggered time is:"+new Date());
    }

}


App.java


package cn.net.simpv.task;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println("Hello World!");
        try
        {
        JobDetail jobdetail = new JobDetail("job1_1","jgroup1",SimpleJob.class);
        
        SimpleTrigger  simpleTrigger=new SimpleTrigger("trigger1","tgroup1");
        simpleTrigger.setStartTime(new Date());
        simpleTrigger.setRepeatInterval(1000);
        simpleTrigger.setRepeatCount(1);
        
        SchedulerFactory schedulerFactory=new StdSchedulerFactory();
        Scheduler scheduler=schedulerFactory.getScheduler();
        scheduler.scheduleJob(jobdetail,simpleTrigger);
        scheduler.start();
        }catch(Exception ex)
        {
        	ex.printStackTrace();
        }
        
        
    }
}