无头java servlet?

发布于 2024-08-26 11:29:57 字数 654 浏览 5 评论 0原文

我有一个独立的、无头的 java 服务器应用程序,它对数据库进行一系列基于队列的处理,我正在考虑将其迁移到 java 应用程序服务器中。我有丰富的后端 java 经验和一点 JSP 经验,但没有很多 servlet 经验。

看起来这种方法就是将我的应用程序包装到 servlet 中并在启动时部署它(并确保只部署一个实例)。

有几个问题:

1) 由于我的应用程序没有任何 HTTP(或其他)请求/响应机制,因此实现没有 URL 映射的 servlet 会很愚蠢吗?看看 API,我是否可以只实现 GenericServlet 并将 service() 方法留空?

2)我的java应用程序的另一部分打开/管理它自己的网络套接字(非HTTP)以接受传入数据流。我认为要使其适合 servlet 请求/响应模型需要做相当多的工作。 Servlet 可以打开/管理自己的网络套接字吗?

3)我们还有一堆Web应用程序(目前处于coldfusion中),它们与java应用程序集成得不是很好(因为它们只能通过DB进行通信)。我们正在研究railo(另一个servlet),我试图弄清楚coldfusion/railo 应用程序(在同一个应用程序服务器中运行)彼此直接通信是多么容易。也许是一个显示 java 引擎当前运行时统计/指标的网页,并最终调用 java 引擎中的一些业务逻辑。

谢谢, 布莱恩

I've got a stand alone, headless, java server app that does a bunch of queue-based processing against a database that I'm thinking of migrating into a java app server. I've got plenty of back-end java experience and a bit of JSP, but not a lot of servlet experience.

It seems like the approach would be to just wrap my app into a servlet and have it deploy on startup (and make sure that it only one instance gets deployed).

Few questions:

1) Since my app doesn't have any HTTP (or other) request/response mechanism, would it be silly to implement a servlet that has no URL mappings? Looking at the API, would I just implement a GenericServlet and just leave the service() method empty?

2) Another portion of my java app opens/manages it's own network sockets (non-HTTP) to accept a stream of incoming data. I think it would take quite a bit of work to get it to fit into the servlet request/response model. Is it ok that a servlet opens/manages its own network sockets?

3) We also have a bunch of web apps (currently in coldfusion) that are not very well integrated with the java app (in that they can only communicate via the DB). We are looking at railo (another servlet) and I'm trying to figure out how easy it would be for the coldfusion/railo apps (running in the same app server) to communicate directly with each other. Maybe a web page that displays current runtime stats/metrics of the java engine, and eventually invoking some of the business logic in the java engine as well.

Thanks,
Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-09-02 11:29:57
  1. Servlet 是一种通用机制,并不专门与 HTTP 世界相关(尽管 99.999% 的情况下都使用 HttpServlet)。您可以子类化 Servlet 类来实现,比如说,一个能够响应邮件事件的 MailServlet - 但据我所知,当前的 Web 服务器仅支持 HTTP 映射。

  2. 套接字属于 Java EE 世界,在这种环境中启动自定义线程被认为是一件坏事 - 如果您打开套接字(用于轮询数据等),那么您肯定需要这样做

  1. Servlets are a generic mechanism that are not specifically tied to the HTTP world (despites the fact that HttpServlets are used in 99.999% of cases). You could subclass the Servlet class to implement, say, a MailServlet that would respond to mail events - but as far as I know, current webservers support only HTTP mappings.

  2. Sockets belong to the Java EE world, and it is considered a Bad Thing to launch custom threads in this environment - and you most certainly would need to do that if you open sockets (for polling data, etc.)

卷耳 2024-09-02 11:29:57

如果您不想拦截 HTTP 请求,那么就不要扩展 HttpServlet。这毫无意义。如果您确实想在 web 应用程序启动时将其作为“后台任务”执行并在 web 应用程序关闭时停止它,那么只需相应地实现 ServletContextListener 即可。

public class Config implements ServletContextListener {

    private YourApp yourApp;

    public void contextInitialized(ServletContextEvent event) {
        yourApp = new YourApp();
        yourApp.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        yourApp.stop();
    }

}

您可以在 web.xml 中注册,如下所示:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

如果 YourApp 实际上没有在单独的线程中触发任务,那么您需要将其包装在可运行并使用 ExecutorService。例如

public class Config implements ServletContextListener {

    private ExecutorService executor;

    public void contextInitialized(ServletContextEvent event) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new YourApp()); // YourApp should implement Runnable.
    }

    public void contextDestroyed(ServletContextEvent event) {
        executor.shutdown();
    }

}

,如果您的 Web 应用程序除此之外什么都不做,那么我会质疑在 servlet 容器中运行它的价值。相反,只需使用 main() 方法将其作为独立的 Java 应用程序运行。

If you don't want to intercept on HTTP requests, then just don't extend HttpServlet. This makes no sense. If you actually want to execute it as a "background task" on webapp's startup and stop it on webapp's shutdown, then just implement ServletContextListener accordingly.

public class Config implements ServletContextListener {

    private YourApp yourApp;

    public void contextInitialized(ServletContextEvent event) {
        yourApp = new YourApp();
        yourApp.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        yourApp.stop();
    }

}

which you can register in web.xml as follows:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

If YourApp actually doesn't fire the task in a separate thread, then you'll need to wrap it up in a Runnable and execute it using ExecutorService. E.g.

public class Config implements ServletContextListener {

    private ExecutorService executor;

    public void contextInitialized(ServletContextEvent event) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new YourApp()); // YourApp should implement Runnable.
    }

    public void contextDestroyed(ServletContextEvent event) {
        executor.shutdown();
    }

}

If, after all, your webapp does nothing else than that, then I question the value of running it in a servletcontainer. Instead rather just run it as a standalone Java application using the main() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文