Results 1 to 5 of 5
  1. #1
    Malathi is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default How to schedule a job in java

    Hi,

    I am new to this forum

    i want to schedule a job every morning by 9AM. How can i schedule a job in java in this manner. could anybody please suggest me with an example

    Regards,
    Malathi

  2. #2
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    If its a pure java use quartz api. Use quartz framework for spring which is more confortable than normal.

    Sample code in pure java... which runs every night at 12:00
    SchedulerService
    public class SchedulerService {
    public static void initiate() throws SchedulerException {

    try {
    SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

    Scheduler sched = schedFact.getScheduler();

    sched.start();

    JobDetail emailJob = new JobDetail("Emails",
    "G1", EndOfDayProcess.class);

    CronTrigger trigger1 = new CronTrigger();
    trigger1.setStartTime(new Date());
    trigger1.setCronExpression("0 5 0 * * ? *");
    trigger1.setName("File Changes");
    trigger1.setGroup("End of Day G1");
    sched.scheduleJob(emailJob, trigger1);

    } catch (Exception e) {
    e.printStackTrace();
    }

    System.out.println("SchedulerService is Called ");
    }
    }

    EndOfDayProcess
    public class EndOfDayProcess {
    public void execute() {
    // actual logic goes here
    }

    }

  3. #3
    Malathi is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default

    cant we do it using the java API itself. Because this is just a minor part that acts as a small service for our application.

    I want to do it using Java API itself. i heard about timer in java. can i know how i can do using timer with a sample example.

  4. #4
    faisalcmpm is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    public class Scheduler implements Runnable{
    public Scheduler() {
    Thread thread = new Thread(this);
    thread.start();
    }
    public static void main(String[] args) {
    new Scheduler();
    }
    public void run() {
    while(true) {
    Calendar calendar = Calendar.getInstance();
    if(calendar.get(Calendar.HOUR_OF_DAY) == 9) {
    //Do ur business here
    //Keep the thread sleep u can go for wither 1 Hr or 23 so that we can ensure that
    //job will run next day
    sleep(1);
    }
    }
    }
    /**
    *
    * @param minutes
    */
    private void sleep(int hours) {
    try {
    Thread.sleep(hours * 60 * 1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

  5. #5
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    Use this method:: public void schedule(TimerTask task,long delay,long period)
    task - task to be scheduled.
    delay - delay in milliseconds before task is to be executed.
    period - time in milliseconds between successive task executions.

    here is the example below
    Timer timer = new Timer("demo");
    timer.schedule(new TimerTask() {
    public void run() {
    //insert your code here
    }
    }, 200L, 1000L);

Similar Threads

  1. Replies: 0
    Last Post: 09-08-2008, 12:34 PM
  2. Replies: 0
    Last Post: 09-08-2008, 12:33 PM
  3. Replies: 1
    Last Post: 08-28-2008, 01:38 PM
  4. Replies: 0
    Last Post: 06-26-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •