A managed bean is, in the end, a POJO. So, your problem boils down to how to use multiple threads to execute a similar task over different objects, so in the end is a Java problem rather than a JSF problem. For this, you have to use the ExecutorService
interface. A brief example to accomplish this:
@ManagedBean
@RequestScoped
public class SomeBean {
List<String> records;
//other fields, constructor, getters and setters...
public void foo() {
int poolSize = records.size();
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
for (final String record : records) {
executor.execute(new Runnable() {
public void run() {
//tasks to perform in the threads...
}
});
}
executor.shutdown();
}
}
Some notes to this example:
poolSize
value should not be records.size()
, I just used this as example about creating a thread per work to perform. Note that you should create as many threads your server support. The real value of this variable will depend on result of your tests. I would recommend using 10 as initial value and then change it to measure the performance results.
- You may create a class that implements
Runnable
interface that will hold the task you want to accomplish instead of creating an anonymous class.
Note that this is a possible solution. IMO it would be better sending the list of records to a component that will process them, for example via JMS call. Then the component will call your restful service and other things it must do to process the record.