DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Introducing SmallRye LLM: Injecting Langchain4J AI Services
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • A Systematic Approach for Java Software Upgrades
  • From J2EE to Jakarta EE

Trending

  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  1. DZone
  2. Coding
  3. Java
  4. Using CDI With Java EE Concurrency Utilities

Using CDI With Java EE Concurrency Utilities

If you aren't familiar with Java EE Concurrency Utilities, it's time to take a look at what the specification has to offer. We'll examine how it works with CDI here.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Apr. 24, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
16.4K Views

Join the DZone community and get the full member experience.

Join For Free

This blog post explores the usage of CDI along with Java EE Concurrency Utilities – specifically using CDI beans as managed tasks. Here is the sample application on GitHub.

Let's Begin With a Quick Overview

Java EE Concurrency Utilities provides APIs and constructs to manage concurrency within Java EE applications. Many of the Java EE components have specific concurrency semantics e.g. EJBs, JAX-RS resources, WebSocket endpoints, etc. Writing components with custom concurrency properties was traditionally difficult, since starting unmanaged threads in a Java EE container was forbidden, i.e. one was not able to leverage Java SE concurrency libraries. With Concurrency Utilities, Java EE applications have access to managed versions of the Java SE counterparts, namely,

  • ManagedExecutorSevice
  • ManagedScheduledExecutorSevice
  • ManagedThreadFactory

...and a bunch of other APIs as well, but the above ones are the Java EE equivalent of the Java SE concurrency APIs.

Tasks as CDI Beans

Both ManagedExecutorSevice and ManagedScheduledExecutorService can accept tasks to execute (in a container-managed thread pool) in the form of Runnable and Callable instances. The good thing is that these tasks can be CDI beans as well. Points worth noting are:

  • These CDI beans can be injected into other components as well as inject other beans.
  • The scope of the CDI beans, which can be used as tasks as restricted to @ApplicationScoped and @Dependent (for details, read section 2.3.2.1 of the specification).

Here Is a Summary of...

...what’s going on in the application. For more details, refer to the README and explore the code:

  • Tasks are POSTED via a REST interface and the client gets back a HTTP 202 (Accepted) in response along with a task id
  • Yhe BackgroundTask (CDI bean) is executed in a background thread by the ManagedExecutorService  – it is injected (@Inject) and a different instance is created on every invocation since the CDI bean is marked @Dependent and the JAX-RS resource is created on each request by the client
  • The status is store in a @Singleton EJB (TaskStore) – this is injected in the BackgroundTask CDI bean
  • Status of each task (in progress, failed, completed) can be tracked via a REST interface by querying against a task ID
  • One can also get the status of all tasks

If you want to read more, take a look at this Concurrency Utilities specification.

Cheers!

CDI Java EE Java (programming language)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introducing SmallRye LLM: Injecting Langchain4J AI Services
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • A Systematic Approach for Java Software Upgrades
  • From J2EE to Jakarta EE

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!