I’m designing an application that uses UDP to receive datagram packets, handles them and allows administrator to view parsed data.
For this moment, I’ve created a single thread for receiving UDP packets. The thread is stored in ServletContext, started on ServletContextListener’s contextInitialized(). My next step should be parsing it, but as long as I want to use Spring’s services (@Service), I have no idea how to inject reference to specified services in receiver’s thread.
I know that I can get ServletContext’s reference in Service and set reference in receiver’s thread to created Service, but is it correct way to do this? (is there a better way?)
Thanks in advice.
I would design a service-like class that launches and manages the thread listening for UDP packets.
This service would then be started from your Spring context, which you can have loaded with
ContextLoaderListener.This way, you can develop the UDP layer in a way that has nothing to do with a web application – and make it possible to re-use it in a non-web context.
Starting the thread yourself in a listener and sticking it in the ServletContext sounds like a mix of concerns.