EJB Made Simple
We seem to know some of the buzzwords surrounding software technology,but sometimes get bogged down when we have to understand where it can be used and more importantly what it is? Though J2EE does not necessarily fall into that category of buzzword, but it generally has an association with web pages, servlets.
This we realized when we had to recruit people with J2EE and we got a horde lot of people with web pages, servlets html experience and then realized that we actually should have been more specific in terms of what we required and then I realized that J2EE is vast and it would not be so easy to cover it all in a single article.
So I renamed the article to indicate EJB made simple instead of J2EE made simple.
After programming in java and being able to make some small applications that run on our desktop, we realize that java actually has a rich set of APIs that can be used.
But however, if we have to do some tasks which are generally always required by a large organization .
i.
e enterprise applications, then we are sure that most of the time we would require some sort of 1>Data persistency 2>Remote communication 3>Ability to process a request asynchronously without actually having to wait for the results immediately and a host of other tasks.
This could be easily written in our java programs, but not without taking lot of pain.
Assuming if we have to do database persistency, thenwe would definitely think about JDBC or related OR mapping tools.
Now if we have to do some remote communication where we have a client and a server, we would typically think of a Remote Method Invocation(RMI).
In case we require asynchronous communication, we would use JMS in all probability.
Now developing application that has one of these features itself would take quite some time.
If we require more than one of these services, then it requires patience to make it work completely.
Wouldn't it be simple if there was some mechanism that would simplify my tasks, as simple as starting a RMI registry when I want to do remoting, automatically taking care of OR mapping if I require database access or starting a JMS service automatically and allowing me to hook onto some message without me writing a whole lot of boiler plate code.
A J2EE container or an Application server does these tasks and more.
So if you are stuck with how to do more than one of these tasks, then go to EJB and it will become simpler.
Let us assume, we have a requirement to store some information in the database, we would definitely need a way to map the Java objects to SQL statements and vice versa.
Since, we are java programmers, it is simpler to work with Java objects instead of rows in a table and trying to map it.
This is where an Entity bean comes into picture.
We here define what variable is mapped to which SQL field.
And which java class maps to which SQL table.
And provide some helper methods which are typically used to query and update the table and maintain consistency between the table and the java objects.
Entity beans of J2EE does exactly this.
Each entry in the database table corresponds to a Java object and the table itself corresponds to a Java class.
In addition, it would be very helpful if we could control the number of threads that establish a connection to the database, so that it can be re-used.
A J2EE application server would help you in controlling this.
Let us assume that we have another requirement to make remote communication, we would require an interface that defines what all operations that can be performed.
Let us take a small example of calculating the product of 2 integers.
So we will end up having an interface that has a method to calculate the product of 2 integers something like the following interface Multiplier int multiply (int first, int second).
Now, we will have to write this class and instantiate it on our server and register it somewhere, so that some remote client can have a look up and make a method call on this object.
With EJB, you do not have to do most of this task yourself.
You define the interface as what is typically called the remote interface and implement the method.
The other tasks of instantiating the object, binding it to the naming service is taken care by the J2EE server using a file called the descriptor file associated with your remote interface.
It also takes care of starting the naming service, in our case more correctly the rmi registry.
So now you are more inclined to only worry about how to do the multiplication task, instead of bogging down on how to register,how to start the rmi registry and how many objects and when do I instantiate these object.
The client has to get the name and make a call to create this object and it is instantiated on the server and you could now make a call to the server with the object.
This is what is called a session bean.
It could either maintain a client state or could be a stateless bean.
In the above example of the Multiplier, it is a stateless session bean as you don't worry who calls this interface and if the same object used by multiple clients.
You typically will not have a member variable associated with these session beans.
However, a case where you would require a stateful session bean is a shopping cart, where you will have to remember all that you had earlier shopped so that when you want to finally pay the amount, it calculates the price of all the books that you had selected, which it stored during each selection of the book.
You would in all cases have one or more member variables associated with your session bean in this case.
Now, finally let us assume that we require some task to be processed in a asynchronous manner.
An example like you need to initiate writing some thing onto a file and you are not worried if that activity needs to done immediately before you start some other activity.
It can run in its own time frame and just needs to be completed.
This is where you would have typically used something called the Java Messaging Service.
You would create a listener object, and find the JMS service and register it there using connection factories and session and all.
Finally you would also have to define how many instances of this listener objects you want to create.
Assume that you donot need to do any of this task and only need to write the logic of processing the task that is of importance to you and all other aspects of looking up, establishing connection to the connection factories is taken care of by associating these in an xml file.
Then you life becomes simpler.
The J2EE server takes care of starting the messaging service and based on your inputs decides to connect to the JMS provider and on top of it all, you also need not worry about how many parallel instances need to be created.
If you want to do this, then it can be done in such a simple way by writing a Message Driven Bean.
The above 3 types of beans(Entity, Session and Message Driven) are what areknown as Enterprise Java Bean in the J2EE world.
This we realized when we had to recruit people with J2EE and we got a horde lot of people with web pages, servlets html experience and then realized that we actually should have been more specific in terms of what we required and then I realized that J2EE is vast and it would not be so easy to cover it all in a single article.
So I renamed the article to indicate EJB made simple instead of J2EE made simple.
After programming in java and being able to make some small applications that run on our desktop, we realize that java actually has a rich set of APIs that can be used.
But however, if we have to do some tasks which are generally always required by a large organization .
i.
e enterprise applications, then we are sure that most of the time we would require some sort of 1>Data persistency 2>Remote communication 3>Ability to process a request asynchronously without actually having to wait for the results immediately and a host of other tasks.
This could be easily written in our java programs, but not without taking lot of pain.
Assuming if we have to do database persistency, thenwe would definitely think about JDBC or related OR mapping tools.
Now if we have to do some remote communication where we have a client and a server, we would typically think of a Remote Method Invocation(RMI).
In case we require asynchronous communication, we would use JMS in all probability.
Now developing application that has one of these features itself would take quite some time.
If we require more than one of these services, then it requires patience to make it work completely.
Wouldn't it be simple if there was some mechanism that would simplify my tasks, as simple as starting a RMI registry when I want to do remoting, automatically taking care of OR mapping if I require database access or starting a JMS service automatically and allowing me to hook onto some message without me writing a whole lot of boiler plate code.
A J2EE container or an Application server does these tasks and more.
So if you are stuck with how to do more than one of these tasks, then go to EJB and it will become simpler.
Let us assume, we have a requirement to store some information in the database, we would definitely need a way to map the Java objects to SQL statements and vice versa.
Since, we are java programmers, it is simpler to work with Java objects instead of rows in a table and trying to map it.
This is where an Entity bean comes into picture.
We here define what variable is mapped to which SQL field.
And which java class maps to which SQL table.
And provide some helper methods which are typically used to query and update the table and maintain consistency between the table and the java objects.
Entity beans of J2EE does exactly this.
Each entry in the database table corresponds to a Java object and the table itself corresponds to a Java class.
In addition, it would be very helpful if we could control the number of threads that establish a connection to the database, so that it can be re-used.
A J2EE application server would help you in controlling this.
Let us assume that we have another requirement to make remote communication, we would require an interface that defines what all operations that can be performed.
Let us take a small example of calculating the product of 2 integers.
So we will end up having an interface that has a method to calculate the product of 2 integers something like the following interface Multiplier int multiply (int first, int second).
Now, we will have to write this class and instantiate it on our server and register it somewhere, so that some remote client can have a look up and make a method call on this object.
With EJB, you do not have to do most of this task yourself.
You define the interface as what is typically called the remote interface and implement the method.
The other tasks of instantiating the object, binding it to the naming service is taken care by the J2EE server using a file called the descriptor file associated with your remote interface.
It also takes care of starting the naming service, in our case more correctly the rmi registry.
So now you are more inclined to only worry about how to do the multiplication task, instead of bogging down on how to register,how to start the rmi registry and how many objects and when do I instantiate these object.
The client has to get the name and make a call to create this object and it is instantiated on the server and you could now make a call to the server with the object.
This is what is called a session bean.
It could either maintain a client state or could be a stateless bean.
In the above example of the Multiplier, it is a stateless session bean as you don't worry who calls this interface and if the same object used by multiple clients.
You typically will not have a member variable associated with these session beans.
However, a case where you would require a stateful session bean is a shopping cart, where you will have to remember all that you had earlier shopped so that when you want to finally pay the amount, it calculates the price of all the books that you had selected, which it stored during each selection of the book.
You would in all cases have one or more member variables associated with your session bean in this case.
Now, finally let us assume that we require some task to be processed in a asynchronous manner.
An example like you need to initiate writing some thing onto a file and you are not worried if that activity needs to done immediately before you start some other activity.
It can run in its own time frame and just needs to be completed.
This is where you would have typically used something called the Java Messaging Service.
You would create a listener object, and find the JMS service and register it there using connection factories and session and all.
Finally you would also have to define how many instances of this listener objects you want to create.
Assume that you donot need to do any of this task and only need to write the logic of processing the task that is of importance to you and all other aspects of looking up, establishing connection to the connection factories is taken care of by associating these in an xml file.
Then you life becomes simpler.
The J2EE server takes care of starting the messaging service and based on your inputs decides to connect to the JMS provider and on top of it all, you also need not worry about how many parallel instances need to be created.
If you want to do this, then it can be done in such a simple way by writing a Message Driven Bean.
The above 3 types of beans(Entity, Session and Message Driven) are what areknown as Enterprise Java Bean in the J2EE world.
Source...