使用嵌入式Jetty服务的GWT前端运行Java守护程序(Running a Java daemon with a GWT front

编程入门 行业动态 更新时间:2024-10-28 17:17:11
使用嵌入式Jetty服务的GWT前端运行Java守护程序(Running a Java daemon with a GWT front-end served by embedded Jetty)

问候,编码员,

背景信息和代码

我正在尝试创建一个守护程序类型的程序(例如,它不断运行,轮询要做的事情),由GWT应用程序(WAR中的servlet)管理,然后由嵌入式Jetty服务器提供服务(使用WebAppContext )。 我在使GWT应用程序知道守护进程对象时遇到问题。

为了测试,我目前有两个项目:守护进程和嵌入式Jetty服务器EmbJetTest ( EmbJetTest ),GWT应用程序EmbJetTest为另一个( DefaultApp )。 这是代码的当前状态:

首先, EmbJetTest创建一个嵌入式Jetty服务器,使用ServletContextListener将守护进程对象注入Web应用程序上下文:

EmbJetTest.server = new Server(8080); // Create and start the daemon Daemon daemon = new Daemon(); Thread thread = new Thread(daemon); thread.start(); // war handler WebAppContext waContext = new WebAppContext(); waContext.setContextPath("/webapp"); waContext.setWar("./apps/DefaultApp.war"); waContext.addEventListener(new DaemonLoader(daemon)); // Add it to the server EmbJetTest.server.setHandler(waContext); EmbJetTest.server.setThreadPool(new QueuedThreadPool(10)); // Start the server; join() blocks until we shut down EmbJetTest.server.start(); EmbJetTest.server.join(); // Stop the daemon thread daemon.stopLoop();

我的问题是双重的。

我甚至在这里正确的轨道? 我完全错误地走了这个路吗? 有些东西告诉我我,但我想不出另一种让守护进程可用于两个应用程序的方法。 有没有更好的方法与GWT应用程序中的守​​护进程通信? GWT应用程序应该拥有守护进程并以某种方式启动守护进程本身吗? 即使没有人访问GWT应用程序的servlet之一,守护进程也需要运行 - 我怎么能这样做? 如果我走在正确的轨道上,我怎样才能绕过类加载器问题?

提前致谢。

Greetings, coders,

Background Info and Code

I am trying to create a daemon-type program (e.g., it runs constantly, polling for things to do) that is managed by a GWT application (servlets in a WAR) which is in turn served by an embedded Jetty server (using a WebAppContext). I'm having problems making the GWT application aware of the daemon object.

For testing things, I currently have two projects: The daemon and embedded Jetty server in one (EmbJetTest), and the GWT application in another (DefaultApp). This is the current state of the code:

First, EmbJetTest creates an embedded Jetty server like so, using a ServletContextListener to inject the daemon object into the web application context:

EmbJetTest.server = new Server(8080); // Create and start the daemon Daemon daemon = new Daemon(); Thread thread = new Thread(daemon); thread.start(); // war handler WebAppContext waContext = new WebAppContext(); waContext.setContextPath("/webapp"); waContext.setWar("./apps/DefaultApp.war"); waContext.addEventListener(new DaemonLoader(daemon)); // Add it to the server EmbJetTest.server.setHandler(waContext); EmbJetTest.server.setThreadPool(new QueuedThreadPool(10)); // Start the server; join() blocks until we shut down EmbJetTest.server.start(); EmbJetTest.server.join(); // Stop the daemon thread daemon.stopLoop();

Question

My question is twofold.

Am I even on the right track here? Am I going about this completely the wrong way? Something tells me I am, but I can't think of another way to make the daemon available to both applications. Is there a better way to communicate with the daemon from the GWT application? Should the GWT app own the daemon and somehow start the daemon itself? The daemon needs to run even if no one visits the one of the GWT app's servlets--how could I do this? If I am on the right track, how can I get around the classloader issue?

Thanks in advance.

最满意答案

为什么不在GWT Web应用程序中的单独线程中运行守护程序的功能? 这样你就可以避免所有的类加载悲伤(正如你所发现的那样,不同的.war文件中的应用程序在他们自己的类加载器中运行)。

只需为您的守护进程和现有的servlet(在同一个.war )创建一个servlet,然后在servlet初始化和销毁​​时启动/停止该线程。

Why not just run the functionality of the daemon in a separate thread within the GWT web application ? That way you'll avoid all the classloading grief (as you've found out, apps in different .war files run within their own classloaders).

Just create a servlet for your daemon alongside your existing servlets (in the same .war), and start/stop the thread on servlet initialisation and destruction.

更多推荐

本文发布于:2023-08-03 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌入式   程序   GWT   Java   Jetty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!