问题
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();
}
}
回答1:
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!!!
来源:https://stackoverflow.com/questions/28003826/schedule-in-java-ee-doesnt-start