[英]Minimal example of Push in Vaadin 7 app (“@Push”)
I want to see the most minimal example of using the new Push technology in Vaadin 7, such as the new @Push
annotation. 我想看看在Vaadin 7中使用新Push技术的最小例子,例如新的@Push
注释。
I am having problems getting server-push to work in my app. 我在使用服务器推送在我的应用程序中工作时遇到问题。 I would like to try a simple example app before trying to fix my own app. 在尝试修复自己的应用之前,我想尝试一个简单的示例应用。
The Book Of Vaadin includes a chapter on Push, including an example using Vaadin Charts . Vaadin书中有一章关于Push,包括使用Vaadin Charts的例子。
Below is my code. 以下是我的代码。 While based on that Vaadin Charts example mentioned above, I simplified it by replacing the use of a Chart
object with a simple Label
object. 虽然基于上面提到的Vaadin Charts示例,我通过将Chart
对象替换为简单的Label
对象来简化它。 The Label updates every second or so to tell you the current time. 标签每秒钟更新一次,以告诉您当前时间。
Caveat: My example below is built for simplicity, not intended as production code . 警告:我的下面的例子是为了简单而构建的, 不是作为生产代码 。 Sleeping a thread is a crude and awkward way to manage scheduled threaded work. 睡眠线程是管理预定线程工作的原始和笨拙方式。 Java provides the Executor
facility for this kind of work. Java为这种工作提供了Executor
工具。 In a real-world project I would use a ScheduledExecutorService rather than a single sleeping Thread
object to schedule our task (of telling time). 在一个真实世界的项目中,我会使用ScheduledExecutorService而不是一个睡眠的Thread
对象来安排我们的任务(告诉时间)。 Related tip: Never use a Timer
in a Servlet environment. 相关提示:切勿在Servlet环境中使用Timer
。 For a fuller and more real-world example, see my Answer to a similar Question about Push with Vaadin. 有关更全面,更真实的示例,请参阅我对与Vaadin推送类似问题的回答 。
I took other shortcuts in this example, such as: I place the Label
widget directly on the UI
whereas real-world work would use a Layout
to contain the Label
. 我在这个例子中采用了其他快捷方式,例如:我将Label
小部件直接放在UI
上,而实际工作则使用Layout
来包含Label
。
My code is using Vaadin 7.3.7 with Java 8 Update 25 in NetBeans 8.0.2 and Tomcat 8.0.15 on Mac OS X 10.8.5 (Mountain Lion). 我的代码在NetBeans 8.0.2中使用Vaadin 7.3.7和Java 8 Update 25,在Mac OS X 10.8.5(Mountain Lion)上使用Tomcat 8.0.15。
Push technology is relatively new, especially the WebSocket variety. 推送技术相对较新,尤其是WebSocket的多样性。 Be sure to use recent versions of your web server, such as recent updates to Tomcat 7 or 8. 请务必使用最新版本的Web服务器,例如Tomcat 7或8的最新更新。
This code is a single file, the MyUI.java
file. 此代码是单个文件,即MyUI.java
文件。 To use this code: 要使用此代码:
MyUI
class with the code below. 使用以下代码替换MyUI
类的内容。 @Push
Annotation @Push
Annotation Beside the code in the middle, note how we added the @Push
annotation to the MyUI
class definition. 除了中间的代码,请注意我们如何将@Push
批注添加到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();
}
}
}
}
Here is a simple but full Vaadin 8 example that demonstrates how to use server push and Java EE messaging APIs to send messages between different UIs using the Broadcaster pattern described in Vaadin docs . 这是一个简单但完整的Vaadin 8示例,演示了如何使用服务器推送和Java EE消息传递API,使用Vaadin文档中描述的Broadcaster模式在不同的UI之间发送消息。 If you are not interested in messaging or broadcasting to other users, then look at ReceiveMessageUI only. 如果您对向其他用户发送消息或广播不感兴趣,请仅查看ReceiveMessageUI 。
In principle it all boils down to the following: 原则上,这一切归结为以下几点:
@Push
to enable server push (by default over a WebSocket connection) 使用@Push
注释Vaadin UI以启用服务器推送(默认情况下通过WebSocket连接) Wrap UI updates with access()
when accessing it from other threads, sending updates happens automatically by default: 从其他线程访问UI时,使用access()
包装UI更新,默认情况下会自动发送更新:
getUI().access(() -> layout.addComponent(new Label("Hello!")));
Use the Broadcaster pattern to publish messages to other users and to subscribe to their messages. 使用Broadcaster模式将消息发布给其他用户并订阅他们的消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.