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 5957473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:24:20+00:00 2026-05-22T18:24:20+00:00

I could not find a definitive answer to whether it is safe to spawn

  • 0

I could not find a definitive answer to whether it is safe to spawn threads within session-scoped JSF managed beans. The thread needs to call methods on the stateless EJB instance (that was dependency-injected to the managed bean).

The background is that we have a report that takes a long time to generate. This caused the HTTP request to time-out due to server settings we can’t change. So the idea is to start a new thread and let it generate the report and to temporarily store it. In the meantime the JSF page shows a progress bar, polls the managed bean till the generation is complete and then makes a second request to download the stored report. This seems to work, but I would like to be sure what I’m doing is not a hack.

  • 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-22T18:24:20+00:00Added an answer on May 22, 2026 at 6:24 pm

    Introduction

    Spawning threads from within a session scoped managed bean is not necessarily a hack as long as it does the job you want. But spawning threads at its own needs to be done with extreme care. The code should not be written that way that a single user can for example spawn an unlimited amount of threads per session and/or that the threads continue running even after the session get destroyed. It would blow up your application sooner or later.

    The code needs to be written that way that you can ensure that an user can for example never spawn more than one background thread per session and that the thread is guaranteed to get interrupted whenever the session get destroyed. For multiple tasks within a session you need to queue the tasks.
    Also, all those threads should preferably be served by a common thread pool so that you can put a limit on the total amount of spawned threads at application level.

    Managing threads is thus a very delicate task. That’s why you’d better use the built-in facilities rather than homegrowing your own with new Thread() and friends. The average Java EE application server offers a container managed thread pool which you can utilize via among others EJB’s @Asynchronous and @Schedule. To be container independent (read: Tomcat-friendly), you can also use the Java 1.5’s Util Concurrent ExecutorService and ScheduledExecutorService for this.

    Below examples assume Java EE 6+ with EJB.

    Fire and forget a task on form submit

    @Named
    @RequestScoped // Or @ViewScoped
    public class Bean {
    
        @EJB
        private SomeService someService;
    
        public void submit() {
            someService.asyncTask();
            // ... (this code will immediately continue without waiting)
        }
    
    }
    
    @Stateless
    public class SomeService {
    
        @Asynchronous
        public void asyncTask() {
            // ...
        }
    
    }
    

    Asynchronously fetch the model on page load

    @Named
    @RequestScoped // Or @ViewScoped
    public class Bean {
    
        private Future<List<Entity>> asyncEntities;
    
        @EJB
        private EntityService entityService;
    
        @PostConstruct
        public void init() {
            asyncEntities = entityService.asyncList();
            // ... (this code will immediately continue without waiting)
        }
    
        public List<Entity> getEntities() {
            try {
                return asyncEntities.get();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new FacesException(e);
            } catch (ExecutionException e) {
                throw new FacesException(e);
            }
        }
    }
    
    @Stateless
    public class EntityService {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        @Asynchronous
        public Future<List<Entity>> asyncList() {
            List<Entity> entities = entityManager
                .createQuery("SELECT e FROM Entity e", Entity.class)
                .getResultList();
            return new AsyncResult<>(entities);
        }
    
    }
    

    In case you’re using JSF utility library OmniFaces, this could be done even faster if you annotate the managed bean with @Eager.

    Schedule background jobs on application start

    @Singleton
    public class BackgroundJobManager {
    
        @Schedule(hour="0", minute="0", second="0", persistent=false)
        public void someDailyJob() {
            // ... (runs every start of day)
        }
    
        @Schedule(hour="*/1", minute="0", second="0", persistent=false)
        public void someHourlyJob() {
            // ... (runs every hour of day)
        }
    
        @Schedule(hour="*", minute="*/15", second="0", persistent=false)
        public void someQuarterlyJob() {
            // ... (runs every 15th minute of hour)
        }
    
        @Schedule(hour="*", minute="*", second="*/30", persistent=false)
        public void someHalfminutelyJob() {
            // ... (runs every 30th second of minute)
        }
    
    }
    

    Continuously update application wide model in background

    @Named
    @RequestScoped // Or @ViewScoped
    public class Bean {
    
        @EJB
        private SomeTop100Manager someTop100Manager;
    
        public List<Some> getSomeTop100() {
            return someTop100Manager.list();
        }
    
    }
    
    @Singleton
    @ConcurrencyManagement(BEAN)
    public class SomeTop100Manager {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        private List<Some> top100;
    
        @PostConstruct
        @Schedule(hour="*", minute="*/1", second="0", persistent=false)
        public void load() {
            top100 = entityManager
                .createNamedQuery("Some.top100", Some.class)
                .getResultList();
        }
    
        public List<Some> list() {
            return top100;
        }
    
    }
    

    See also:

    • Spawning threads in a JSF managed bean for scheduled tasks using a timer
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could not find a clear answer to this question elsewhere, so I'll try
I have been searching everywhere but I could not find an answer. I need
I have been looking for an answer to this and could not find it
sorry for my rather general question, but I could not find a definite answer
I have been googling around this issue, but could not find clear and definitive
I could not find any pointers on how to create a menubar icon on
I could not find the function that returns the name of the cell referenced.
After having a search around I could not find exactly what I want, I
I have research about an issue and could not find a solution for it
I searched a lot but could not find a way to dump table relations

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.