示例的简化瓦丁之书
The 瓦丁之书 https://vaadin.com/book包括关于推送的一章,包括使用的示例瓦丁图表 https://vaadin.com/add-ons/charts.
下面是我的代码。基于上面提到的 Vaadin Charts 示例,我通过替换使用Chart http://demo.vaadin.com/javadoc/com.vaadin.addon/vaadin-charts/2.0.0/com/vaadin/addon/charts/Chart.html对象有一个简单的Label https://vaadin.com/api/com/vaadin/ui/Label.html目的。标签大约每秒更新一次,告诉您当前时间。
仅供示例使用 – 在实际项目中使用执行器
Caveat:我下面的示例是为了简单起见而构建的,不打算作为生产代码。休眠线程是管理预定线程工作的一种粗暴且笨拙的方法。 Java 提供了Executor
进行此类工作的设施。在现实世界的项目中我会使用预定执行服务 http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html而不是一个人睡觉Thread http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html反对安排你的任务(告诉时间)。相关于: 切勿使用Timer
在 Servlet 环境中。有关更完整、更真实的示例,请参阅我的答案 https://stackoverflow.com/a/27872769/642706关于 Push with Vaadin 的类似问题。
在这个例子中我采用了其他快捷方式,例如:我将Label
小部件直接在UI
而现实世界的工作会使用Layout
来包含Label
.
我的配置
我的代码在 Mac OS X 10.8.5 (Mountain Lion) 上的 NetBeans 8.0.2 和 Tomcat 8.0.15 中使用 Vaadin 7.3.7 和 Java 8 Update 25。
推送技术相对较新,尤其是WebSocket http://en.wikipedia.org/wiki/WebSocket种类。请务必使用最新版本的 Web 服务器,例如 Tomcat 7 或 8 的最新更新。
如何使用此示例
这段代码是一个单独的文件,MyUI.java
文件。要使用此代码:
- 在您选择的 IDE 中创建一个新的默认 Vaadin 应用程序。
- 在修改之前让该示例成功运行。
- 替换内容
MyUI
类与下面的代码。
@Push
注解
除了中间的代码之外,请注意我们如何添加@Push https://vaadin.com/api/com/vaadin/annotations/Push.html注释到MyUI
类定义。
示例代码
package com.example.pushvaadinapp;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import javax.servlet.annotation.WebServlet;
/**
* © 2014 Basil Bourque. This source code may be used freely forever by anyone absolving me of any and all responsibility.
*
* +----------------------------+
* | NOT FOR PRODUCTION USE! |
* +----------------------------+
* Sleeping threads is an awkward way to manage scheduled background work.
* By the way, never use a 'Timer' in a Servlet environment.
* Use an Executor instead, probably a ScheduledExecutorService.
*/
@Push
@Theme ( "mytheme" )
@Widgetset ( "com.example.pushvaadinapp.MyAppWidgetset" )
public class MyUI extends UI
{
Label label = new Label( "Now : " );
@Override
protected void init ( VaadinRequest vaadinRequest )
{
// Put a widget on this UI. In real work we would use a Layout.
setContent( this.label );
// Start the data feed thread
new FeederThread().start();
}
@WebServlet ( urlPatterns = "/*" , name = "MyUIServlet" , asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class , productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
}
public void tellTime ()
{
label.setValue( "Now : " + new java.util.Date() ); // If Java 8, use: Instant.now(). Or, in Joda-Time: DateTime.now().
}
class FeederThread extends Thread
{
int count = 0;
@Override
public void run ()
{
try {
// Update the data for a while
while ( count < 100 ) {
Thread.sleep( 1000 );
// Calling special 'access' method on UI object, for inter-thread communication.
access( new Runnable()
{
@Override
public void run ()
{
count ++;
tellTime();
}
} );
}
// Inform that we have stopped running
// Calling special 'access' method on UI object, for inter-thread communication.
access( new Runnable()
{
@Override
public void run ()
{
label.setValue( "Done." );
}
} );
} catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}
}