如何使用java和glassfish实现每日计划任务?

时间:2016-06-20 14:05:30

标签: java glassfish

我试图在java中实现每日计划任务,与glassfish一起工作,它是每天实现数据库的记录,是为了集合的应用程序,因此平衡和兴趣他们必须更新日常。我正在使用:

  • Glassfish 4
  • Java EE 7 Web
  • JSF 2.2
  • Hibernate 4.3.1

我见过Maven的例子,所以我还不清楚,如果有人实施并且可以帮我一臂之力我很感激

2 个答案:

答案 0 :(得分:2)

最后我成功了,显然有玻璃鱼的问题,带走了他,我回去补充。 但最终的代码是

@Singleton
@Startup

public class BackgroundJobManager {

@Schedule(hour="0", minute="0", second="0", persistent=false)
public void someDailyJob() {
    // Do your job here which should run every start of day.
    System.out.println("");
}

@Schedule(hour="*/1", minute="0", second="0", persistent=false)
public void someHourlyJob() {
    // Do your job here which should run every hour of day.
}

@Schedule(hour="*", minute="*/15", second="0", persistent=false)
public void someQuarterlyJob() {
    // Do your job here which should run every 15 minute of hour.
    System.out.println("Imprimir cada 15 mins");
}

@Schedule(hour="*", minute="*/1", second="0", persistent=false)
public void someMinuteJob() {
    // Do your job here which should run every minute of the hour.
    System.out.println("Imprimir cada 1 mins");
}

此代码位于@BalusC JSF managed bean for scheduled tasks using a timer

的帖子中

答案 1 :(得分:1)

你的问题不是很清楚。但是,使用@Schedule注释可以粗略地读出以下示例:

//change the statuses, from 'Executing...' to 'Uncomplete' every at 5pm 
@Schedule(hour = "17", persistent = false)
public void runEndOfDay() throws Exception {

    System.out.println("Tickets updated on - " + new Date());

    /*Set tickets to status 'Uncomplete' if Lodged without being completed until the end of business*/
    query = em.createQuery("update Ticket t set t.status = 'Uncompleted' where t.status = 'Executing...' or t.status = 'Paused...' or t.status = 'Missing...' or t.status = 'Lodged'");

    query.executeUpdate();
}

此方法在@Stateless会话bean(或EJB)对象中定义,并在每天17:00执行。该应用程序已作为.war文件部署在GlassFish 4.0中,并使用JSF 2.2 Framework

创建