Pages

Monday, August 29, 2011

Sample Stateless EJB3 in JBoss

Share it Please
In this article we will see how we can create a sample stateless bean in EJB3 using eclipse and deploy that in JBoss 5.The example simply takes some text from web application (index.jsp) and sends that to (WebClient.jsp) jsp page which has the code to access the EJBs and print the response from EJBs.

1. Create a Stateless EJB3

The First step in creating a sample EJB3 stateless application is (in eclipse),
Select New -> EJB Project -> Enter The Project Name (Example).
Select the Target Runtime as JBoss and EJB version as 3.0.Click Next.
Click Next
Select the “Generate ejb-jar.xml”.Click Finish

Once the project is created, select the ejbModule -> Right click and create a Package (com.exmaple).Once the package is created, create a sample java interface (HelloBeanInf) like,

HelloBeanInf.java

@Remote
public interface HelloBeanInf {
        public String sayHello(String message);
}

Enter the @Remote annotation on the top of the interface which makes that a remote interface. Export the javax.ejb.Remote if missing.

Once the Interface is created, its time to create a sample class (helloBeanImpl) like

HelloBeanImpl.java 

@Stateless(name="HelloBeanImpl")
@Remote(HelloBeanInf.class)
@Clustered

public class HelloBeanImpl implements HelloBeanInf {

        @Override
        public String sayHello(String message) {
                           return "This Is Sample EJB With Text"+message;
                  }
       
}

This prints the message that we sent.

This was the actual stateless bean that implements the HellobeanInf interface and implements the method. Make sure you write the annotations @Stateless and @Remote. we can use @clustered if we are planning to deploy the application in a clustered environment.

By this the Creation of Stateless EJB3 is completed.
2. Create a Sample Web application to access the Stateless bean (ExampleWeb).

For this, create a sample web application,in the WebContent,create 2 jsp pages which work in accessing the EJB’s.

WebClient.jsp

The content of the WebClient.jsp includes Creation of the hellobean object by using lookup of Context. Here is how it goes.

<! -- Make Sure you Write the Import Statements  - - >

<%@ page import="com.example.HelloBeanInf"%>
<%@ page import="com.example.*"%>
<%@ page import="javax.naming.*"%>

<%!
    private HelloBeanInf helloBean = null;

        public void jspInit() {

      try {
            InitialContext ic = new InitialContext();
           helloBean = (HelloBeanInf) ic.lookup("ExampleEAR/HelloBeanImpl/remote");
           System.out.println("Loaded HelloBean");

        } catch (Exception ex) {
            System.out.println("Error:"+ex.getMessage());
        }
    }

    public void jspDestroy() {
        helloBean = null;
    }

%>


<%
try {
       String message = request.getParameter("message");
      String result=helloBean.sayHello(message);
                                    
   %>
        <p> <b>The result is:</b> <%= result %> <p>
       
   <%
       }// end of try
          catch (Exception e) {
                  e.printStackTrace ();
        }
     %>


By this we complete the WebClient.jsp page.Now its time to create a index.jsp page which has the text box by which we send the text to Ejbs.

Index.jsp

<form name="myForm" action="WebClient.jsp" method="post">
Enter Text Here<input type=text value="" name="message"/>
<input type=submit name="submit" value="Enter Test To Get Response"/>
</form>

This file contains nothing but a form which has a text field to send message to WebClient.jsp.

3. Create a EAR with both the Example EJB application (Example) and Web application (ExampleWeb).

Select New -> Java EE -> Enterprise Application project (ExampleEAR) .Click next.
Enter the Name (ExampleEAR ) and Click Next.In the Next screen select the Java EE modules (in this case both Example and ExampleWeb).Check the “Generate the application.xml” and click Finish.

Now once the ExampleEAR is created,open the application.xml and modify the <context-root> to /example like
<context-root>/example<context-root>

4. Create a ear from the ExampleEAR file and copy the file into all/deploy/ location. Start the Server.

If you check the logs, you can see similar INFO messages 


2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main) Created KernelDeployment for: Example.jar
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main) installing bean: jboss.j2ee:ear=ExampleEAR.ear,jar=Example.jar,name=HelloBeanImpl,service=EJB3
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)   with dependencies:
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)   and demands:
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)  partition:partitionName=DefaultPartition
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)  jboss.ejb:service=EJBTimerService
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)   and supplies:
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)  Class:com.example.HelloBeanInf
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)  jndi:ExampleEAR/HelloBeanImpl/remote-com.example.HelloBeanInf
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main)  jndi:ExampleEAR/HelloBeanImpl/remote
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (main) Added bean(jboss.j2ee:ear=ExampleEAR.ear,jar=Example.jar,name=HelloBeanImpl,service=EJB3) to KernelDeployment of: Example.jar
2011-08-26 13:13:12,679 INFO  [org.jboss.ejb3.endpoint.deployers.EJB3EndpointDeployer] (main) Deploy AbstractBeanMetaData@b8a5bb{name=jboss.j2ee:ear=ExampleEAR.ear,jar=Example.jar,name=HelloBeanImpl,service=EJB3_endpoint bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container] constructor=null autowireCandidate=true}
2011-08-26 13:13:12,741 INFO  [org.jboss.ejb3.session.SessionSpecContainer] (main) Starting jboss.j2ee:ear=ExampleEAR.ear,jar=Example.jar,name=HelloBeanImpl,service=EJB3
2011-08-26 13:13:12,741 INFO  [org.jboss.ejb3.EJBContainer] (main) STARTED EJB: com.example.HelloBeanImpl ejbName: HelloBeanImpl
2011-08-26 13:13:12,989 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (main) Binding the following Entries in Global JNDI:

ExampleEAR/HelloBeanImpl/remote - EJB3.x Default Remote Business Interface
ExampleEAR/HelloBeanImpl/remote-com.example.HelloBeanInf - EJB3.x Remote Business Interface

2011-08-26 13:13:13,113 INFO  [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (main) deploy, ctxPath=/example

Here are the Output ,




























More to Come , Happy Coding :-)

No comments :

Post a Comment