سؤال

i want to run the ejb timer every 2 hrs between 9-18 and btw mon-sat and on friday btw 9-14 and on sat 9-13

if it is possible that on friday timers run after ever 1 hr

how can i do this with ejb timer

@Singleton
@Startup
public class TimedBean{
    @Resource
    private TimerService service;

    @PostConstruct
    public void init(){
        ScheduleExpression exp=new ScheduleExpression();
        exp.hour("*")
            .minute("*")
            .second("*/30");
        service.createCalendarTimer(exp);
    }

    @Timeout
    public void timeOut(){
        System.out.println(new Date());
        System.out.println("time out");
    }

}

also note that according to me this code will run after every 30 sec , but problem is it is called 2-3 times after every 30 sec any help is appreciated

هل كانت مفيدة؟

المحلول

If I understand you correctly, you can create Timers to do all of those things:

ScheduleExpression exp;

exp = new ScheduleExpression().minute(0).second(0);
exp.dayOfWeek("mon-sat").hour("9,11,13,15,17");
service.createCalendarTime(exp);

exp = new ScheduleExpression().minute(0).second(0);
exp.dayOfWeek("fri").hour("9-14");
service.createCalendarTime(exp);

exp = new ScheduleExpression().minute(0).second(0);
exp.dayOfWeek("sat").hour("9,11,13");
service.createCalendarTime(exp);

There are some conflicts in the above timers, but that seems to be what you're asking for. You may want to stagger them by giving each a different minute of the hour; for instance, you may want the second to use minute(20) and the third to use minute(40).

Reference: ScheduleExpression documentation

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top