Wednesday, November 21, 2012

CDI, when to break out the EJBs

I sometimes joke that CDI is basically EJB 4.0. While that's obviously not true there is a considerable amount of similarity and that does create a bit of confusion for people. Here is some general information on EJB and CDI as they relate to each together.


Note that EJBs are CDI beans and therefore have all the benefits of CDI. The reverse is not true (yet). So definitely don't get into the habit of thinking "EJB vs CDI" as that logic really translates to "EJB+CDI vs CDI", which is an odd equation.
In future versions of Java EE we'll be continuing to align them. What aligning means is allowing people to do what they already can do, just without the@Stateful@Stateless or @Singleton annotation at the top.

EJB and CDI in Implementation Terms

Ultimately, EJB and CDI share the same fundamental design of being proxied components. When you get a reference to an EJB or CDI bean, it isn't the real bean. Rather the object you are given is a fake (a proxy). When you invoke a method on this fake object, the call goes to the container who will send the call through interceptors, decorators, etc. as well as take care of any transaction or security checks. Once all that is done, the call finally goes to the real object and the result is passed back through the proxy to the caller.
The difference only comes in how the object to be invoked is resolved. By "resolved" we simply mean, where and how the container looks for the real instance to invoke.
In CDI the container looks in a "scope", which will basically be a hashmap that lives for a specific period of time (per request @RequestScoped, per HTTP Session @SessionScoped, per application @ApplicationScoped, JSF Conversation @ConversationScoped, or per your custom scope implementation).
In EJB the container looks also into a hashmap if the bean is of type @Stateful. An @Stateful bean can also use any of the above scope annotations causing it to live and die with all the other beans in the scope. In EJB @Stateful is essentially the "any scoped" bean. The @Stateless is basically an instance pool -- you get an instance from the pool for the duration of one invocation. The @Singleton is essentially @ApplicationScoped
So in a fundamental level, anything you can do with an "EJB" bean you should be able to do with a "CDI" bean. Under the covers it's awfully hard to tell them apart. All the plumbing is the same with the exception of how instances are resolved.
They aren't currently the same in terms of the services the container will offer when doing this proxying, but as I say we're working on it at the Java EE spec level.

Performance note

Disregard any "light" or "heavy" mental images you may have. That's all marketing. They have the same internal design for the most part. CDI instance resolution is perhaps a bit more complex because it is slightly more dynamic and contextual. EJB instance resolution is fairly static, dumb and simple by comparison.
I can tell you from an implementation perspective in TomEE, there's about zero performance difference between invoking an EJB vs invoking a CDI bean.

Default to POJOs, then CDI, then EJB

Of course don't use CDI or EJB when there is no benefit. Throw in CDI when you start to want injection, events, interceptors, decorators, lifecycle tracking and things like that. That's most the time.
Beyond those basics, there are a number of useful container services you only have the option to use if you make your CDI bean also an EJB by adding@Stateful@Stateless, or @Singleton on it.
Here's a short list of when I break out the EJBs.

Using JAX-WS

Exposing a JAX-WS @WebService. I'm lazy. When the @WebService is also an EJB, you don't have to list it and map it as a servlet in the web.xml file. That's work to me. Plus I get the option to use any of the other functionality mentioned below. So it's a no-brainer for me.
Available to @Stateless and @Singleton only.

Using JAX-RS

Exposing a JAX-RS resource via @Path. I'm still lazy. When the RESTful service is also an EJB, again you get automatic discovery and don't have to add it to a JAX-RS Application subclass or anything like that. Plus I can expose the exact same bean as an @WebService if I want to or use any of the great functionality mentioned below.
Available to @Stateless and @Singleton only.

Startup logic

Load on startup via @Startup. There is currently no equivalent to this in CDI. Somehow we missed adding something like an AfterStartup event in the container lifecycle. Had we done this, you simply could have had an @ApplicationScoped bean that listened for it and that would be effectively the same as an @Singleton with @Startup. It's on the list for CDI 1.1.
Available to @Singleton only.

Working in Parallel

@Asynchronous method invocation. Starting threads is a no-no in any server-side environment. Having too many threads is a serious performance killer. This annotation allows you to parallelize things you do using the container's thread pool. This is awesome.
Available to @Stateful@Stateless and @Singleton.

Scheduling work

@Schedule or ScheduleExpression is basically a cron or Quartz functionality. Also very awesome. Most containers just use Quartz under the covers for this. Most people don't know, however, that scheduling work in Java EE is transactional! If you update a database then schedule some work and one of them fails, both will automatically cleaned up. If the EntityManager persist call fails or there is a problem flushing, there is no need to un-schedule the work. Yay, transactions.
Available to @Stateless and @Singleton only.

Using EntityManagers in a JTA transaction

The above note on transactions of course requires you to use a JTA managed EntityManager. You can use them with plain "CDI", but without the container-managed transactions it can get really monotonous duplicating the UserTransaction commit/rollback logic.
Available to all Java EE components including CDI, JSF @ManagedBean@WebServlet@WebListener@WebFilter, etc. The@TransactionAttribute annotation, however, is available to @Stateful@Stateless and @Singleton only.

Keeping JTA managed EntityManager

The EXTENDED managed EntityManager allows you to keep an EntityManager open between JTA transactions and not lose the cached data. Good feature for the right time and place. Use responsibly :)
Available to @Stateful only.

Easy synchronization

When you need synchronization, the @Lock(READ) and @Lock(WRITE) annotations are pretty excellent. It allows you to get concurrent access management for free. Skip all the ReentrantReadWriteLock plumbing. In the same bucket is @AccessTimeout, which allows you to say how long a thread should wait to get access to the bean instance before giving up.
Available to @Singleton beans only.


«Oldest   ‹Older   401 – 433 of 433
Best Waist Trainers said...

"Waist Trainer" comes with a Belt Closure and you need to wear it on midsection of your body.
Best Waist Trainer covers your body from underbust to downwards up to your waistline.

Best Top Waist Trainer and Shapewear

vannesa said...

Togel Online

Bandar Togel Terpercaya

Judi Togel

Agen Togel

data hk

data sgp

data sydney


Tim Pegios said...

What a nice blog, thanks for this great post. If you have question regarding How To setup Orbi, then you can directly consult with our Orbi Helpline experts or call us at USA/Canada: +1-855-869-7373 and UK/London: + 44-800-041-8324.
Orbi Helpline

Antivirus Activation Helpline said...

What a wonderful Post. Thanks for this great article. If you have any question regarding How to Activate Antivirus, then you can directly consult with our Antivirus Activation Helpline experts or contact us at USA/Canada: +1-855-869-7373 and UK/London: + 44-800-041-8324.
Antivirus Activation Helpline
How To Resolve McAfee Error 76567

desitv said...

As the show is the spin-off of Kumkum Bhagya, it narrates a story of Pragya and Bulbul's lost younger sisters Preeta and Srishti.
Kundali Bhagya Drama

Supreme mobiles said...

Nice post today gold rate

Rosetta said...

Watch Ertugrul Ghazi Seasons & Movies Online & Free Download in HD Quality Videos, Visit to See All Types of Seasons, Movies, Dramas & Shows.

Ertugrul Ghazi In Urdu

Mason Olivia said...

QuickBooks may have a lot many benefits, but it is a software after all and that’s why users face many errors or technical glitches which affects business operations. Some of these errors can be easily fixed while for few others you need to take help from experts. One quite common error is the QuickBooks error code 6073. This error generally happens when someone opens the company file in the multi-user mode.

QuickBooks Error Code 6073
QuickBooks Error 6073
Error 6073 QuickBooks
QuickBooks Error 6073
Error Code 6073: QuickBooks
QuickBooks Error 6073

Venkatesh CS said...

Excellent Blog. Thank you so much for sharing.
salesforce training in chennai
salesforce training in omr
salesforce training in velachery
salesforce training and placement in chennai
salesforce course fee in chennai
salesforce course in chennai
salesforce certification in chennai
salesforce training institutes in chennai
salesforce training center in chennai
salesforce course in omr
salesforce course in velachery
best salesforce training institute in chennai
best salesforce training in chennai

Mason Olivia said...

QuickBooks error code 6129 is seen on the screen, when the database connection verification failure, or when the software is looking to access the company files. This actually results in forbidding the user to open the company file. This error can be a bit annoying for the users to handle, but we will ease the process for you, by discussing the causes and fixes for it.

QuickBooks error code 6129
QuickBooks error code -6129 0
QuickBooks error 6129
QuickBooks error -6129
QuickBooks Desktop error 6129
QuickBooks Desktop error code 6129
Error Code 6129 in QuickBooks
6129 Error in QuickBooks Desktop

lavanya said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog. Software Testing Training in Chennai | Software Testing Training in Anna Nagar | Software Testing Training in OMR | Software Testing Training in Porur | Software Testing Training in Tambaram | Software Testing Training in Velachery

Mason Olivia said...

Users are getting a QuickBooks Error Code 15101 or the series of 15000 while updating the Payroll or QuickBooks. You should check for anything that can cause the problem on that computer. QuickBooks provides 24/7 services to resolve the issues of QuickBooks Users. Dial QuickBooks Error Support Phone Number +1844-857-4846.

QuickBooks Error 15101
QuickBooks Error Code 15101
QuickBooks Update Error 15101
QuickBooks Payroll Error 15101
QuickBooks Desktop Error 15101
Error 15101 in QuickBooks
Update Error 151010 in QuickBooks
Payroll Error 15101 in QuickBooks
QuickBooks Error 6073

Raj said...

The Best Real Estate Agency in Jaipur call now.

imran said...

Bigg Boss season 14 will conduct its online registration very soon.
The popular television reality show has completed its 13th season

Bigg Boss 14 Watch Online

Mumbai escorts and call girls services said...

Mumbai escorts providing an Incall and outcall facilty in Mumbai see the more services in Mumbai and get the mumbai call girls at your location.

Mumbai escorts

Escorts mumbai

Mumbai escorts service

Female escorts Mumbai

Mumbai call girls

Mumbai call girl

Andheri escorts

Vashi call girls

bandra escorts

Juhu escorts

Unknown said...

the admin of this website is very hard working. i like this website and i am following this website also.
We are uploading new latest Dramas And New Movies on this website please visit and Watch and download your favorite
Episodes and Movies


resultangka88 said...


govt job said...

B.SC. final year Result 2020

Tim Pegios said...

Very Nice Blog...If you want to get to know more about How to fix Orbi Device Issues, then you can directly consult with Orbi Helpline experts. For any queries, call our experts at the USA/CA: +1-855-869-7373 and UK/London: +44-800-041-8324.
Orbi Helpline
Orbi Setup Issues
Tips to Fix Orbi Login and Setup Issues
How to Resolve The Netgear Orbi Error 404
How to Fix the Issue of Orbi Router Not Found
How to Fix Netgear Orbi App Login Error
How To Resolve The Error Of Orbi Firmware Update Error
How To Resolve The Netgear Orbi Authentication Error
How To Resolve The Netgear Orbi Certificate Error
Orbi Login and Setup Issues

Antivirus Activation Helpline said...

Nice Blog...Do you face any problem while using McAfee Antivirus? No need to worry about that. Our experienced experts will help you on How to Fix the McAfee Error x5485. For any query? Contact our Antivirus Activation Helpline experts at toll free number- USA/Canada: +1-855-869-7373 and UK/London: +44-800-041-8324.
How to Fix the McAfee Error x5485

QuickBooks Support Phone Number said...

Most of the users are looking for frequently asked questions (FAQs) to clear their doubts. It is pretty obvious that someone is looking for answers related to their questions that usually arise while using accounting software. Don’t need to worry, if you’ve queries or eagerly looking for the solutions because is providing the information related to your queries as it covers almost all topics related to accounting and bookkeeping FAQs. If you still have more queries or questions that keep popping up in your mind then reach us anytime. said...

Watch Online Kurulus Osman Season 1 All Episodes Urdu And English Subtitle By TvFun4u. Watch Turkish Drama Series Kurulus Osman Season 1 In Urdu And English Dubbing Complete In HD Quality.

QuickBooks Support Phone Number said...

When a QB user is making an attempt to open QB, QuickBooks Error 7149 seems on the screen with the subsequent message – there’s a tangle with QuickBooks and it’s needed that the program is closed. For further details dial QuickBooks support phone number. Experts will solve your every query.

QuickBooks Error Code 7149
QuickBooks Run Time Error 7149

seotech said...

The admin of this website is very hard working. i like this website and i am following this website also.
We are uploading new latest Dramas And New Movies on this website please visit and Watch and download your favorite
Episodes and Movies

Drama Point

desitv said...

Discover the most anticipated Indian movies on IMDb based on real-time popularity.
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya
Kundali Bhagya

priyankabajaj said...

Our Mohali Escorts are intended to indulgence the beauty lovers. We stand HI Fi network with great true associates. We have Female Escorts, College girls, VIP Escorts, Models, and Call girls ready to entertain you.
Mohali Escorts
Mohali Call Girls
Mohali Escorts Services
Call Girls In Mohali
Mohali Escorts
Mohali Escort Service
Mohali Escorts
Mohali Escort
Mohali Escorts

priyankabajaj said...

Are you looking for sexy Chandigarh Escorts? Priyanka Bajaj provides independent female call girls and escorts services from leading Chandigarh Escorts Services. Book best Call Girls in Chandigarh With 100% Confidential.
Follow Us:-
Chandigarh Escorts
Chandigarh Call Girls
Chandigarh Escorts Services

priyankabajaj said...

Our Rishikesh Escorts are intended to indulgence the beauty lovers. We stand HI Fi network with great true associates. We have Female Escorts, College girls, VIP Escorts, Models, and Call girls ready to entertain you.
Rishikesh Escorts
Rishikesh Escorts
Rishikesh Escorts Services
Rishikesh Call Girls

edan link said...

I thank you for the information and articles you provided cara menggugurkan kandungan

Unknown said...

I think the admin of this website is very hard working. i like this website and i am following this website also.
We are uploading new latest Dramas And New Movies on this website please visit and Watch and download your favorite
Episodes and Movies


snk social fame said...

thanks for sharing.
Do you want to Buy Active Instagram Followers India , contact us

And if you want to Buy Real Tiktok Followers India , contact us.

Gurgaon Escorts Service said...

Call Girls in Raiwala
Call Girls in Roorkee
Call Girls in Shantikunj
Escorts Service in Haridwar
Escorts Service in Haridwar
Call Girls in haridwar
Call Girls in Haridwar
Escorts Service in Haridwar
Hi Profile Escorts in Haridwar
Russian Call Girls in Haridwar

Aditi Gupta said...

Thanks for sharing nice article. This is so amazing. If you want to instagram followers to increase engagement and reach the target audience. Buy instagram followers Mumbai
We are offer 100% real and active instagram followers. I like this and its helpful for me.

«Oldest ‹Older   401 – 433 of 433   Newer› Newest»