在glassfish / centos中安排工作的最佳方式 [英] Best way to schedule a job in glassfish/centos

查看:183
本文介绍了在glassfish / centos中安排工作的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每天运行一次,每天工作一次,并从不同的数据库获取一些数据。该任务被开发为EJB。我尝试了使用EJB的@Schedule,它工作正常。但问题是如果日程安排有变更,代码必须更改,应用程序才能重新部署。有没有办法避免这种情况?可能是使用配置文件等我在CentOS中使用JSF 2.2,glassfish3.4.2。

I need to run a daily job which will run once a day and get some data from a different database. The task is developed as an EJB. I tried the @Schedule with EJB, and it is working fine. But the issue is if there is a change in the schedule, the code has to be changed and the app to be redeployed. Is there a way to avoid this? May be by using configuration files etc. I am using JSF 2.2, glassfish3.4.2 in CentOS.

推荐答案

你可以这样做以编程方式创建你的计时器这样的

You can do this by creating your timer programmatically something like this

....
    private static final String TIMER_NAME = "SOME_TIMER_NAME";
....
    @Resource
    TimerService timerService;    
....
    public void createTimer(String days, String hours, String minutes) {
        removeTimer();
        ScheduleExpression scheduleExpression = new ScheduleExpression();       
        if (days != null) {           
            scheduleExpression.dayOfMonth(datys);
        }
        if (hours != null) {           
            scheduleExpression.hour(hours);
        }
        if (minutes != null) {           
            scheduleExpression.minute(minutes);
        }
        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setInfo(TIMER_NAME);
        Timer timer = timerService.createCalendarTimer(scheduleExpression, timerConfig);
    }


    private void removeTimer() {
        for (Timer timer : timerService.getTimers()) {
            if (TIMER_NAME.equals(timer.getInfo())) {
                timer.cancel();
            }
        }
    }

    @Timeout
    public void retrieveData() {
        // retrieve data from DBs
    }

现在调用这样的createTimer方法

now calling the createTimer method like this

createTimer("*", "3", null);

retrieveData()方法将在每天凌晨3:00执行。如果要更改计划,您应该使用适当的值调用该方法。您可以调整它以满足您的要求,如添加输入验证,不同的定义参数。您可能需要查看ScheduleExpression以找到最适合您的内容。我的代码只是显示一个想法。

retrieveData() method will be executed at 3:00 AM each day. If you want to change the schedule you should call the method with appropriate values. You may tune it to meet your requirements like add input validation, define parameters differently etc. You may want to look at ScheduleExpression to find what is best for you. My code just shows an idea.

这篇关于在glassfish / centos中安排工作的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆