Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java ee background service

You'll have to excuse me if I'm describing this incorrectly, but essentially I'm trying to get a service-like class to be instantiated just once at server start and to sort of "exist" in the background until it is killed off at server stop. At least from what I can tell, this is not exactly the same as a typical servlet (though I may be wrong about this). What's even more important is that I need to also be able to access this service/object later down the line.

As an example, in another project I've worked on, we used the Spring Framework to accomplish something similar. Essentially, we used the configuration XML file along with the built-in annotations to let Spring know to instantiate instances of some of our services. Later down the line, we used the annotation @Autowired to sort of "grab" the object reference of this pre-instantiated service/object.

So, though it may seem against some of the major concepts of Java itself, I'm just trying to figure out how to reinvent this wheel here. I guess sometimes I feel like these big app frameworks do too much "black-box magic" behind the scenes that I'd really like to be able to fine-tune.

Thanks for any help and/or suggestions!


Oh and I'm trying to run this all from JBoss 6

like image 894
jerluc Avatar asked Feb 25 '23 13:02

jerluc


1 Answers

Here's one way to do it. Add a servlet context listener to your web.xml, e.g.:

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

Then create that class to manage your background service. In this example I use a single-threaded ScheduledExecutorService to schedule it to run every 5 minutes:

public class BackgroundServletContextListener implements ServletContextListener {
    private ScheduledExecutorService executor;
    private BackgroundService service;

    public void contextInitialized(ServletContextEvent sce) {
        service = new BackgroundService();

        // setup single thread to run background service every 5 minutes
        executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(service, 0, 5, TimeUnit.MINUTES);

        // make the background service available to the servlet context
        sce.getServletContext().setAttribute("service", service);
    }

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

public class BackgroundService implements Runnable {
    public void run() {
        // do your background processing here
    }
}

If you need to access the BackgroundService from web requests, you can access it through the ServletContext. E.g.:

ServletContext context = request.getSession().getServletContext();
BackgroundService service = (BackgroundService) context.getAttribute("service");
like image 50
WhiteFang34 Avatar answered Feb 27 '23 03:02

WhiteFang34