Glassfish Web部署后执行代码 [英] Execute code after Glassfish Web Deployment

查看:149
本文介绍了Glassfish Web部署后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Glassfish上运行一个java web服务。有一些初始化代码设置一些变量,并从Glassfish环境本身检索一些信息。
我在@WebService类中的静态初始化程序中拥有该代码,但是该代码似乎被调用太早,一旦部署了WebService端点即可运行,而我需要运行一次整个Web服务已成功部署。



我尝试将代码移动到WebService类的构造函数中,但是该代码仅在我进入测试者网页并运行时运行一些数据使网络方式运行。



在整个Web服务部署完成后,是否有任何方法设置一些初始化代码才能运行? p>

解决方案

选项1:在Glassfish中,您有生命周期模块



选项2:
您还可以编写 ServletContextLi上下文被加载时被触发:

  public class MyServlet implements ServletContextListener {

public void contextInitialized(ServletContextEvent e){
//实现代码
}

public void contextDestroyed(ServletContextEvent e){
//实现代码
}
}

参考:




I'm trying to run a java web service on Glassfish. There is some initialization code that sets a few variables and retrieves some information from the Glassfish environment itself. I have that code in a static initializer inside the @WebService class, however this code appears to be called too early, it gets run as soon as the WebService endpoint is deployed, whereas I need it to run once the whole web service is successfully deployed.

I tried moving the code into the constructor of the WebService class, however then that code was only run when I went into the Tester web page and sent some data to make the web methods run.

Is there any way to set up some initialization code to be run as soon as the whole web service deployment is completed?

解决方案

Option 1: In Glassfish you have the Lifecycle modules

Option 2: You also have the ability to code a ServletContextListener to be triggered when the context is loaded:

public class MyServlet implements ServletContextListener {

  public void contextInitialized(ServletContextEvent e) {
         // implementation code
  }

  public void contextDestroyed(ServletContextEvent e) {
         // implementation code
  }
}

Reference:

这篇关于Glassfish Web部署后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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