开始和停止我的Vaadin网络应用程序的钩子? [英] Hook for my Vaadin web app starting and stopping?

查看:111
本文介绍了开始和停止我的Vaadin网络应用程序的钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道我的Vaadin 7网络应用程序何时首次启动/启动,所以我可以做一些初步的设置工作?

How do I know when my Vaadin 7 web app first starting/launching, so I can do some initial set-up work?

同样,我怎么知道当我的网络应用程序结束时,正在关闭/退出?

Likewise, how do I know when my web app is ending, getting shutdown/exiting?

推荐答案

ServletContextListener



Vaadin建立在 Java Servlet <之上/ a>技术。 上下文是Servlet术语中Web应用程序的技术术语。所以这里的答案不是Vaadin特有的,它适用于任何Servlet - 并且在一天结束时,Vaadin只是一个大Servlet。

ServletContextListener

Vaadin is built on top of Java Servlet technology. A "context" is the technical term for your web app in Servlet terms. So the answer here is not Vaadin-specific, it applies to any Servlet -- and at the end of the day, Vaadin is just one big Servlet.

自Servlet规范以来版本2.3, Servlet容器,例如 Tomcat Jetty 等等必须承诺关注您定义为实现 ServletContextListener 界面。该接口有两个简单的方法:

Since Servlet spec version 2.3, a Servlet container such as Tomcat, Jetty, etc. must promise to be on the lookout for any Java class you define as implementing the ServletContextListener interface. That interface has two simple methods:

  • One that gets called when your web first launches (contextInitialized)
  • One that gets called when your web app is ending (contextDestroyed).

结束可能是由于Servlet容器(例如:Tomcat)正在关闭所以所有的网络应用程序(上下文)正在结束,或者因为你的Vaadin应用程序的上下文正在结束(如果你的Servlet容器支持每个上下文关闭)。

The ending could be caused by the Servlet container (ex: Tomcat) is being shutdown so all the web apps ("contexts") are ending, or because just your Vaadin app’s context is ending (if your Servlet container supports per-context shutdown).

每个Servlet容器必须满足的合同是每个ServletContextListener类(可以有多个)必须具有 contextInitialized 在任何servlet或过滤器执行之前调用 。因此,这是进行初始化工作的最佳时机,这可能比单个Servlet请求 - 响应周期更有益。如果您需要启动数据库,例如 [H2数据库),这是一个好时机。如果将一些数据作为缓存加载到内存中以供servlet重复使用,那么现在是个好时机。例如,也是测试应用程序资源,确定日志记录工作或某些预期文件的好时机。

The contract every Servlet container must fulfill is that each of your ServletContextListener classes (you can have more than one) must have its contextInitialized invoked before any servlet or filter executes. So this is the perfect time to do initialization work that might benefit more than a single Servlet request-response cycle. If you need to startup a database such as [H2 Database), this is a good time. If you load some data into memory as a cache to be used by the servlet(s) repeatedly, this is a good time. Also a good time to test your apps resources, to be certain logging works or certain expected files are in place, for example.

同样,每个兼容的Servlet容器都会调用 contextDestroyed servlet和过滤器完成后的最后一次调用。因此,这是关闭数据库,进行备份或执行适合您的Web应用程序的任何其他清理工作的好地方。

Likewise, every compliant Servlet container invokes contextDestroyed only after the servlet(s) and filters have finished their last invocation. So this is a good place to shutdown your database, make backups, or do any other clean-up chore appropriate to your web app.

我们正在讨论生命周期您的网络应用程序的上下文。该上下文可能涉及一个或多个servlet。上下文的生命周期超出了参与的任何一个servlet的生命周期这个背景。上下文有点像女王蜂,她在一个新的蜂巢里生下了她所有的无人机,她生活在她们面前,她将在她们为她尽职尽责地死去的时候比她们更长寿(如果这是一个蜂巢有效吗?)。

We are discussing the life cycle of your web app’s "context". That context may involve one, or more than one, servlet. This life cycle of the context goes beyond the life cycle of any one of the servlets participating in this context. The context is kinda-sorta like the queen bee who gives birth to all her drones in a new hive, where she was living before them and she will outlive them all as they die off in dutiful service to her (if that is how a hive works?).

制作 ServletContextListener 非常简单:使用一对方法和一个注释创建一个类。

Making a ServletContextListener is quite easy: Make a class with a pair of methods plus an annotation.

添加一个新的Java类作为Vaadin应用程序的一部分。您可以根据需要为课程命名。

Add a new Java class as part of your Vaadin app. You can name the class anything you want.

我将我的上下文监听器添加到与我的主要Vaadin应用程序相同的包中 UI class( MyUI.java 可能是由您的Vaadin插件或Maven原型生成的)。看起来像一个自然的地方,因为上下文监听器是我的Vaadin应用程序在任何用户被处理之前启动的开始,而指定的 UI 类将是我的Vaadin应用程序的第一部分是为每个用户运行的。

I add my context listeners in the same package as my main Vaadin app UI class (MyUI.java may have been generated by your Vaadin plugin or by Maven archetype). Seems like a natural place as the context listener is the beginning of my Vaadin app launching before any user is handled while the designated UI class will then be the first piece of my Vaadin app being run for each user.

将您的类声明为实现 ServleContextListener 。添加上面讨论的两种必要方法;您的 IDE 可以帮助您完成这项工作。

Declare your class as implementing ServleContextListener. Add the two required methods discussed above; your IDE may assist with this chore.

还有一个技巧:您必须通知Servlet容器有关此上下文侦听器的信息。有不止一种方法可以做到这一点,但我使用最简单的注释 @WebListener

One more trick: You must inform the Servlet container about this context listener. There is more than one way to do this, but I use the simplest, an annotation @WebListener on the class.

这是一个完整的示例类。

Here is an entire example class.

package com.example.amazingapp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 *
 * @author Basil Bourque
 */
@WebListener
public class WebAppListener implements ServletContextListener {

    @Override
    public void contextInitialized ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is starting. " );
    }

    @Override
    public void contextDestroyed ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is shutting down." );
    }

}

这篇关于开始和停止我的Vaadin网络应用程序的钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆