Wednesday, December 18, 2013

How to Create Your First EJB Bean

In this post i would like to highlight the importance of EJBs into the SOA world and demonstrate how to create a simple EJB (with a couple of advanced posts on EJBs following in the very near future).

So let' start with the definition of what an EJB is.

 Enterprise JavaBeans (EJB) is a Java EE server-side component architecture for modular construction of enterprise applications. Due to it's numerous advantages (portability, productivity, integration with the J2EE platform, scalability, etc.) it remains a precious weapon into any developer's hands. Especially in large, complex, enterprise SOA implementations.

So let's see how you can jump start yourself into the EJB world.

I've very quickly created an application ("HelloWorldEJBApp") with a single project ("HelloWorldEJB").


There are three types of beans that you can create:

  • Stateless Session Beans: As it's name denotes, this bean does not store session or client state information between invocations.
  • Stateful Session Beans: Stateful Session Beans maintain state information between the bean and a specific client.
  • Entity Beans: Represent a set of persistent data and provide methods for maintaining and reading these data.
Stateless Session Beans can support multiple clients, offering better scalability, therefore i will be using this type of bean in my example.


Create a new Session Bean (right-clicking on the project and from the "All Technologies" tab select "EJB" from the "Business Tier" category. Select "Session Bean" from the items).


On step 1 of the "Create Session Bean" wizard, make sure you select the latest EJB version which is 3.0.

On step 2 provide an EJB name (in my example it's "HelloWorldSessionEJB"). In this step you have additional options such as the session type (in our case it's a stateless session type), transaction type (whether the bean will be managed by the Weblogic Server EJB container or by the EJB itself; in our case it will be set to "Container"), whether to implement the TimedObject interface for delivering timer exception notifications, customizing the mapped name (i will leave it as is. In my case it's "HelloWorldEJBApp-HelloWorldEJB-HelloWorldSessionEJB") and whether to generate session facade methods (it's not applicable in our case since we are not using any persistent unit).


On step 3 specify the bean class name and path and on step 4 select whether you would like to implement a remote and/or local interface. If you will be deploying and using the EJB from the same tier then just select the local interface, otherwise if you will be deploying and accessing the EJB remotely then select the remote interface.In this example i will be demonstrating how to use the remote interface (since it makes more sense in a SOA environment).



Click finish. You should have 3 files created; "HelloWorldSessionEJBLocal.java" which is the local interface created for your bean, "HelloWorldSessionEJBBean.java" which is the bean implementation class and "weblogic-ejb-jar.xml", a Weblogic Server specific EJB deployment descriptor file.

I will create a simple method in the EJB implementation class as follows:

    public String sayHello(String name){
        return "Hello " + name;   
    }


Make sure you register this new method in the remote interface








and add the "WebLogic 10.3 Thin-Client" jar in the project's "Libraries and Classpath".


To test your EJB you will first need to generate a sample java client and secondly you need to deploy the EJB to your JDeveloper's integrated Weblogic Server.

To generate a sample client just right click the EJB implementation class and select from the context menu "New Sample Java Client ...".


Update the generated client java class as follows to call the sayHello EJB method:

            HelloWorldSessionEJBBeanRemote bean = (HelloWorldSessionEJBBeanRemote)context.lookup("HelloWorldEJBApp-HelloWorldEJB-HelloWorldSessionEJBBean#aantoniou.HelloWorldSessionEJBBeanRemote");
            String response = bean.sayHello("Antonis");

System.out.println(response);

To deploy your EJB bean just right-click on the bean and select run. JDeveloper will automatically deploy the bean onto the integrated Weblogic Server.

Once the bean is deployed run the client class and you should see onto the console the hello message returned from the sayHello EJB method.

That's it. You have successfully created your first EJB bean.

In the following posts i will demonstrate how to pack the EJB into a jar file and use it from different applications and how to service enable your EJB.


No comments:

Post a Comment