Mute Java servlet?

I have a standalone, headless java server application that does a bunch of processing based on a queue with a database that I am going to transfer to a Java application server. I have a lot of java functionality and a bit of JSP, but not a lot of servlet experience.

It seems like the approach is to just bind my application to servlets and deploy it at startup (and make sure it only deploys with one instance).

A few questions:

1) Since my application does not have an HTTP (or other) request / response mechanism, would it be foolish to implement a servlet that does not have URL mappings? If you look at the API, would I just implement a GenericServlet and just leave the service () method empty?

2) Another part of my java application opens / manages its own network sockets (non-HTTP) in order to receive an incoming data stream. I think it will take a lot of work to get it fit into the servlet request / response model. Is it normal for a servlet to open / manage its own network sockets?

3) We also have a bunch of web applications (currently in coldfusion) that are not very well integrated with the java application (in that they can only communicate via the database). We look at the rail (another servlet), and I'm trying to understand how easy it would be for coldfusion / railo applications (running on the same application server) to communicate directly with each other. Perhaps a web page displaying current statistics / environment metrics of the java engine and, ultimately, the invoking part of the business logic in the java engine.

Thanks Brian

+4
source share
2 answers
  • Servlets are a common mechanism that is not specifically tied to the HTTP world (it is constrained by the fact that HttpServlets are used in 99.999% of cases). You can subclass the Servlet class to implement, say, MailServlet, which will respond to mail events, but as far as I know, current web servers only support HTTP mappings.

  • Sockets belong to the Java EE world, and it is believed that Bad Thing runs user threads in this environment - and you will certainly need to do this if you open sockets (for polling data, etc.),

+2
source

If you do not want to intercept HTTP requests, just do not extend the HttpServlet . It makes no sense. If you really want to run it as a "background job" when you start webapp and stop it when you finish webapp, then just run 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 does not actually run the task in a separate thread, you need to wrap it in Runnable and execute it using the ExecutorService . For instance.

 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, in the end, your webapp has nothing , then I ask a question about its launch in the servletcontainer. Instead, simply run it as a stand-alone Java application using the main() method.

+2
source

Source: https://habr.com/ru/post/1304792/


All Articles