@Schedule in Java EE doesn't start

一个人想着一个人 提交于 2019-12-12 01:53:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!