nakul
nakul

Reputation: 533

Job scheduler not invoke

JobDetail job = new JobDetail();
job.setName("dummyJ");
job.setJobClass(NotificationCreater.class);

SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("mn");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

i am using above code to schedule my activity in NotificationCreater.class but i get error message

error:-Unable to store Job with name: 'dummyJ' and group: 'DEFAULT', because one already exists with this identification.

Upvotes: 1

Views: 413

Answers (3)

Shailesh Pratapwar
Shailesh Pratapwar

Reputation: 4224

trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);

The trigger is being set for the indefinite repeat counts. Meaning, the trigger will be there in database forever. And as a result, the job associated with the trigger would also exist in database forever.

So, you executed your program for the first time and become glad to see it running. You stopped the execution and had a coffee break. You then come back and want to show this to your manager and @#$%@# BOOM #$%#$%#$5.

You trying to create the job and trigger with the name which are already in database. And scheduler will offcourse prevents you from doing this.

Solutions :

  1. Wipe out all the data from the quartz database tables before you start the next execution of program. OR
  2. Don't use an indefinite trigger . Use a simple one . A one time execution or two or three but not ~. OR
  3. Use RAMJobStore.

Upvotes: 0

Richie
Richie

Reputation: 9266

You can use the init method in Servlet to initialise and start of the schedule. You should also use the destroy method in Servlet to remove the scheduled job from the pool once you application is removed to avoid the same error happening during re-deployment. You can do something like scheduler.unscheduleJob() and scheduler.shutdown() to remove the job and stop the scheduler from destroy method.

Upvotes: 1

roger_that
roger_that

Reputation: 9791

If using servlets, and want to run your job on application startup, I guess this is how you should proceed to achieve.

The Job Class

public class DummyJob{

public DummyJob() throws ParseException, SchedulerException {

JobDetail job = new JobDetail();
job.setName("dummyJ");
job.setJobClass(NotificationCreater.class);

SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("mn");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
 }
}

The servlet

public class JobInitializerServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 5102955939315248840L;

/**
 * Application logger to log info, debug, error messages.
 */
private static final Logger APP_LOGGER = Logger.getLogger("appLogger");

/**
 * @see Servlet#init(ServletConfig) Initializes DummyJob
 */
public void init(ServletConfig config) throws ServletException {

    try {
        DummyJob scheduler = new DummyJob();
    } catch (java.text.ParseException e) {
        APP_LOGGER.error(e.getLocalizedMessage(), e);
    } catch (SchedulerException e) {
        APP_LOGGER.error(e.getLocalizedMessage(), e);
    }

}

}

And servlet Mapping

<servlet>
    <description>
    </description>
    <display-name>JobInitializerServlet</display-name>
    <servlet-name>JobInitializerServlet</servlet-name>
    <servlet-class>com.job.servlet.JobInitializerServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

This will initiate the job as soon as you deploy or start your application. Hope this helps.

Upvotes: 0

Related Questions