Pages

Monday, August 29, 2011

Sample MDB in JBoss 5

Share it Please

In this article we will see how we can create a sample Message Driven bean in EJBS3 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 Message Driven Bean

The First step in creating a sample EJB3 application is (in eclipse),
Select New -> EJB Project -> Enter The Project Name (ExampleMDB).
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 Message Driven Bean(ExampleMDBean) and add the code ,

package com.example;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(name = "MessageMDBSample", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyQueue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")},
        messageListenerInterface = MessageListener.class)

public class ExampleMDBean implements MessageListener, MessageDrivenBean {

private static final long serialVersionUID = 1L;

    public ExampleMDBean() {
        // TODO Auto-generated constructor stub
    }

    public void ejbRemove() {
        // TODO Auto-generated method stub
    }

    public void setMessageDrivenContext(MessageDrivenContext arg0) {
     this.setMessageDrivenContext(arg0);
    }
    
    public void onMessage(Message mess) {
     TextMessage message=(TextMessage)mess;
     try {
System.out.println("Message Received"+message.getText());
}catch(Exception e) {
e.printStackTrace();
    }
        }
}


2. Create a Sample Web application to access the Stateless bean (ExampleMDBWeb).

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

WebClient.jsp

This page simply creates an InitialContext and will make a lookup for the Queue | Topic.

<%@ page import="javax.naming.*"%>
<%@ page import="javax.jms.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%!

QueueConnection queueConnection=null;
QueueConnectionFactory queueFactory =null;
QueueSession queueSession =null;
Queue queue =null;
QueueSender queueSender =null;
TextMessage message =null;
public void jspInit() {
        try {
        
            InitialContext context = new InitialContext();
            queueFactory = (QueueConnectionFactory)context.lookup("/ConnectionFactory");
            queueConnection = queueFactory.createQueueConnection();
            queueSession = queueConnection.createQueueSession
                                         (false,javax.jms.Session.AUTO_ACKNOWLEDGE);
            queue = (Queue)context.lookup("/queue/MyQueue");
            queueSender = queueSession.createSender(queue);
            message = queueSession.createTextMessage();
            
            System.out.println("Configuration For Bean Done");

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

    public void jspDestroy() {      }
 %>
<%

try {
            String resultMessage = request.getParameter("message");
            message.setText(resultMessage);
            queueSender.send(message);
       %>
      
   <p>
      <b>The result is:</b> <% response.getWriter().println("Message Sent Successfully"); %>
     <p>
        
  <%
            }// end of try
                catch (Exception e) {
                             e.printStackTrace ();
    }
%>
        
</body>
</html>

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 .Click next.
Enter the Name (ExampleMDBEAR ) and Click Next.In the Next screen select the Java EE modules (in this case both ExampleMDB and ExampleMDBWeb).Check the “Generate the application.xml” and click Finish.

Now once the ExampleMDBEAR 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 ExampleMDBEAR file and copy the file into all/deploy/ location. Start the Server.

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

2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) Created KernelDeployment for: EampleMDB.jar
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) installing bean: jboss.j2ee:ear=ExampleMDBEar.ear,jar=EampleMDB.jar,name=MessageMDBSample,service=EJB3
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)   with dependencies:
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)   and demands:
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)  jboss.ejb:service=EJBTimerService
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)   and supplies:
2011-08-29 08:21:14,807 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)  jndi:null
2011-08-29 08:21:14,822 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner)  Class:javax.jms.MessageListener
2011-08-29 08:21:14,822 INFO  [org.jboss.ejb3.deployers.JBossASKernel] (HDScanner) Added bean(jboss.j2ee:ear=ExampleMDBEar.ear,jar=EampleMDB.jar,name=MessageMDBSample,service=EJB3) to KernelDeployment of: EampleMDB.jar
2011-08-29 08:21:14,869 INFO  [org.jboss.ejb3.EJBContainer] (HDScanner) STARTED EJB: com.example.ExampleMDBean ejbName: MessageMDBSample
2011-08-29 08:21:15,086 INFO  [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/example

Check the Application using , http://localhost:Port Number>/example

More articles to come , Happy Coding.

1 comment :

  1. I used to be suggested this web site by means of my cousin. I'm now not positive
    whether or not this submit is written by means of him
    as no one else recognise such specific about my trouble.
    You're wonderful! Thanks!

    ReplyDelete