3

I am using EJB 3.1 and jboss-eap-6.4 and I want to set some dynamic parameters for hour, minute and second of ejb scheduler as follows:

Non-parametric code - which run in 30th second of every 5 minutes :

@Singleton
@Startup
public class TriggerJob {
    @EJB
   //some db injections           

    @PostConstruct
    public void onStartup() {
        try {
            preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @Schedule(second = "30", minute = "*/5", hour = "*", persistent = false)
    public void preparation() {
    //my scheduled tasks
    }
}

The above code executes properly.

Dynamic Parametric code - which should run in 30th second of every 5 minutes:

@Singleton
@Startup
public class TriggerJob {

    @EJB
    //some injections

    private boolean runningFlag = false;

    @Resource
    private TimerService timerService;

    public void setTimerService(TimerService timerService) {
        this.timerService = timerService;
    }

    @Timeout
    public void timerTimeout() {
        try {
            preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @PostConstruct
    private void postCunstruct() {
        timerService.createCalendarTimer(createSchedule(),new TimerConfig("EJB timer service timeout at ",false));
    }

    private ScheduleExpression createSchedule() {
        ScheduleExpression expression = new ScheduleExpression();
        expression.hour("*")
                .minute("*/5")
                .second("30");
        return expression;
    }

    public void preparation(){
    // my scheduled tasks
    }
}

The above code does not execute correctly, usually it executes multiple times at a second.

Also, I have read some other questions which did not help me:

Dynamic parameters for @Schedule method in an EJB 3.x

Using the Timer Service - The Java EE 6 Tutorial

Any help would be appreciated.

Community
  • 1
  • 1
Hosein Aqajani
  • 1,553
  • 4
  • 26
  • 46
  • Can you debug & verify 'Timer' object returned by 'createCalendarTimer', it contains ScheduleExpression & compare whether it's created as required or not. Additionally check if no other timers are active. – Nayan Wadekar Feb 28 '17 at 05:22
  • Dear @Nayan Wadekar the multiplying run is solved by the answer, however a new issue occurs: the scheduler is not perform at startup – Hosein Aqajani Mar 01 '17 at 05:59
  • 1
    Don't think there is any issue with the given code. Did you tried debugging, whether expression is correct, timeout method is called correctly, postconstruct etc. – Nayan Wadekar Mar 01 '17 at 17:23

1 Answers1

2

Instead, use programmatic scheduling, here is an exmaple :

@Singleton
@Startup
public class TriggerJob{

    @EJB
    //some injections

    @Resource
    private TimerService timerService;

    @PostConstruct
    public void init() {
        createTimer();
        //the following code resolve my startup problem
        try {
        preparation();
        } catch (CertificateVerificationException e) {
            e.printStackTrace();
        }
    }

    @Timeout
    public void timerTimeout() {
        try {
        preparation();
        } catch (CertificateVerificationException e) {
        e.printStackTrace();
        }
    }

    private void createTimer() {
        ScheduleExpression scheduleExpression = new ScheduleExpression();
        scheduleExpression.second("30").minute("*/5").hour("*");
        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setPersistent(false);
        timerService.createCalendarTimer(scheduleExpression, timerConfig);
        }

    public void preparation(){
        // my scheduled tasks
    }
}
Hosein Aqajani
  • 1,553
  • 4
  • 26
  • 46
javadev
  • 1,639
  • 2
  • 17
  • 35
  • Thanks Dear @javadev , but there are some misunderstanding issues in your code: 1- `PropertiesLoader` is unknown class, 2- `constructCronsMap` is undefined parameter, 3- where should I define `SECOND`, `MINUTE` and `HOUR`? , 4- your solution is not full-parametric because you set a number (30) for AccessTimeout annotation; Is there any full parametric approach? – Hosein Aqajani Feb 20 '17 at 07:09
  • 1
    entitiesUpdater is called on schedulerexpression bases cron expression, the 30 is only the timeOut (when a large transaction occurs), so foget about the comment (I removed it btw). PropertiesLoader is my own class (I'm loading these properties from a properties file, MINUTE, SECOND and DAY_OF_WEEK are properties keys btw). You can configure your own parameters by those you have (I assume you are getting these params from DB). – javadev Feb 20 '17 at 12:24
  • You right, however there is a problem: the @Startup annotation is not perform at all. I mean the scheduler does not call at beginning. Also, I have another request: if it is possible (I really appreciate) revise your answer (especially the code) to be useful in general for other users (I mean do not depend on your class). – Hosein Aqajani Feb 20 '17 at 14:12
  • EDITED : entitiesUpdater should now run every day at noon. You can change noon by any param you specify. – javadev Feb 20 '17 at 15:06
  • Try to construct once the EntitiesScheduler. It would then run every day by itself. – javadev Feb 20 '17 at 15:07
  • Dear @javadev , I have mentioned in the last comment that: the code does not load at startup; Do you have this problem? What should I do? – Hosein Aqajani Feb 21 '17 at 11:50
  • Make sure you have at least one reference to the EntitiesScheduler ejb, like having @EJB EntitiesScheduler in a class you know it is constructed. Hope that helps. – javadev Feb 21 '17 at 11:52