In my previous post here I demonstrated how to register a quartz job and trigger it manually. Now it’s time to move ahead and schedule quartz jobs with cron expression. Like I had created a generic class for manual jobs let’s create one for schedule jobs. Here’s the source:

public class ScheduledJob {
    public static final Logger log = Logger.getLogger(ScheduledJob.class.getName());

    private String name;
    private String groupName;
    private JobDetail jobDetail;
    private JobDataMap jobDataMap = new JobDataMap();
    private String cronExpression;
    private Trigger trigger;

    public ScheduledJob(String firstName, String groupName,Class<? extends Job> clazz,
                         String cronExpression, Map<? extends String,?> jobDataMap){ = firstName;
        this.groupName = groupName;
        this.cronExpression = cronExpression ;
        if (jobDataMap != null)this.jobDataMap.putAll(jobDataMap);
        this.jobDetail = buildJob(firstName, groupName, clazz);
        this.trigger = this.buildTrigger();

    public ScheduledJob(String firstName, String groupName,Class<? extends Job> clazz, String cronExpression){
        this(firstName, groupName, clazz, cronExpression, null);

    private JobDetail buildJob(String firstName, String lastName, Class<? extends Job> clazz){
        return JobBuilder
                .withIdentity(JobKey.jobKey(firstName, lastName))

    private Trigger buildTrigger(){
        return TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey(this.getName(),this.getGroupName()))

    public String getName() {

    public String getGroupName() {
        return this.groupName;

    public JobDetail getJobDetail() {
        return this.jobDetail;

    public JobKey getKey(){
        return JobKey.jobKey(, this.groupName);

    public Trigger getTrigger(){
        return this.trigger;

That’s the job itself but it still doesn’t know what block does it need to execute when it will be triggered. Like in the previous post. We’ll need to create the class that implements Job interface and overrides the execute method of it like:

public class JobExecutor implements Job{
    public static final Logger log = Logger.getLogger(Loggable.class.getName());
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        StringBuffer dataString = new StringBuffer();
        for(Map.Entry<String,Object> e:jobExecutionContext.getMergedJobDataMap().entrySet()){
            dataString.append(e.getKey()).append(" : ").append(e.getValue()).append(" || ");
        }"============JOB RUNNING with data============ "+dataString.toString());


Now, let’s start scheduling the jobs.

public static void main(String... args) throws Exception{
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("username", "prashant");
        ScheduledJob job3 = new ScheduledJob("job1","scheduled",JobExecutor.class,"*/6 * * * * ?",dataMap);
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();"Starting the Scheduler... ");
        scheduler.start();"Scheduler started... ");
        dataMap.put("app", "sample");"-----------------"+job3.getTrigger());

And that’s all to it.