在Java中部署和触发Crystal Scheduler

时间:2019-03-30 06:14:13

标签: java eclipse quartz-scheduler

我是Java新手。我想用石英创建一个作业调度程序。当我通过eclipse运行应用程序时,一切正常,但是我不知道如何在tomcat中部署它。在谷歌上到处都有,我发现只有一种创建调度程序的方法,而没有找到将其部署在tomcat中的方法。这可能是最基本的东西,但我不知道。我正在努力部署它。 尽管我用石英制作它,但我并不仅仅坚持使用它。我对其他任何调度程序都开放,例如spring等。 我需要帮助。下面是我编写的代码。

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>com.pratibimb.jobscheduler</groupId>
   <artifactId>JobScheduler</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
          <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.7.0</version>
              <configuration>
                 <source>1.8</source>
                 <target>1.8</target>
              </configuration>
          </plugin>
      <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>3.2.1</version>
      <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>
<dependencies>
  <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
  </dependency>
  <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.12</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.26</version>
</dependency>


web.xml文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
     <display-name>JobScheduler</display-name>
 </web-app>

计划程序,主要方法是:

package com.pratibimb.jobscheduler.scheduler;
import org.quartz.CronScheduleBuilder;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import com.pratibimb.jobscheduler.vaatsalyamjobs.ShowGenericMessages;

public class TaskScheduler {
    public static void main(String[] args) throws Exception {   
        TaskScheduler scheduler = new TaskScheduler();
        scheduler.fireJob();
    }
public void fireJob() throws SchedulerException, InterruptedException {
    SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    Scheduler scheduler = schedFact.getScheduler();
    scheduler.start();

    // define the job and tie it to our HelloJob class
    JobBuilder jobBuilder = JobBuilder.newJob(ShowGenericMessages.class);

    JobDetail jobDetail = jobBuilder.withIdentity("myJob", "group1").build();


 // Fire at curent time + 1 min every day
    Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("myTrigger", "group1")
    .startAt(DateBuilder.todayAt(22, 33, 20))
   // .withSchedule(CronScheduleBuilder.cronSchedule("0 " + (min + 1) + " " + hour + " * * ? *"))     
    .build();

    // Tell quartz to schedule the job using our trigger
    scheduler.scheduleJob(jobDetail, trigger);
    System.out.println("All triggers executed. Shutdown scheduler");
    //scheduler.shutdown();
  }
 }

class:ShowGenericMessages.java

package com.pratibimb.jobscheduler.vaatsalyamjobs;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig; 
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class ShowGenericMessages implements Job {

     public void execute(JobExecutionContext jobContext) throws JobExecutionException {
    System.out.println("test 1");      

    }
}

0 个答案:

没有答案