Push in Vaadin 7 app(“@ Push”)的最小示例 [英] Minimal example of Push in Vaadin 7 app ("@Push")
问题描述
我希望看到使用新而不是一个人睡觉的 线程
对象来安排我们的任务(告知时间)。相关提示:切勿在Servlet环境中使用 Timer
。有关更全面,更真实的示例,请参阅我的答案,了解有关Push with Vaadin的类似问题。
我在这个例子中采用了其他快捷方式,例如:我将 Label
小部件直接放在 UI上
而实际工作会使用布局
来包含标签
。
我的配置
我的代码在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技术相对较新,尤其是 WebSocket 品种。请务必使用最新版本的Web服务器,例如Tomcat 7或8的最新更新。
如何使用此示例
此代码是单个文件, MyUI.java
文件。要使用此代码:
- 在您选择的IDE中创建一个新的默认Vaadin应用程序。
- 在修改之前,让该示例成功运行。
- 使用以下代码替换
MyUI
类的内容。
@Push
注释
除此之外中间的代码,请注意我们如何添加 @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。这个源代码可以被任何人免除任何和所有责任,永远自由使用。
*
* + ---------------------------- +
* |不适合生产使用! |
* + ---------------------------- +
*睡眠线程是管理预定背景的一种尴尬方式工作。
*顺便说一句,永远不要在Servlet环境中使用'Timer'。
*改为使用Executor,可能是ScheduledExecutorService。
* /
@Push
@Theme(mytheme)
@Widgetset(com.example.pushvaadinapp.MyAppWidgetset)
公共类MyUI扩展UI
{
标签标签=新标签(现在:);
@Override
protected void init(VaadinRequest vaadinRequest)
{
//在此UI上放置一个小部件。在实际工作中,我们将使用布局。
setContent(this.label);
//启动数据Feed线程
new FeederThread()。start();
}
@WebServlet(urlPatterns =/ *,name =MyUIServlet,asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class,productionMode = false)
公共静态类MyUIServlet扩展VaadinServlet
{
}
public void tellTime()
{
label.setValue(Now: + new java.util.Date()); //如果是Java 8,请使用:Instant.now()。或者,在Joda-Time:DateTime.now()中。
}
类FeederThread扩展线程
{
int count = 0;
@Override
public void run()
{
try {
//更新数据
while(count< ; 100){
Thread.sleep(1000);
//在UI对象上调用特殊的访问方法,用于线程间通信。
access(new Runnable()
{
@Override
public void run()
{
count ++;
tellTime() ;
}
});
}
//通知我们已经停止运行
//在UI对象上调用特殊的'access'方法,用于线程间通信。
access(new Runnable()
{
@Override
public void run()
{
label.setValue(Done。);
}
});
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
I want to see the most minimal example of using the new Push technology in Vaadin 7, such as the new @Push
annotation.
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.
Simplification Of Example In The Book Of Vaadin
The Book Of Vaadin includes a chapter on Push, including an example using 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. The Label updates every second or so to tell you the current time.
For Example Use Only – Use an Executor in real-world project
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. In a real-world project I would use a ScheduledExecutorService rather than a single sleeping Thread
object to schedule our task (of telling time). Related tip: Never use a Timer
in a Servlet environment. For a fuller and more real-world example, see my Answer to a similar Question about Push with 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
.
My Configuration
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).
Push technology is relatively new, especially the WebSocket variety. Be sure to use recent versions of your web server, such as recent updates to Tomcat 7 or 8.
How To Use This Example
This code is a single file, the MyUI.java
file. To use this code:
- Create a new default Vaadin app in your IDE of choice.
- Get that example running successfully, before modifying.
- Replace the contents of
MyUI
class with the code below.
@Push
Annotation
Beside the code in the middle, note how we added the @Push
annotation to the MyUI
class definition.
Example Code
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();
}
}
}
}
这篇关于Push in Vaadin 7 app(“@ Push”)的最小示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!