I have a dynamic web project which I deploy in EAR format on JBoss 5. Using ant I compile and deploy the ear file on application server. If I deploy EJB and war (client) independently then everything work but when i try to package in ear session bean does not bound. My EJB code and client(Web) are in same project I do not have Independent EJB modules in project. I tried adding jar out of business interface in WEB-INF lib and even in lib directory of ear file but nothing worked. I want to know If EJB jar is not in the class path and how to set order of modules in ear file?
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="Application_ID" version="5">
<display-name>X3</display-name>
<module>
<web>
<web-uri>x3.war</web-uri>
<context-root>x3</context-root>
</web>
</module>
<module>
<ejb>x3.jar</ejb>
</module>
</application>
build.xml
<?xml version="1.0"?>
<!DOCTYPE xml>
<project name="x3" default="all" basedir=".">
<property name="compiledWebClasses" value="${basedir}/build/classes" />
<property name="dist" value="${basedir}/dist" />
<property name="descriptors" value="${basedir}/dd" />
<property name="build" value="${basedir}/build" />
<property name="JBOSS_CLIENT_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\client" />
<property name="JBOSS_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\lib" />
<property name="JBOSS_COMMON_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\common\lib" />
<path id="compile.classpath">
<fileset dir="${JBOSS_CLIENT_LIB}">
<include name="*.jar"/>
</fileset>
<fileset dir="${JBOSS_LIB}">
<include name="*.jar"/>
</fileset>
<fileset dir="${JBOSS_COMMON_LIB}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${basedir}/build" />
<delete dir="${basedir}/dist" />
</target>
<target name="createFolders">
<mkdir dir="${basedir}/build/classes" />
<mkdir dir="${basedir}/dist" />
</target>
<target name="compile">
<javac srcdir="src" destdir="build/classes" debug="true">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="jar">
<jar jarfile="${build}/x3.jar">
<fileset dir="${compiledWebClasses}">
<include name="**/ejb/**"/>
</fileset>
<metainf dir="${descriptors}">
<include name="persistence.xml"/>
</metainf>
</jar>
</target>
<fileset dir="${compiledWebClasses}" id="warClasses.fileset">
<include name="**/controller/**" />
</fileset>
<target name="war">
<war destfile="${build}/x3.war" webxml="${basedir}/WebContent/WEB-INF/web.xml">
<fileset dir="${basedir}/WebContent"/>
<classes refid="warClasses.fileset"/>
</war>
</target>
<target name="ear">
<jar destfile="${dist}/x3.ear">
<metainf dir="${descriptors}">
<include name="application.xml"/>
<include name="jboss-app.xml"/>
</metainf>
<fileset file="${build}/x3.war"/>
<fileset file="${build}/x3.jar"/>
</jar>
</target>
<target name="all" depends="clean, createFolders, compile, jar, war, ear">
<echo message="done" />
</target>
</project>
jboss-app.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jboss-app>
<loader-repository>dev.lmd.com:loader=x3.ear</loader-repository>
</jboss-app>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="StudentMgtPU"
transaction-type="JTA">
<jta-data-source>java:/StudentMgtDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
Business Interface
package com.lmd.dev.ejb.session;
import javax.ejb.Local;
import com.lmd.dev.ejb.domain.Student;
@Local
public interface ManageStudentSessionBeanLocal {
public boolean addStudent(Student Student);
}
Business Logic
package com.lmd.dev.ejb.session;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.lmd.dev.ejb.domain.Student;
/**
* Session Bean implementation class ManageStudentSessionBean
*
* @author Sameera Jayasekara
*/
@Stateless
public class ManageStudentSessionBean implements ManageStudentSessionBeanLocal {
@PersistenceContext
private EntityManager entityManager;
public boolean addStudent(Student student) {
System.out.println("Trying .............");
entityManager.persist(student);
return true;
}
}
Servlet (Client)
package com.lmd.dev.controller;
import java.io.IOException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lmd.dev.ejb.domain.Student;
import com.lmd.dev.ejb.session.ManageStudentSessionBeanLocal;
/**
*
*
*
*/
public class ManageStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB(mappedName="ManageStudentSessionBean/local")
private ManageStudentSessionBeanLocal manageStudentSessionBeanLocal;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String message = "";
String firstName = request.getParameter("fname");
String lastName = request.getParameter("lname");
String email = request.getParameter("email");
Student student = new Student();
student.setFirstName(firstName);
student.setLastName(lastName);
student.setEmail(email);
if (manageStudentSessionBeanLocal != null && manageStudentSessionBeanLocal.addStudent(student)) {
message = "Student Successfuly Added";
} else {
message = "Student Adding Failed";
}
request.setAttribute("message", message);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>x3</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ManageStudentServlet</display-name>
<servlet-name>ManageStudentServlet</servlet-name>
<servlet-class>com.lmd.dev.controller.ManageStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManageStudentServlet</servlet-name>
<url-pattern>/ManageStudentServlet</url-pattern>
</servlet-mapping>
</web-app>
You can find source code on git as well
@Stateless(name="manageStudentSessionBean")
to the EJB module instead of just@Stateless
and in your servlet use the annotation@EJB(name = "manageStudentSessionBean")
above the entryprivate ManageStudentSessionBeanLocal manageStudentSessionBeanLocal = null;
– JGlass