java quartz scheduler fire a new job immediately

Is it possible to crate a job that will trigger immediately ? when i want the job to be triggres now i builed a cron expression string with the current date and time - i think it's too complicated, is there another way to trigger the job immediately ?

Thank's In Advance.

Answers


All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance.

//Create a new Job 
JobKey jobKey = JobKey.jobKey("myNewJob", "myJobGroup");
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity(jobKey).storeDurably().build();

//Register this job to the scheduler
scheduler.addJob(job, true);

//Immediately fire the Job MyJob.class
scheduler.triggerJob(jobKey);

Note :

  • scheduler is the Scheduler instance used throughout your application . Its start() method should be already called after it is created.

  • The job is the durable job which cannot attach any triggers or cron to it .It can only be fired programmatically by calling triggerJob(JobKey jobKey).


Yeah, use the following Trigger to immediately fire your job instead of waiting upon the Cron Expressions.

    String jobName = ""; // Your Job Name
    String groupName = ""; // Your Job Group
    Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(jobName, groupName)
                .startNow()
                .build();

You can create the "JobKey" on the fly with the 2 key string values.

IScheduler sched = /* however you get your scheduler*/;

sched.TriggerJob(new JobKey("myname", "mygroup"));

Need Your Help

MySQL Hashing Function Implementation

php mysql hash passwords

I know that php has md5(), sha1(), and the hash() functions, but I want to create a hash using the MySQL PASSWORD() function. So far, the only way I can think of is to just query the server, but I...

How difficult is Haskell multi-threading?

multithreading haskell concurrency multicore

I have heard that in Haskell, creating a multi-threaded application is as easy as taking a standard Haskell application and compiling it with the -threaded flag. Other cases, however, have describe...