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.

EJB >= CDI

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.

254 comments:

«Oldest   ‹Older   201 – 254 of 254
Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai

healthandfitness said...

this article is very good easy to understand thnanks

raj khan said...

Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

https://globalonlinepills.com/product/soma-350-mg/


Buy Soma 350 mg
Soma Pill
Buy Soma 350 mg online



Buy Soma 350 mg online
Soma Pill
Buy Soma 350 mg

Manisha Sudha said...

Nice blog....waiting for next update....
Struts Training in Chennai
Best Struts Training in Chennai
Best Struts Training institute in Chennai
struts Training in OMR
struts Training in Porur
Wordpress Training in Chennai
SAS Training in Chennai
Spring Training in Chennai
Photoshop Classes in Chennai
DOT NET Training in Chennai

johnsmith265 said...



thanks for the information provided by you it's really a great help from your side but i got the complete solution from mentioned site
Epson Error Code 0x98

Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai

indiatweetsvizag said...

content

Indiatweets present an awesome traveling blogging and allied services in the different part of the country regarding travel glitches, like cuisine trip, cultural round up and wholesome travel experience for people who have come to vizag for a short time due to cultural or business excavation. our expert team is awesome
Vizag escorts
Visakhapatnam escorts
Vizag independent escorts
Vizag best escorts
Visakhapatnam escort service
Visakhapatnam independent escorts
Vizag escorts
Vizag escorts
Visakhapatnam escorts
Vizag escorts
Visakhapatnam escorts
Vizag independent escorts
Vizag best escorts
Visakhapatnam escort service
Visakhapatnam independent escorts
Vizag escorts
Vizag escorts
Visakhapatnam escorts

jose said...

Really nice post. Thank you for sharing amazing information.
Java Training in Credo Systemz/Java Training in Chennai Credo Systemz/Java Training in Chennai/Java Training in Chennai with Placements/Java Training in Velachery/Java Training in OMR/Java Training Institute in Chennai/Java Training Center in Chennai/Java Training in Chennai fees/Best Java Training in Chennai/Best Java Training in Chennai with Placements/Best Java Training Institute in Chennai/Best Java Training Institute near me/Best Java Training in Velachery/Best Java Training in OMR/Best Java Training in India/Best Online Java Training in India/Best Java Training with Placement in Chennai

The India said...

Popular Fashion Blogs in Surat
Fashion Blogger in Surat
Surat Blogger
Indian Fashion Blogger

t.balajithaman said...

Best Job done your content is very help full.I have got more informative data through your blog Euro Indian Matrimony - Telugu Matrimony good blog. blog like these are very helpful for us. It is always good to read and upgrade ourselves.

Regards
Telugu Matrimony

Sam curren said...


Brother is one of the highly preferred and top most electronic firms which are highly popularized for the manufacturing of printers. Brother Printer Support is active helpline that is available round the clock to resolve Brother printer problems. If you face any problem with your printer like, Brother Printer not Printing Anything, cable wire problem and others then reach experts of Brother Printer Support and get instant help.

Connect with our high expertise technicians if you are looking for resolving issues like 

crisbrown said...

nice post thansk for sharing
https://aboutkarachicity.blogspot.com/p/blog-page_16.html?showComment=1569654465194

johnsmith265 said...

thanks for the information provided by you it's really a great help from your side but i got the complete solution from mentioned site
https://www.hpsupport247.com/blog/steps-to-fix-hp-printer-error-code-59-f0

Mahiescorts said...


Hi I am mahi verma a beautiful manali Escorts who is ready to give you high quality of fun and unstoppable service without any restriction. I am a well-educated and

the best immense love provider where I can also show you every sexual drive which gives you the best love feelings and once you did the best sensual love with me, you

always wait for the next meet..https://mahiescorts.com/

Kodak Printer Support said...

Get one-stop solutions for Kodak printer issues via Kodak printer support customer service. Get comprehensive solutions in one platform right on the place.

Kodak Printer Support Number

Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai

jose said...

Really nice post. Thank you for sharing amazing information.
Java Training in Credo Systemz/Java Training in Chennai Credo Systemz/Java Training in Chennai/Java Training in Chennai with Placements/Java Training in Velachery/Java Training in OMR/Java Training Institute in Chennai/Java Training Center in Chennai/Java Training in Chennai fees/Best Java Training in Chennai/Best Java Training in Chennai with Placements/Best Java Training Institute in Chennai/Best Java Training Institute near me/Best Java Training in Velachery/Best Java Training in OMR/Best Java Training in India/Best Online Java Training in India/Best Java Training with Placement in Chennai

Sam curren said...


Brother printers are the symbol of perfection. Users of these printers might feel happy after using the features of these printers. Brother printers are known for its high quality prints. There are also some negatives in spite of its amazing features. Number of individuals is affected with Brother printer error and wants to get easy solution for these issues. Brother Printer support expert is available 24x7 to resolve these types of issues like not working properly, cable problem, cartage problem, blank screen, Brother Printer not Printing Anything etc If you need any Brother printer support, you may contact Brother Customer Service.
Brother Printer Customer Support number

johnsmith265 said...


thanks for the information provided by you it's really a great help from your side but i got the complete solution from mentioned site
https://www.hpsupport247.com/blog/fix-hp-laptop-error-code-601/

INTRADOTE (**OFFICIAL**) said...

tndte

Unknown said...

Bigg Boss 13 is the thirteen season of the Indian reality TV series Bigg Boss which is based on the Dutch series Big Brother,
and premiered on 29 September 2019 on Colors. The launch episode was titled 'Bigg Night'. Salman Khan hosts the season for the ninth time


Bigg Boss Seasion 13

Venkatesh CS said...

Thanks for sharing valuable information.
Digital Marketing training Course in Chennai
digital marketing training institute in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in Chennai
digital marketing courses with placement in Chennai
digital marketing certification in Chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in Chennai

zuan said...

Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai

Rajesh said...

Nice infromation
Selenium Training In Chennai
Selenium course in chennai
Selenium Training
Selenium Training institute In Chennai
Best Selenium Training in chennai
Selenium Training In Chennai

Rajesh said...

Rpa Training in Chennai
Rpa Course in Chennai
Rpa training institute in Chennai
Best Rpa Course in Chennai
uipath Training in Chennai
Blue prism training in Chennai

Data Science Training In Chennai
Data Science Course In Chennai
Data Science Training institute In Chennai
Best Data Science Training In Chennai

neha mumbai escorts said...

Welcome to Mumbaihotcollection.com, home of the finest Mumbai escorts in Mumbai area. Our Mumbai escorts are the finest Mumbai has to offer and will provide an unforgettable Mumbai Escort experience!
Escorts in Mumbai
Escort Service in Mumbai
Mumbai Escorts
Mumbai Call Girls
Call girls in Mumbai
Escorts Mumbai
Mumbai Escorts Blogs

johnsmith265 said...

thanks for the information provided by you it's really a great help from your side but i got the complete solution from mentioned site
https://www.epsonsupports247.com/how-to-fix-epson-error-code-0xf3/

alexryan said...

Awesome article! It is in detail and well formatted that i enjoyed reading. which in turn helped me to get new information from your blog.
https://www.hpsupport247.com/blog/steps-to-fix-hp-printer-error-code-59-f0/

Rajesh said...

Nice infromation
Selenium Training In Chennai
Selenium course in chennai
Selenium Training
Selenium Training institute In Chennai
Best Selenium Training in chennai
Selenium Training In Chennai

Rajesh said...

Rpa Training in Chennai
Rpa Course in Chennai
Rpa training institute in Chennai
Best Rpa Course in Chennai
uipath Training in Chennai
Blue prism training in Chennai

Data Science Training In Chennai
Data Science Course In Chennai
Data Science Training institute In Chennai
Best Data Science Training In Chennai

Rajesh said...

Python Training In Chennai
Python course In Chennai
Protractor Training in Chennai
jmeter training in chennai
Loadrunner training in chennai

Faizan Ali said...

Most of the time people want to use converter that converts PDF to JPG and vice versa. Just hit the link and in the search box write down about your required converter and here you go!!! Yes! It is this much easy. Online converters are really popular these days because they are providing a simple way to do a difficult task in no time. They may take few minutes to convert your file from one format to another but the thing is they are really saving your lots of time and effort because they are doing a really complex task for you.

Faizan Ali said...

Most of the time people want to use converter that converts PDF to JPG and vice versa. Just hit the link and in the search box write down about your required converter and here you go!!! Yes! It is this much easy. Online converters are really popular these days because they are providing a simple way to do a difficult task in no time. They may take few minutes to convert your file from one format to another but the thing is they are really saving your lots of time and effort because they are doing a really complex task for you.

Faizan Ali said...

if you want to convert a youtube video file in mp3 or mp4 then how exactly you are going to do this? There is a long process for it but you don’t need to follow it because now you have online converter that promises you to provide clean and efficient data as an end product. File format is converted here and it is the one of the most used soft ware that is really helping out each and every person in order to complete your tasks with finish and elegance or even to watch anything from youtube mp3 or mp4 file format.

Faizan Ali said...

Dear lottery sambad really give people a chance to play and win even when they can’t afford a precious lottery ticket just start with the lowest amount and let the lottery sambad today 8pm be your lucky news ever. Yes! The news of happiness and luck!

Faizan Ali said...

Pinoy TV gives you something that can make you feel like home and gives you the warmth and feel of the place where you belong and maintain that charm as well! Pinoy lamingan and Pinoy ako are working with the massive thought to show the world that how much they are doing in TV industry and how much they have talent in them.

Call girls in Dehradun said...

Call Girls in Gurgaon
Call Girl in Gurgaon
Call Girls Gurgaon
Call Girl Gurgaon
Call Girl Gurgaon Service
Call Girls Gurgaon Service
Call Girl in Gurgaon Service
Call Girls in Gurgaon Service
Best Call Girl Gurgaon
Best Call Girls Gurgaon
VIP Call Girls Gurgaon
VIP Call Girl Gurgaon
Gurgaon Call Girl
Gurgaon Call Girls
Russian Call Girls Gurgaon
Russian Call Girl Gurgaon
Call Girls in Dwarka
Call Girl in Dwarka
Call Girls Dwarka
Call Girl Dwarka
Call Girls in Haridwar
Call Girl in Haridwar
Call Girls Haridwar
Call Girl Haridwar
Call Girls in Noida
Call Girl in Noida
Call Girls Noida
Call Girl Noida
plumbing company Toronto
plumbing company Mississauga
https://aqualuxdp.ca/
https://www.torontoplumbinggroup.com/
https://www.singledate.in/
https://muskangirlsdwarka.in/
https://rishikeshgirls.in/call-girls-in-haridwar/
https://shipranoida.in/
Call Girls in Gurgaon | Call Girls Gurgaon | Call Girl Gurgaon | Call Girl in Gurgaon

archers smith said...

great information. The blog was really nice, I really found out about a lot of Awesome content.epson printer error code 0xea

Shazia Mughal said...

This blog was how do I say it? Relevant!! Finally I have found something which helped me. Thanks!!!...Yeh Rishta Kya Kehlata Hai

Animated said...

Mumbai, Nov 5 : Explicit language, extreme egos and explosive rage are nothing new in "Bigg Boss". This year, however, housemates are engaging in something they never did before. Participants in the ongoing "Bigg Boss season 13" do not hesitate to engage in physical fights. Be it Shehnaz Gill and Devoleena Bhattacharjee or Siddharth Shukla and Mahira Sharma, housemates this season seem solely interested in taking the controversial reality show to a new low. Twitter on Tuesday has been abuzz about television star and housemate Siddharth Shukla. He reportedly got into a physical fight with TV actress Mahira Sharma, who injured herself during a task on the daily show airing on Colors. In a leaked promo of Tuesday's episode that is doing the rounds on social media, Sidharth is seen getting evicted for hurting Mahira while they were performing a task. In the clip, housemates are seen being actively involved in a new task for captaincy. In the task, they were required to throw bags in a truck. As the task started, they begin running around.

Neha Khan said...

BA Exam Result - BA 1st Year, 2nd Year and 3rd Year Result

Bsc Exam Result - Bsc 1st Year, 2nd Year and 3rd Year Result

Review News said...


Explore the list of all episodes of TV Serial Kumkum Bhagya online.
full HD Video Watch all episodes anytime, anywhere only on ZEE5

Kumkum Bhagya All Episodes

Review News said...

Regardless of the idea of the problem that your computers may encounter, it is necessary to direct extensive research and ensure that the organization of your choice is exceptionally reliable and has a very bad reputation. Yes we are here to help you on behalf of our company here at Computer Technician Kuwait. All kinds of computer repair and maintenance works that we offer from computer engineer for years here in Kuwait.
فني كمبيوتر

Manmohini said...

Watch Manmohini Zee Tv Online Latest Indian Drama Serial Full Episodes In HD, We Try Our Best To Make Available All Latest Full Episodes, Manmohini Zee Tv. Manmohini Zee Tv Online

meritstep Technology said...

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
best angular js online training

archers smith said...


Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. epson printer error code e-01

meritstep Technology said...

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
angular js online training
best angular js online training
top angular js online training

Husnain said...

Watch Online Vidya Video Episode On Voot. Vidya Colors Tv Latest Hindi Drama Watch Full Episodes Vidya
online Today Drama.
Thanks for sharing such a nice stuff

Vidya Latest Episode
<a href="https://vidyacolorstv.com/>Colors Tv</a>

mark henry said...

Nice and good post thanks for sharing if you are searching for the best Small business phone systems at affordable prices in Australia, then you may visit this website. Bizpoke provides the most reliable business phone systems at reasonable prices.
gugaset A540 cordless telephone,
hosted pbx,
cloud phone system,
small business phone system,

mark henry said...

Nice and good post thanks for sharing if you are finding celebrate kids birthday party in delhi then you may visit our website and book a dream party destination for your kid.

Bumble Boom Birthday,
Kids Birthday Party In Delhi,
Kids Playing Zone In Delhi,
Birthday Party Place In Delhi,
Best Kids Birthday party in Delhi,
Celebrate kids birthday party in Delhi,
Bumble Boom Birthday,

Yeh Rishta Kya Kehlata Hai said...

Yeh Rishta Kya Kehlata is indian star plus drama serial started on 2009.
Its cast involve naira and kartik they have good chemistry in love and other.

Kasauti Zindagi Ki

archers smith said...

Hey, I'm regular visitor of this website, I've read many of your blogs but never engaged with any but your this blog inspired me so deep that I couldn't resist my self today from giving you appreciation about this blog, so thank you very much Mr.Author keep up writing like this and inspiring other people and making them knowledgeful.
printer not activated error code 41

Shazia Mughal said...

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy...Kumkum Bhagya
Yeh Rishta Kya Kehlata Hai

Sage 50 and QuickBooks Support said...

If you are new user to sage 50 accounting software and looking for the sage 50 technical support.If yes than you have come to right place as we provide efficient technical support service to customers who show complete faith in us. With our efficient and highly qualified team ,we never disappoint our customers.You can reach us at 1800-910-4754 at any hour of the day. You can also visit our website at https://www.geekaccounting247.com/ for the complete knowledge of the sage products and services.

The Services we offered are following-
Sage 50 Technical Support Number
Sage 100 Technical Support Number
Sage 50 live chat
Sage 50 Technical Support phone Number
Sage 50 customer service number
Sage 50 payroll support number


Sage 50 support phone number

«Oldest ‹Older   201 – 254 of 254   Newer› Newest»