java - Vaadin 7 应用程序中推送的最小示例 ("@Push")

标签 java vaadin vaadin7 web-push

我想看看使用新 Push technology 的最简单示例在 Vaadin 7 中,例如新的 @Push 注释。

我在获取 server-push 时遇到问题在我的应用程序中工作。在尝试修复我自己的应用程序之前,我想尝试一个简单的示例应用程序。

最佳答案

The Book Of Vaadin 示例的简化

Book Of Vaadin包括有关推送的一章,包括使用 Vaadin Charts 的示例.

下面是我的代码。基于上面提到的 Vaadin 图表示例,我通过替换 Chart 的使用来简化它。带有简单 Label 的对象目的。标签大约每秒更新一次,以告诉您当前时间。

screen shot of example Vaadin app telling current time in UTC as text

仅供示例使用——在实际项目中使用执行器

警告:我下面的示例是为简单起见而构建的,并非旨在作为生产代码。 hibernate 线程是一种管理计划的线程工作的粗糙而笨拙的方法。 Java 为这种工作提供了 Executor 工具。在实际项目中,我会使用 ScheduledExecutorService而不是一个人 sleep Thread反对安排我们的任务(告诉时间)。相关提示:切勿在 Servlet 环境中使用 Timer。有关更完整、更真实的示例,请参阅 my Answer关于使用 Vaadin 推送的类似问题。

我在此示例中采用了其他快捷方式,例如:我将 Label 小部件直接放在 UI 上,而实际工作将使用 Layout 以包含 Label

我的配置

我的代码在 NetBeans 8.0.2 中使用 Vaadin 7.3.7 和 Java 8 Update 25,在 Mac OS X 10.8.5 (Mountain Lion) 中使用 Tomcat 8.0.15。

推送技术相对较新,尤其是WebSocket种类。请务必使用最新版本的 Web 服务器,例如 Tomcat 7 或 8 的最新更新。

如何使用这个例子

此代码是单个文件,MyUI.java 文件。要使用此代码:

  1. 在您选择的 IDE 中创建一个新的默认 Vaadin 应用。
  2. 在修改之前让该示例成功运行。
  3. 用下面的代码替换 MyUI 类的内容。

@Push注解

在中间的代码旁边,请注意我们是如何添加 @PushMyUI 类定义的注释。

示例代码

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

关于java - Vaadin 7 应用程序中推送的最小示例 ("@Push"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27808460/

相关文章:

Javascript:保存和恢复浏览器窗口大小

vaadin - 如何在 Vaadin Flow 的 FormLayout 中添加空格?

spring - 当 session 未锁定时无法从父级中删除

java - Apache 与 vaadin7 共用电子邮件吗?

java - hibernate 问题

java - 将非常小的 double 转换为 String

java - Mule ESB - 如何在 Java 组件中处理来自网页的 JSON 请求

java - 创建签名的 bundle 版本。错误 : Android resource linking failed

javascript - 如何仅更改页面内 vaadin-select 中 vaadin-select-text-field 的颜色?

hover - 当用户将鼠标指针悬停在 Vaadin 7 中的网格或表格中的行上时,会发生 "mouse over"的事件吗?