【发布时间】:2016-11-28 03:27:25
【问题描述】:
我使用vaadin 作为我们应用程序的框架。我想在服务器上实现一个定时器,它会定期触发一个事件,但我希望它是一个系统范围的轮询线程,以便服务器上只有一个运行,并且所有UI 都在需要时使用该定时器.我意识到如果它在UI 中,那么每个UI 实例都会创建一个新线程。所以我不知道把它放在哪里。在扩展Vaadin Servlet 的类中,就像在servletInitialized 方法中一样?如果是这样,不确定如何将事件从那里转移到可以使用它的类中。
想做这样的事情
TimerTask tt = new TimerTask() {
@Override
public void run() {
try
{
System.out.println("fired event");
//get the event out of here to somewhere in the code that can use it
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
};
Timer t = new Timer(true);
t.scheduleAtFixedRate(tt, 0, 10000);
}
}
但想在应用启动时启动它的地方运行它,而不是在每个启动的实例中运行它
【问题讨论】:
-
请提供您迄今为止尝试过的内容,即您的代码,以便我们能够为您提供进一步的帮助。谢谢。
-
谢谢基思。我刚刚决定使用 UI 中的 pollListener 事件在轮询间隔触发事件。似乎工作正常