0

I'm developing a web app in java ee and i want to update the weather every x amount of time. This is the code that i want to execute every 20 seconds but it does not start, can someone explain me why? I have to call it somewhere?

@Startup
@Singleton
public class ManageForecast implements Serializable{

private String urlQuery;
private String woeid;
private String cityHome;
private String tempHome;
private List<Location> place;
private Forecast forecast;

@PersistenceContext
EntityManager em;

@Schedule(second = "*/20", minute = "*", hour = "*")
public void updateForecast(){
    try {
        System.out.println("PARTITO!!!");
        place = em.createQuery("select l from Location l").getResultList();
        for (Location location : place) {
            forecast(location);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}
  • Did you check the Timerdatasource in your AppServer? Please name the application server, it might help to solve the problem. – teclis Jan 18 '15 at 14:38
  • I use Glassfish but I don't think it is the problem, because if I do a new project with the same configuration of mine and I write a class with a schedule it works! – Matteo Pasina Jan 18 '15 at 23:26

1 Answers1

0

It worked for me. I used GF3 with an Eclipse EJB-Project and the following code:

@Startup

@Singleton
public class ManageForecast implements Serializable{

//  private String urlQuery;
//  private String woeid;
//  private String cityHome;
//  private String tempHome;

@Schedule(second = "*/20", minute = "*", hour = "*")
public void updateForecast(){
    try {
        System.out.println("PARTITO!!!");
        //        place = em.createQuery("select l from Location l").getResultList();
        //        for (Location location : place) {
        //            forecast(location);
        //        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    }
}

Output:

2015-01-22T16:28:40.015+0100|Information: PARTITO!!! 2015-01-22T16:29:00.005+0100|Information: PARTITO!!!

teclis
  • 604
  • 1
  • 8
  • 19
  • I don't know why but it doesn't work in my project, I solved creating a new class with the schedule and calling the method from that class – Matteo Pasina Jan 25 '15 at 11:33