This discussion is archived
13 Replies Latest reply: Jan 17, 2013 6:20 PM by 985432 RSS

Cron Job

985432 Newbie
Currently Being Moderated
Good Day Experts,

We have EJB JSF application. I need to create a scheduler, where the application will generate a report based on what customer defined(saved in database). For example, customer define to generate a report every 3pm. Or a customer can define to generate a report for them every Friday 7am.

Any idea on How can I implent this? given that we use EJB JSF running in JBOSS 7.

Regards
  • 1. Re: Cron Job
    r035198x Pro
    Currently Being Moderated
    You can create timers programmatically based on the user's input
    @Resource 
    TimerService timerService;
    
    //........
    ScheduleExpression expression = new ScheduleExpression(); 
    expression.second("*/1").minute("*").hour("*"); timerService.createCalendarTimer(expression);
    The only thing to think about is how best to get the time from the user and convert that input to valid API ScheduleExpression calls. This is usually based on the user's knowledge, the best case scenario being that they can understand cron expressions.
  • 2. Re: Cron Job
    Kayaman Guru
    Currently Being Moderated
    user2000609 wrote:
    Any idea on How can I implent this? given that we use EJB JSF running in JBOSS 7.
    I'd start by reading the documentation of whichever scheduler you decide to use.
  • 3. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    You mean in this code, it will run every second to check the database whether a user set a schedule to generate a report?
  • 4. Re: Cron Job
    Kayaman Guru
    Currently Being Moderated
    user2000609 wrote:
    You mean in this code, it will run every second to check the database whether a user set a schedule to generate a report?
    That would be a stupid polling implementation. He meant to create the scheduler with the user's input. No database checking involved (although you might want to save the schedulings in the DB for restart purposes).
  • 5. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    Yes, the scheduling will be save in the DB, per customer... Any suggestions? For example, Customer A has a schedule that was saved into DB, that the generation of their report is everyday 3pm.

    Regards
  • 6. Re: Cron Job
    r035198x Pro
    Currently Being Moderated
    You can create the timers when the application starts using code similar to what I posted above. Hopefully you don't have millions of customers with different run times.
  • 7. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    You mean when the application starts, it will have a timer to query database and check whether there is current schedule that will generate report? Please explain further if im wrong.

    Thank you
  • 8. Re: Cron Job
    r035198x Pro
    Currently Being Moderated
    No don't think about everything together as one process. Think about the parts in isolation.
    One approach is

    1.) You need a way to get user input and store into into a database as cron expressions together with the customerId associated with that expression. This has nothing to do with running the report.
    2.) You need to create the logic for running the report given a customerId. This could be an ejb method. It has nothing to do with timers or cron.
    3.) You need to be able to read the cron expressions for customers and create programmatic timers for them. Set the customerId as part of the timer info when you schedule the timer.
    4.) The timeout method then just reads the customerId from the Timer object's info and calls the process that generates the report (2)
    Make sure to read: http://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html before starting any coding
  • 9. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    I'm just confused with number 3.

    How can I trigger to read the cron expression of all the customer in the database?
  • 10. Re: Cron Job
    r035198x Pro
    Currently Being Moderated
    You are not triggering them. The container will trigger them. You are creating the timers (scheduling them to be triggered later by the container) You only need to do that once when the application starts up so look at using ejb 3.1@Startup (read about it from the docs)
  • 11. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    If the application starts up, then a new customer setup a schedule generation. Do the application need to start up again?
  • 12. Re: Cron Job
    r035198x Pro
    Currently Being Moderated
    user2000609 wrote:
    If the application starts up, then a new customer setup a schedule generation. Do the application need to start up again?
    No, schedule the timer for that customer as well after adding their cron to the database.
  • 13. Re: Cron Job
    985432 Newbie
    Currently Being Moderated
    Thanks a lot expert!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points