2

I'm trying to implement a daily scheduled task in java, to work with glassfish, it is that every day actualize the records of the database, is for an application of collections, therefore balances and interest They must be updated daily. I'm using:

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

I have seen examples with Maven, so I have not clear yet, if anyone implemented and can give me a hand I be grateful

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
John Lopez
  • 127
  • 1
  • 9

2 Answers2

2

Finally I succeeded, apparently had problems with glassfish, took him away and I went back to add. But the finally code is

@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");
}

This code is in a post from @BalusC JSF managed bean for scheduled tasks using a timer

John Lopez
  • 127
  • 1
  • 9
1

Your question is not very clear. However, reading in between the lines one could roughly come up with the following example using the @Schedule annotation:

//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();
}

this method is defined inside a @Stateless session bean (or EJB) object, and gets executed everyday at 17:00 hours. The application has been deployed in GlassFish 4.0 as a .war file and has been created using JSF 2.2 Framework

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Charles
  • 320
  • 2
  • 8
  • Thanks for the quick response and comment in stateles as bean, that kind has to be the bean, Session, view, request? you have to add something more to web.xml ?? – John Lopez Jun 20 '16 at 15:40
  • I tried but I have this error: `Severe: Exception while loading the app Severe: Undeployment failed for context /WebApplication3 Warning: EJB Timer Service is not available. Timers for application with id 96104569243631616 will not be deleted Severe: Exception while loading the app : EJB Timer Service is not available` – John Lopez Jun 20 '16 at 16:17
  • be sure to:import javax.ejb.Schedule; import javax.ejb.Stateless; – Charles Jun 23 '16 at 11:21