Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:59:11+00:00 2026-05-25T19:59:11+00:00

I would like to know if it’s ok to use Timer inside application scoped

  • 0

I would like to know if it’s ok to use Timer inside application scoped beans.

Example, lets say that I want to create a timer task that sends out a bunch of emails to every registered member one time per day. I’m trying to use as much JSF as possible and I would like to know if this is acceptable (it sounders a bit weird, I know).

Until now I have used all of the above inside a ServletContextListener. (I don’t want to use any application server or cron job and I want to keep
the above things inside my web app.)

Is there a smart JSF way of doing this or should I stick with the old pattern?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:59:11+00:00Added an answer on May 25, 2026 at 7:59 pm

    Introduction

    As to spawning a thread from inside a JSF managed bean, it would only make sense if you want to be able to reference it in your views by #{managedBeanName} or in other managed beans by @ManagedProperty("#{managedBeanName}"). You should only make sure that you implement @PreDestroy to ensure that all those threads are shut down whenever the webapp is about to shutdown, like as you would do in contextDestroyed() method of ServletContextListener (yes, you did?). See also Is it safe to start a new thread in a JSF managed bean?

    Never use java.util.Timer in Java EE

    As to using java.util.Timer in a JSF managed bean, you should absolutely not use the old fashioned Timer, but the modern ScheduledExecutorService. The Timer has the following major problems which makes it unsuitable for use in a long running Java EE web application (quoted from Java Concurrency in Practice):

    • Timer is sensitive to changes in the system clock, ScheduledExecutorService isn’t.
    • Timer has only one execution thread, so long-running task can delay other tasks. ScheduledExecutorService can be configured with any number of threads.
    • Any runtime exceptions thrown in a TimerTask kill that one thread, thus making Timer dead, i.e. scheduled tasks will not run anymore. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want. Task which threw exception will be canceled, but other tasks will continue to run.

    Apart from the book quotes, I can think of more disadvantages:

    • If you forget to explicitly cancel() the Timer, then it keeps running after undeployment. So after a redeploy a new thread is created, doing the same job again. Etcetera. It has become a “fire and forget” by now and you can’t programmatically cancel it anymore. You’d basically need to shutdown and restart the whole server to clear out previous threads.

    • If the Timer thread is not marked as daemon thread, then it will block the webapp’s undeployment and server’s shutdown. You’d basically need to hard kill the server. The major disadvantage is that the webapp won’t be able to perform graceful cleanup via e.g. contextDestroyed() and @PreDestroy methods.

    EJB available? Use @Schedule

    If you target Java EE 6 or newer (e.g. JBoss AS, GlassFish, TomEE, etc and thus not a barebones JSP/Servlet container such as Tomcat), then use a @Singleton EJB with a @Schedule method instead. This way the container will worry itself about pooling and destroying threads via ScheduledExecutorService. All you need is then the following EJB:

    @Singleton
    public class BackgroundJobManager {
    
        @Schedule(hour="0", minute="0", second="0", persistent=false)
        public void someDailyJob() {
            // Do your job here which should run every start of day.
        }
    
        @Schedule(hour="*/1", minute="0", second="0", persistent=false)
        public void someHourlyJob() {
            // Do your job here which should run every hour of day.
        }
    
        @Schedule(hour="*", minute="*/15", second="0", persistent=false)
        public void someQuarterlyJob() {
            // Do your job here which should run every 15 minute of hour.
        }
    
    } 
    

    This is if necessary available in managed beans by @EJB:

    @EJB
    private BackgroundJobManager backgroundJobManager;
    

    EJB unavailable? Use ScheduledExecutorService

    Without EJB, you’d need to manually work with ScheduledExecutorService. The application scoped managed bean implementation would look something like this:

    @ManagedBean(eager=true)
    @ApplicationScoped
    public class BackgroundJobManager {
    
        private ScheduledExecutorService scheduler; 
    
        @PostConstruct
        public void init() {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(new SomeDailyJob(), 0, 1, TimeUnit.DAYS);
        }
    
        @PreDestroy
        public void destroy() {
            scheduler.shutdownNow();
        }
    
    }
    

    where the SomeDailyJob look like this:

    public class SomeDailyJob implements Runnable {
    
        @Override
        public void run() {
            // Do your job here.
        }
    
    }
    

    If you don’t need to reference it in the view or other managed beans at all, then better just use ServletContextListener to keep it decoupled from JSF.

    @WebListener
    public class BackgroundJobManager implements ServletContextListener {
    
        private ScheduledExecutorService scheduler;
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(new SomeDailyJob(), 0, 1, TimeUnit.DAYS);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            scheduler.shutdownNow();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what kind of tool you use for writing your
I would like know the limit of maximum number of rows that can be
I would like know how I could simplify(modify) the hSelecVarIndCallback function. I want to
I am creating an Ajax autocomplete application and would like know if there is
Would like to know if there is any resource on the web that conveniently
i would like know how to build a rss feed that is evergoing using
I would like to know how to use multiples clas in Java. I know
Would like to know when to use, This applies for what browser ? if
I would like know how to implement the Login Check example by comparing the
Would like to know which library (open source preferable) should I use to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.