Java EE 7+ Concurrency Utilities Example (managed thread executor) in JBoss EAP 7+
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 7+
Issue
- How to use the
JavaEE 7 Concurrency Utilitiesin JBoss EAP 7? - How to create a managed thread in JBoss EAP 7?
- Example of the managed thread executor , managed thread factory , managed scheduled thread executor in JBoss EAP 7
- We need to create threads from java code inside JBoss EAP. In Jboss EAP 6 we use asynchronous EJBs because there are not implementations of Work Managers outside resource adapters and the use of executors is disalowed. What alternatives do we have in JBoss EAP7?
- I want to know the different possibilities I have to create new threads on JBoss EAP7. Beside this, I want to know the recommended way to do parallel processing.
Resolution
Disclaimer: Links contained herein to an external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
Java EE 7 added the ManagedThreadFactory , which allows applications to inject a managed thread factory, executor service or scheduled executor service.
import javax.enterprise.concurrent.ManagedThreadFactory;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
private Thread thread = null;
@Resource
private ManagedThreadFactory managedThreadFactory;
@Resource
private ManagedExecutorService managedExecutorService;
@Resource
private ManagedScheduledExecutorService scheduledExecutorService;
@PostConstruct
public void start() {
try {
this.thread = managedThreadFactory.newThread(new SimpleTask());
this.thread.setName("SimpleTask Thread");
this.thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public class SimpleTask implements Runnable {
@Override
public void run() {
...
}
}
Refers Content from docs.oracle.com is not included.Interface ManagedThreadFactory and Content from docs.oracle.com is not included.Interface ManagedExecutorService documentation.
The configuration for the Concurrency Utilities is in the EE subsystem configuration:
<subsystem xmlns="urn:jboss:domain:ee:4.0">
...
<concurrent>
<context-services>
<context-service name="default" jndi-name="java:jboss/ee/concurrency/context/default" use-transaction-setup-provider="true"/>
</context-services>
<managed-thread-factories>
<managed-thread-factory name="default" jndi-name="java:jboss/ee/concurrency/factory/default" context-service="default"/>
</managed-thread-factories>
<managed-executor-services>
<managed-executor-service name="default" jndi-name="java:jboss/ee/concurrency/executor/default" context-service="default" hung-task-threshold="60000" keepalive-time="5000"/>
</managed-executor-services>
<managed-scheduled-executor-services>
<managed-scheduled-executor-service name="default" jndi-name="java:jboss/ee/concurrency/scheduler/default" context-service="default" hung-task-threshold="60000" keepalive-time="3000"/>
</managed-scheduled-executor-services>
</concurrent>
<default-bindings context-service="java:jboss/ee/concurrency/context/default" datasource="java:jboss/datasources/ExampleDS" managed-executor-service="java:jboss/ee/concurrency/executor/default" managed-scheduled-executor-service="java:jboss/ee/concurrency/scheduler/default" managed-thread-factory="java:jboss/ee/concurrency/factory/default"/>
</subsystem>
Important Points:
-
context-service="default" needs to be there(configuration for the Concurrency Utilities in the EE subsystem configuration) if you are going to create new managed executors.
-
Without context-service="default" , the Thread Context ClassLoader does not get set, which means when the code in the thread runs, any thing looking for services , things in META-INF/services or META-INF, etc will not be found.
Related Solutions
What are the default-bindings in the ee subsystem in JBoss EAP 7.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.