Parameterize EJB scheduler with Schedule Expression

后端 未结 1 1143
孤街浪徒
孤街浪徒 2020-12-19 21:39

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 -

相关标签:
1条回答
  • 2020-12-19 22:34

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题