How to do with Quartz put some trigger to one job? - quartz-scheduler

public static void sendEmailForNewTrigger(String jobName, String triggerName, Date sendDate) {
try {
Scheduler sched = QuartzSchedulerManager.getInstanceScheduler();
JobKey jobKey = JobKey.jobKey(jobName, "group_email");
JobDetail job = sched.getJobDetail(jobKey);
SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity(triggerName)
.startAt(sendDate)
.build();
if (job == null) {
job = newJob(SendEmailJob.class).withIdentity(jobName, "group_email").build();
}
sched.scheduleJob(job, trigger);
log.info(jobName + " will run at: " + sendDate);
} catch (SchedulerException e) {
log.error(e.toString());
throw new RuntimeException(e.getMessage());
}
}
in my code ,it can't work, always tell me 'Unable to store Job : 'group_email.send_1', because one already exists with this identification.' i don't know how to do ,just want to add two or more trigger to one job.

Related

quartz.net simple example as per the time given

I am new to quartz.net. I want to build a simple window based application which scheduled the task. Suppose I have 4 task ans it start and end time
Example
Breakfast ; 8:00;8:30
Lunch;13:00;13:30
dinner;19:30;20:00
Now I want when I click on button at 8:00 AM a message box should appear with a text "breakfast started!!!" at 8:30 AM again a message box should appear with a text as "Breakfast end!!!" and so on.
I had gone through tutorial. But confuse how to proceed. Can any one help me out?
EDIT
Is it possible to use one job and one triggers for this?
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
for (int i = 0; i < listBox1.Items.Count; i++)
{
string[] strArr = Regex.Split(listBox1.Items[i].ToString(), #";", RegexOptions.Multiline);
// define the job and tie it to our HelloJob class
IJobDetail jobStart = JobBuilder.Create<HelloJob>()
.WithIdentity("job" + i, "group1") // name "myJob", group "group1"
.StoreDurably()
.UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "starts")
.Build();
string[] ArrStart = strArr[1].Trim().Split(':');
ITrigger triggerstart = TriggerBuilder.Create()
.WithIdentity("trigger" + i, "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(ArrStart[0]), Convert.ToInt32(ArrStart[1])))
// .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
.ForJob(jobStart)
.Build();
// .WithSchedule(CronScheduleBuilder.CronSchedule("0 4 06 1/1 * ? *"))
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(jobStart, triggerstart);
scheduler.Start();
string[] Arrend = strArr[2].Trim().Split(':');
IJobDetail jobend = JobBuilder.Create<HelloJob>()
.WithIdentity("job1" + i, "group1")
.StoreDurably()
.UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "end")
.Build();
ITrigger triggerend = TriggerBuilder.Create()
.WithIdentity("trigger1" + i, "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(Arrend[0]), Convert.ToInt32(Arrend[1])))
// .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
.ForJob(jobend)
.Build();
scheduler.ScheduleJob(jobend, triggerend);
scheduler.Start();
}
}
catch (SchedulerException se)
{
Console.WriteLine("Scheduler Exception : " + se);
}
}
public class HelloJob : IJob
{
public void Execute(IJobExecutionContext context)
{
JobKey key = context.JobDetail.Key;
JobDataMap dataMap = context.JobDetail.JobDataMap;
string jobSays = dataMap.GetString("jobSays");
MessageBox.Show(jobSays);
}
}
You can schedule a job as follows and use this simple example:
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
IJobDetail job = JobBuilder.Create<MealAlertJob>()
.WithIdentity("mealJob", "mealJobGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("breakfastStartTrigger", "mealTriggerGroup")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 0))
.ForJob(job)
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("breakfastEndTrigger", "mealTriggerGroup")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 30))
.ForJob(job)
.Build();
scheduler.ScheduleJob(job, trigger);
scheduler.ScheduleJob(trigger1);
scheduler.Start();
You should implement your logic (let's say showing a message box) in your job class's "Execute" method.
Defining multiple triggers for your job results in multiple execution. In the job class for example "MealAlertJob" you may check the time and show an alert based on it.
In the above example you can add more triggers for the other four times (lunch start/end and dinner start/end).
public class MealAlertJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var now = DateTime.Now;
var hour = now.Hour;
var minute = now.Minute;
if (hour == 8 && minute == 0)
System.Windows.Forms.MessageBox.Show("Breakfast started!!!");
//and so on....
}
}
or something like this.

how to run multiple jobs in quartz scheduler in struts

I implemented multiple jobs in quartz scheduler as a plugin in struts but only first job is running by using the tutorial from http://www.mkyong.com/struts/struts-quartz-scheduler-integration-example/
public class QuartzPlugin implements PlugIn {
private JobDetail job = null;
private Trigger trigger = null;
private Scheduler scheduler = null;
private static Class<QuartzPlugin> clazz = QuartzPlugin.class;
public static final String KEY_NAME = clazz.getName();
private static Logger logger = Logger.getLogger(clazz);
#Override
public void destroy() {
try {
String METHODNAME = "destroy";
logger.debug("entering " + KEY_NAME + " " + METHODNAME);
scheduler.shutdown();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void init(ActionServlet servlet, ModuleConfig modConfig) throws ServletException {
String METHODNAME = "init";
logger.debug("entering " + KEY_NAME + " " + METHODNAME);
job = JobBuilder.newJob(SchedulerJob.class).withIdentity("anyJobName","group1").build();
try {
trigger = TriggerBuilder.newTrigger().withIdentity("anyTriggerName", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/30 * * * * ?")).build();
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
servlet.getServletContext().setAttribute(KEY_NAME, scheduler);
// define the job and tie it to our HelloJob class
JobDetail job2 = JobBuilder.newJob(HelloJob.class).withIdentity("job2", "group2").build();
// Trigger the job to run on the next round minute
Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2")
.withSchedule(CronScheduleBuilder.cronSchedule("15/45 * * * * ?")).build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
scheduler.scheduleJob(job2, trigger2);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}

Quartz not executing jobs randomly

I'm trying to use Quartz in order to schedule jobs in a web app running on Glassfish. I'm using RAMJobStore. The problem is that sometimes, the job that was scheduled isn't being executed, even if it was scheduled in the past or the future. The amount of jobs are extremely low, a total of under 20 jobs scheduled at all times on the scheduler and a guaranteed maximum of 1 job running at the same time, so I presume the thread count is not an issue, I could set it to threadCount 1 and it would still work. The scheduler is also not being shut down before the servlet is being destroyed. So what can be the cause for some jobs not being run ?
StartupServlet
public void init()
{
try
{
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
loadJobs();
}
catch (SchedulerException se)
{
se.printStackTrace();
}
}
#Override
public void destroy()
{
try
{
scheduler.shutdown();
}
catch (SchedulerException se)
{
se.printStackTrace();
}
}
Scheduling a job
JobDetail job = JobBuilder.newJob(ScheduledTransactionJob.class)
.withIdentity(transaction.getId())
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(transaction.getId())
.startAt(date)
.build();
try
{
scheduler.scheduleJob(job, trigger);
dateFormat = new SimpleDateFormat("dd MMM yyyy, HH:mm:ss");
String recurringTransactionTime = dateFormat.format(date);
logger.info("Scheduled job for " + recurringTransactionTime);
}
catch (SchedulerException se)
{
se.printStackTrace();
}
quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = AppScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 10
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Seems to be working now. Haven't ran into any more problems. Could've been a config issue, as I have moved the config file in /src/main/resources.
Also try turning logging on in order to help with the debug:
log4j.logger.com.gargoylesoftware.htmlunit=DEBUG
We also added a JobTriggerListener to help with the logs:
private static class JobTriggerListener implements TriggerListener
{
private String name;
public JobTriggerListener(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void triggerComplete(Trigger trigger, JobExecutionContext context,
Trigger.CompletedExecutionInstruction triggerInstructionCode)
{
}
public void triggerFired(Trigger trigger, JobExecutionContext context)
{
}
public void triggerMisfired(Trigger trigger)
{
logger.warn("Trigger misfired for trigger: " + trigger.getKey());
try
{
logger.info("Available threads: " + scheduler.getCurrentlyExecutingJobs());
}
catch (SchedulerException ex)
{
logger.error("Could not get currently executing jobs.", ex);
}
}
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context)
{
return false;
}
}

How to exclude job parameter from uniqueness in Spring Batch?

I am trying to launch a job in Spring Batch 2, and I need to pass some information in the job parameters, but I do not want it to count for the uniqueness of the job instance. For example, I'd want these two sets of parameters to be considered unique:
file=/my/file/path,session=1234
file=/my/file/path,session=5678
The idea is that there will be two different servers trying to start the same job, but with different sessions attached to them. I need that session number in both cases. Any ideas?
Thanks!
So, if 'file' is the only attribute that's supposed to be unique and 'session' is used by downstream code, then your problem matches almost exactly what I had. I had a JMSCorrelationId that i needed to store in the execution context for later use and I didn't want it to play into the job parameters' uniqueness. Per Dave Syer, this really wasn't possible, so I took the route of creating the job with the parameters (not the 'session' in your case), and then adding the 'session' attribute to the execution context before anything actually runs.
This gave me access to 'session' downstream but it was not in the job parameters so it didn't affect uniqueness.
References
https://jira.springsource.org/browse/BATCH-1412
http://forum.springsource.org/showthread.php?104440-Non-Identity-Job-Parameters&highlight=
You'll see from this forum that there's no good way to do it (per Dave Syer), but I wrote my own launcher based on the SimpleJobLauncher (in fact I delegate to the SimpleLauncher if a non-overloaded method is called) that has an overloaded method for starting a job that takes a callback interface that allows contribution of parameters to the execution context while not being 'true' job parameters. You could do something very similar.
I think the applicable LOC for you is right here:
jobExecution = jobRepository.createJobExecution(job.getName(),
jobParameters);
if (contributor != null) {
if (contributor.contributeTo(jobExecution.getExecutionContext())) {
jobRepository.updateExecutionContext(jobExecution);
}
}
which is where, after execution context creatin, the execution context is added to. Hopefully this helps you in your implementation.
public class ControlMJobLauncher implements JobLauncher, InitializingBean {
private JobRepository jobRepository;
private TaskExecutor taskExecutor;
private SimpleJobLauncher simpleLauncher;
private JobFilter jobFilter;
public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
/**
* Optional filter to prevent job launching based on some specific criteria.
* Jobs that are filtered out will return success to ControlM, but will not run
*/
public void setJobFilter(JobFilter jobFilter) {
this.jobFilter = jobFilter;
}
public JobExecution run(final Job job, final JobParameters jobParameters, ExecutionContextContributor contributor)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobFilteredException {
Assert.notNull(job, "The Job must not be null.");
Assert.notNull(jobParameters, "The JobParameters must not be null.");
//See if job is filtered
if(this.jobFilter != null && !jobFilter.launchJob(job, jobParameters)) {
throw new JobFilteredException(String.format("Job has been filtered by the filter: %s", jobFilter.getFilterName()));
}
final JobExecution jobExecution;
JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
if (lastExecution != null) {
if (!job.isRestartable()) {
throw new JobRestartException("JobInstance already exists and is not restartable");
}
logger.info(String.format("Restarting job %s instance %d", job.getName(), lastExecution.getId()));
}
// Check the validity of the parameters before doing creating anything
// in the repository...
job.getJobParametersValidator().validate(jobParameters);
/*
* There is a very small probability that a non-restartable job can be
* restarted, but only if another process or thread manages to launch
* <i>and</i> fail a job execution for this instance between the last
* assertion and the next method returning successfully.
*/
jobExecution = jobRepository.createJobExecution(job.getName(),
jobParameters);
if (contributor != null) {
if (contributor.contributeTo(jobExecution.getExecutionContext())) {
jobRepository.updateExecutionContext(jobExecution);
}
}
try {
taskExecutor.execute(new Runnable() {
public void run() {
try {
logger.info("Job: [" + job
+ "] launched with the following parameters: ["
+ jobParameters + "]");
job.execute(jobExecution);
logger.info("Job: ["
+ job
+ "] completed with the following parameters: ["
+ jobParameters
+ "] and the following status: ["
+ jobExecution.getStatus() + "]");
} catch (Throwable t) {
logger.warn(
"Job: ["
+ job
+ "] failed unexpectedly and fatally with the following parameters: ["
+ jobParameters + "]", t);
rethrow(t);
}
}
private void rethrow(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
}
throw new IllegalStateException(t);
}
});
} catch (TaskRejectedException e) {
jobExecution.upgradeStatus(BatchStatus.FAILED);
if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
jobExecution.setExitStatus(ExitStatus.FAILED
.addExitDescription(e));
}
jobRepository.update(jobExecution);
}
return jobExecution;
}
static interface ExecutionContextContributor {
boolean CONTRIBUTED_SOMETHING = true;
boolean CONTRIBUTED_NOTHING = false;
/**
*
* #param executionContext
* #return true if the exeuctioncontext was contributed to
*/
public boolean contributeTo(ExecutionContext executionContext);
}
#Override
public void afterPropertiesSet() throws Exception {
Assert.state(jobRepository != null, "A JobRepository has not been set.");
if (taskExecutor == null) {
logger.info("No TaskExecutor has been set, defaulting to synchronous executor.");
taskExecutor = new SyncTaskExecutor();
}
this.simpleLauncher = new SimpleJobLauncher();
this.simpleLauncher.setJobRepository(jobRepository);
this.simpleLauncher.setTaskExecutor(taskExecutor);
this.simpleLauncher.afterPropertiesSet();
}
#Override
public JobExecution run(Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
return simpleLauncher.run(job, jobParameters);
}
}
Starting from spring batch 2.2.x, there is support for non-identifying parameters. If you are using CommandLineJobRunner, you can specify non-identifying parameters with '-' prefix.
For example:
java org.springframework.batch.core.launch.support.CommandLineJobRunner file=/my/file/path -session=5678
If you are using old version of spring batch, you need to migrate your database schema. See 'Migrating to 2.x.x' section at http://docs.spring.io/spring-batch/getting-started.html.
This is the Jira page of the feature https://jira.springsource.org/browse/BATCH-1412, and here are the change that implement it https://fisheye.springsource.org/changelog/spring-batch?cs=557515df45c0f596588418d53c3f2bae3781c1c3
In more recent versions of Spring Batch (I am using spring-batch-core:4.3.3), you can use the JobParametersBuilder to specify whether a parameter is identifying or not. For example:
new JobParametersBuilder()
.addString("identifying-param-name", paramValue1)
.addString("non-identifying-param-name", paramValue2, false)
.toJobParameters();
The 'false' in the third argument makes the parameter non-identifying.

how to skip cron trigger firetime

I am transferring files from a directory on a remote host and a trigger fires jobs when interval arrives for this job.But i want be sure that if a job still working on a storage (downloading of files not yet finished) when trigger fire time arrives ,quartz going to skip this interval.I try to use this
c r on_trigger.MISFIRE_INSTRUCTION_DO_NOTHING but i seems it only for if there is no available thread for job.
public CronTrigger scheduleJob(RemoteJob job, String cronExpression,Date firstFireTime) throws SchedulerException, ParseException {
JobDetail jobDetail = new JobDetail(job.getDescription(), job.getName(), job.getClass());
CronTrigger crTrigger = new CronTrigger(
"cronTrigger", job.getName(), cronExpression);
scheduler.scheduleJob(jobDetail, crTrigger);
crTrigger.setStartTime(firstFireTime);
crTrigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
return crTrigger;
}
Create a TriggerListener that tracks if one of your download jobs is running, and and then return true for vetoing the execution of the other type of job.
I slightly modified the code above, it's worked.
/**
* not for cluster
*/
#Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
try {
List<JobExecutionContext> currentlyExecutingJobs = context.getScheduler().getCurrentlyExecutingJobs();
for (JobExecutionContext jobContext : currentlyExecutingJobs) {
if (jobContext.getTrigger().equals(trigger) &&
jobContext.getJobDetail().getKey().equals(trigger.getJobKey())) {
return true;
}
}
} catch (SchedulerException ex) {
return true;
}
return false;
}
I did as you say
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
try {
List<JobExecutionContext> jobs =
context.getScheduler().getCurrentlyExecutingJobs();
for (JobExecutionContext job : jobs) {
if (job.getTrigger().equals(context.getTrigger()) &&
!job.getJobInstance().equals(this)) {
_logger.info("There's another instance running,So job discarded " + context.getJobDetail().getGroup()+ ":"+context.getJobDetail().getName());
return true;
}
}
} catch (SchedulerException ex) {
return true;
}
return false;
}