tags:

views:

246

answers:

3

Hello, As we all know that EJB's in 3.1 or 3.0 are simple POJOs. You just need to give annotations here and there and it gets converted to EJB from simple class. So, now my question is why should i use EJBs at all? Can i not do without them? In .Net i created class library and got things done. I never felt the need for anything like EJB. Simple classes were enough. Then, why in Java people stress on EJB? What is difference between a simple POJO and EJB in terms of execution and memory? Further which function should i write in EJB and which should i write in simple class? Should i dump every function in EJB only? or there is some kind of strategy?

Does EJB provide anything special?

+1  A: 

You should probably do some proper investigating about EJBs before making a decision (i.e. via the resources linked on this page). They are useful depending on the application that you're building.

They provide remoting and transactional support for one which you don't get with simple POJOs.

In general, if you're doing large-scale "Enterprise" development, using EJBs can really help. Otherwise, stick with a framework like Spring.

Phill Sacre
Ok Phill Sacre. And my another (maybe) stupid question is :- We make remote interfaces in EJB and say it can be accessed remotely. As i work on localhost i never experienced "remote" thing. The thing i want to know is remote interface can only work in a network. Right?I mean only LAN. Or can it take the place of web service? I mean it can work across networks?
Ankit Rathod
Remote interfaces work across a network, yes. Web services are so similar to session beans behind remote interfaces that it only takes an annotation of the session bean to publish it as a web service.If you don't need or want network access, use local interfaces.
Tomislav Nakic-Alfirevic
Great! Thanks Tomislav Nakic-AlfirevicSo are web services better or EJBs? Because RMI works behind EJBs which marshall and unmarshall the parameters?
Ankit Rathod
+6  A: 

You should use EJB if it solves a problem for you that one of the lighter-weight frameworks doesn't. Some examples are:

  • Clustering
  • Fail-over
  • Distributed caching
  • Administration tools
richj
+3  A: 

In the commercial world, EJBs have almost totally been supplanted by Spring. The advantage of Spring is that it can be run inside a simple Web container like Jetty or Tomcat whereas EJBs (pre-3.1) require a J2EE application server (eg Websphere, Weblogic, JBoss, Glassfish), which has a larger footprint.

EJBs do a couple of things well, most especially stateless session beans, which can be an efficient avenue for distributed transactions.

EJB since 3.0 has been split into two parts: the EJB spec and the JPA spec. JPA (Java Persistence API) is as the name suggests the persistence part of the API. This is really commonly used in libraries like EclipseLink, Hibernate and Toplink. Hibernate is the most popular and predates JPA but the differences aren't so large.

JPA is a form of ORM (object-relational mapper) of projecting an object model onto a relational database.

JPA is, in my experience, used much more commonly than EJB.

cletus
Both Spring and EJB have become relatively similar, the first one becoming more and more heavyweight (there is even a spring server now) and the second one more lightweight. http://www.adam-bien.com/roller/abien/entry/future_of_enterprise_java_is
ewernli
So, if i have some utility functions like generateRandomFileName() which is used to save photos of albums to disk with random file name each time. Should i put this kind of utility functions also in EJB? or only logic of CRUD, means accessing entities and doing operations with EntityManagers should be put in EJBs?
Ankit Rathod
@Nitesh uploading a photo will have a Web component to handle the actual uploading. You could make a session bean to persist that photo. That session bean will write an entry to the database and possibly save it to a file. Arguably you could save it to a file from the Web component and pass that file name and pass that file name to the session bean. Session beans are typically wrappers around JMS or database calls as they are (or can be) transactional vs file operations, which generally aren't.
cletus
@Nitesh also bear in mind Java has a native method for creating unique file names `File.createTempFile()` (you can use it for more than temp file names). See http://bit.ly/5gZXnJ
cletus
Many many thanks.
Ankit Rathod