-5

Sorry if asking sth dumb but I'm completly beginner. I'm trying to write simple web app using java webapp archetype (servlets/maven). In app clients will be sending some data to server, then that data will be precessed on server side and send back to clients after constant interval. My problem is that I don't know how to create method that runs continously on server side (something like main class). Is it even possible to create sth like that?

1 Answers1

-1

At a low level, this is what a server does: it listens on a port for client requests and when a request arrives, the server processes it. But if you want to build a web application, you don't have to work at this level, but at a much higher level: you run your code in a web server like Tomcat or Netty, that handles all the low level work, and just invokes your code when a client connects to it. So I would advise you to first choose a server and follow a tutorial on how to run a web app, for example read the Netty docs.

If you want to have some background threads that do work independently of the client requests, you can register a ServletContextListener implementation, that let you do work when the context is initialized (your application starts) and destroyed (your application ends). For example see the tutorial at https://mkyong.com/servlet/what-is-listener-servletcontextlistener-example/

JP Moresmau
  • 7,185
  • 13
  • 30
  • Thanks for answer. I'm already using tomcat and client-server communication works (http requests). But I also want some part of code that is called independent from clients requests. I'd like to call a method every 10s that will process data sent by all clients during that time. And I don't how to configure it. – Beginner Apr 24 '21 at 15:04
  • OK, look up ServletContextListener. Added in my answer. – JP Moresmau Apr 24 '21 at 15:11