quartz2.3.0 (XII) is added to the Task Scheduler job scheduler via remote RMI protocol

This code example adds to the Task Scheduler job scheduler by remote RMI protocol.

Code files including: job task class (SimpleJob.java), RMI server server class (RemoteServerExample.java), RMI client client class (RemoteClientExample.java).

Note: job tasks and client class clients in the same application.

RMI server server class (RemoteServerExample.java)

Setting an end timing of the first configuration server quartz.properties

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName: Sched1
org.quartz.scheduler.rmi.export: true         //重点
org.quartz.scheduler.rmi.registryHost: localhost     //重点
org.quartz.scheduler.rmi.registryPort: 1099          //重点
org.quartz.scheduler.rmi.createRegistry: true

#============================================================================
# Configure ThreadPool  
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5

#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

RemoteServerExample.java Class Code:

 

Package org.quartz.examples.example12; 

Import org.quartz.Scheduler;
 Import org.quartz.SchedulerFactory;
 Import org.quartz.SchedulerMetaData;
 Import org.quartz.impl.StdSchedulerFactory;
 Import org.slf4j.Logger;
 Import org.slf4j .LoggerFactory; 

/ ** 
 * this example will generate a large number of jobs to be run 
 * / 
public  class RemoteServerExample { 
    Logger the LOG = LoggerFactory.getLogger (RemoteServerExample. class ); 

    public  void rUN () throws Exception { 

        // initialize a scheduling facility, and examples of a scheduling class
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        sched.start();

        try {
            Thread.sleep(300L * 1000L);
        } catch (Exception e) {
            //
        }

        sched.shutdown(true);
        LOG.info("------- Shutdown Complete -----------------");

        SchedulerMetaData metaData = sched.getMetaData();
        LOG.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
        
    }

    public static void main(String[] args) throws Exception {

        RemoteServerExample example = new RemoteServerExample();
        example.run();
    }

}

 

 

job task class (SimpleJob.java)

 

 

Package org.quartz.examples.example12; 

Import java.util.Date; 

Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory;
 Import the org.quartz.Job;
 Import org.quartz.JobExecutionContext;
 Import org.quartz.JobExecutionException ;
 Import org.quartz.JobKey; 

/ ** 
 * achieve a dumb job, means for testing. 
 * / 
Public  class SimpleJob the implements the Job { 

    public  static  Final String the MESSAGE = "MSG" ; 

    Private  static Logger LoggerFactory.getLogger the LOG = (. SimpleJob class );

    // must be modified public constructor with no arguments 
    public SimpleJob () { 
    } 

    // task execution method 
    public  void Execute (the JobExecutionContext context) throws JobExecutionException { 
        JobKey JobKey = context.getJobDetail () getKey ();. 
        String Message = ( String) context.getJobDetail () getJobDataMap () GET (MESSAGE);.. 
        log.info ( "task execution 1.SimpleJob:" + + JobKey "Executing AT" + new new a Date ()); 
        log.info ( "task execution 2.SimpleJob: MSG: "+ Message); 
    } 

}

 

 

RMI client client class (RemoteClientExample.java)

Client-side configuration setting timing quartz.properties

org.quartz.scheduler.instanceName: Sched1
org.quartz.scheduler.logger: schedLogger
org.quartz.scheduler.rmi.proxy: true        //重点
org.quartz.scheduler.rmi.registryHost: localhost       //重点
org.quartz.scheduler.rmi.registryPort: 1099            //重点

 

RemoteClientExample.java Class Code:

Package org.quartz.examples.example12; 

Import  static org.quartz.CronScheduleBuilder.cronSchedule;
 Import  static org.quartz.JobBuilder.newJob;
 Import  static org.quartz.TriggerBuilder.newTrigger; 

Import org.quartz.JobDataMap;
 Import org.quartz .JobDetail;
 Import org.quartz.Scheduler;
 Import org.quartz.SchedulerFactory;
 Import org.quartz.Trigger;
 Import org.quartz.impl.StdSchedulerFactory;
 Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory; 

/ ** 
 * this example is a client program that remotely communicate with the dispatcher to dispatch the job.
 * In this example, we will need to use the JDBC job storage. The client is connected to the remote stored job scheduling jobs JDBC. 
 * / 
Public  class RemoteClientExample {
     static Logger LoggerFactory.getLogger the LOG = (RemoteClientExample. Class ); 

    public  void RUN () throws Exception {
      // initialize a scheduling facility, and a scheduling class instantiates 
        the SchedulerFactory SF = new new the StdSchedulerFactory (); 
        Scheduler sched = sf.getScheduler (); 

        // definition of a job task, remotely add tasks to the scheduler  
         // Group: default, job: remotelyAddedJob 
        the JobDetail job = newJob (SimpleJob. class ) 
            .withIdentity ("remotelyAddedJob", "default")
            .build();
        
        JobDataMap map = job.getJobDataMap();
        map.put("msg", "Your remotely added job has executed!");
        
        Trigger trigger = newTrigger()
            .withIdentity("remotelyAddedTrigger", "default")
            .forJob(job.getKey())
            .withSchedule(cronSchedule("0/2 * * * * ?"))
            .build();
        sched.scheduleJob(job, trigger);

        LOG.info("Remote job scheduled.");
    }

    public static void main(String[] args) throws Exception {

        RemoteClientExample example = new RemoteClientExample();
        example.run();
        
    }

}

 

Guess you like

Origin www.cnblogs.com/zhuwenjoyce/p/11221004.html
Recommended