1

In my Jakarta/Java EE (Tomcat) project I need to connect to a Serial device and communicate with it. To achieve this, I have to open the connection on server startup. From this point on, I can run part of the program (like different thread) continuously for communication purpose. This part of the program should therefore run independently of user requests, but it has to be able to access the communication part.

As a possible solution I have read that I could use the default main method, but in my case, it does not get executed.

[I can’t use two separate programs (one SE and one EE) because I need to access the variables/Objects.]

So, how to safely execute a function that can run continuously on server startup? (Please provide an example)

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Japhei
  • 88
  • 7
  • 1
    While I could imagine a few dozen ways to rig it and frameworks that help, I think this question is too broad to answer directly. I will point out that Jakarta/Tomcat have a specific purpose and it's not generally to run long-lived processes outside of user requests. You want to find other solutions better geared toward that. – Atmas Apr 09 '21 at 01:40

1 Answers1

3

The Jakarta Servlet specification defines a lifecycle for a web app. Implement the ServletContextListener in a class annotated @WebListener. You’ll find two hooks there, one for your web app starting and another for your web app ending. Start your executor service in one, close your executor service in the other.

You’ll find example code here on Stack Overflow if you look, such as this and this, Callback on Tomcat server startup complete.

Submit your task as a Runnable or Callable. For repeated runs, use ScheduledExecutorService.

Be sure to eventually shutdown the executor service. Otherwise its backing thread pool may continue indefinitely like a zombie ‍♂️.

Most of these steps are eliminated if using an app server that supports Jakarta Concurrency. Tomcat does not.

You said:

I could use the default main method

No, there is no main method in Web app.

I suggest you do some more study on the basics of web apps and Servlets.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
  • I did some research: Acewin pointed out that "In Java EE project you include 2 runtimes one is SE and 2nd is a EE container. You can create class with a main method. When you will execute it will run through java SE runtime." [link](https://stackoverflow.com/questions/66922687/how-can-i-execute-java-se-code-in-java-ee). Also Luiggi Mendoza "You can indeed create common console Java based applications with a public static void main(String[] args) that are handled by the application server" [link](https://stackoverflow.com/questions/28415660/main-method-in-a-java-application) – Japhei Apr 09 '21 at 06:41
  • Is there a way to detect a server restart/reload? Like you said the " thread pool may continue indefinitely like a zombie". I'm getting this `WARNING: The web application […] appears to have started a thread named […] but has failed to stop it.` As expected, everything is running twice. Is there a way to stop these threads on server reload? As I said they have to run continuously while the server is running, so I can’t kill them anytime. – Japhei Apr 14 '21 at 16:25
  • @Japhei You need to fix your problem at its root: leaving threads running. Did you look at the `ServletContextListener` Javadoc? You’ll find two hooks there, one for your web app starting and another for your web app ending. Start your executor service in one, close your executor service in the other. You’ll find example code here on Stack Overflow if you look. – Basil Bourque Apr 14 '21 at 17:46