本文介绍了Java EE中的@Schedule无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个java ee的web应用程序,我想每x次更新天气。这是我想要每20秒执行一次的代码,但它没有启动,有人能解释一下为什么吗?我必须在某个地方调用它?

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

}


推荐答案

它对我有用。我将GF3与Eclipse EJB-Project和以下代码一起使用:

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

    }
}

输出:

这篇关于Java EE中的@Schedule无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:48