GWT EJB3 Maven JBoss 5.1 integration tutorial

Hello everyone,

In this article we are going to demonstrate how to properly integrate GWT and EJB3 in order to implement an example project, build it using maven and deploy it on JBoss 5.1 application server. Actually you can easily change the dependencies in the maven build file and deploy the project in your favorite application server. In order to be as generic as possible we will be using the command line flavors of maven and the gedit text editor.

First we have to create the pom project. We will call it ejb-gwt.

mkdir ejb-gwt 
cd ejb-gwt
gedit pom.xml 

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>com.javacodegeeks</groupid>
  <artifactid>ejb-gwt</artifactid>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>ejb-gwt</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
  </properties>
  <dependencies>
  </dependencies>
  <modules>
  </modules>
 <build>
  <plugins>
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <configuration>
     1.6
     <target>1.6</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Then we will create the module that will contain the ejb classes. We will call it ejb-jar.

mvn archetype:create -DgroupId=com.javacodegeeks -DartifactId=ejb-jar
cd ejb-jar/src/main
mkdir resources
cd resources
mkdir META-INF
cd META-INF
gedit persistence.xml

We will configure the persistence.xml to use the default datasource of jboss.

persistence.xml

<persistence version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyPersistenceUnit">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <jta-data-source>java:/DefaultDS</jta-data-source>
   <properties>
   <property name="hibernate.hbm2ddl.auto" value="update">
   <property name="hibernate.show_sql" value="true">
   <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect">
  </property></property></property></properties>
 </persistence-unit>
</persistence>

At src/java you will place your ejb code. In our example we have an entity bean

Employee.java

package com.javacodegeeks.ejb.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements java.io.Serializable {
 
 private static final long serialVersionUID = 7440297955003302414L;

 @Id
 @Column(name="employee_id")
 private long employeeId;
 
 @Column(name="employee_name", nullable = false, length=30)
 private String employeeName;
 
 @Column(name="employee_surname", nullable = false, length=30)
 private String employeeSurname;
 
 @Column(name="job", length=50)
 private String job;
  
 public Employee() {
 }

 public Employee(int employeeId) {
  this.employeeId = employeeId;  
 }

 public Employee(long employeeId, String employeeName, String employeeSurname,
   String job) {
  this.employeeId = employeeId;
  this.employeeName = employeeName;
  this.employeeSurname = employeeSurname;
  this.job = job;
 }

 public long getEmployeeId() {
  return employeeId;
 }

 public void setEmployeeId(long employeeId) {
  this.employeeId = employeeId;
 }

 public String getEmployeeName() {
  return employeeName;
 }

 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }

 public String getEmployeeSurname() {
  return employeeSurname;
 }

 public void setEmployeeSurname(String employeeSurname) {
  this.employeeSurname = employeeSurname;
 }

 public String getJob() {
  return job;
 }

 public void setJob(String job) {
  this.job = job;
 }
}

The local interface of a session bean.

EmployeeService.java

package com.javacodegeeks.ejb.session;

import com.javacodegeeks.ejb.entity.Employee;

public interface EmployeeService {
 public Employee findEmployee(long employeeId);
 public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void deleteEmployee(long employeeId) throws Exception;
}

And finaly the session bean.

EmployeeServiceBean.java

package com.javacodegeeks.ejb.session;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.javacodegeeks.ejb.entity.Employee;

@Stateless(name = "employeeService")
@Local(EmployeeService.class)
public class EmployeeServiceBean implements EmployeeService {
 @PersistenceContext
 private EntityManager entityManager;

 public void deleteEmployee(long employeeId) throws Exception {
  entityManager.remove(entityManager.find(Employee.class, employeeId));
 }

 public Employee findEmployee(long employeeId) {
  return entityManager.find(Employee.class, employeeId);
 }

 public void saveEmployee(long employeeId, String name, String surname,
   String jobDescription) throws Exception {
  Employee emp = new Employee();
  emp.setEmployeeId(employeeId);
  emp.setEmployeeName(name);
  emp.setEmployeeSurname(surname);
  emp.setJob(jobDescription);
  
  entityManager.persist(emp);

 }

 public void saveOrUpdateEmployee(long employeeId, String name,
   String surname, String jobDescription) throws Exception {
  Employee emp = new Employee();
  emp.setEmployeeId(employeeId);
  emp.setEmployeeName(name);
  emp.setEmployeeSurname(surname);
  emp.setJob(jobDescription);
  
  entityManager.merge(emp);
 }

 public void updateEmployee(long employeeId, String name, String surname,
   String jobDescription) throws Exception {
  Employee emp = entityManager.find(Employee.class, employeeId);
  emp.setEmployeeName(name);
  emp.setEmployeeSurname(surname);
  emp.setJob(jobDescription);
  
  entityManager.merge(emp);
 }

}

The pom.xml of ejb-jar module should be :

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <parent>
    <artifactid>ejb-gwt</artifactid>
    <groupid>com.javacodegeeks</groupid>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupid>com.javacodegeeks</groupid>
  <artifactid>ejb-jar</artifactid>
  <version>0.0.1-SNAPSHOT</version>
  <name>ejb-jar</name>
  <packaging>ejb</packaging>
  <url>http://maven.apache.org</url>

  <repositories>
   <repository>
    <id>jboss-maven2</id>
    <url>http://repository.jboss.com/maven2</url>
    </repository>
  </repositories>

  <dependencies>
   <dependency>
     <groupid>org.jboss.jbossas</groupid>
     <artifactid>jboss-as-client</artifactid>
     <version>5.1.0.GA</version>
     <type>pom</type>
     <scope>provided</scope>
 </dependency>
   <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.7</version>
      <scope>test</scope>
    </dependency> 
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-ejb-plugin</artifactid>
            <configuration>
                <ejbversion>3.0</ejbversion>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

We will now generate our web project based on gwt-maven-plugin archertype.

mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo -DarchetypeArtifactId=gwt-maven-plugin -DarchetypeVersion=1.2    -DgroupId=com.javacodegeeks    -DartifactId=war
cd war
gedit pom.xml

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  
  <modelversion>4.0.0</modelversion>
  <parent>
        <artifactid>ejb-gwt</artifactid>
        <groupid>com.javacodegeeks</groupid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
  <groupid>com.javacodegeeks</groupid>
  <artifactid>war</artifactid>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version> 
  <properties>
      <gwt.version>2.0.4</gwt.version>
      <maven.compiler.source>1.6</maven.compiler.source>
      <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupid>com.google.gwt</groupid>
      <artifactid>gwt-servlet</artifactid>
      <version>${gwt.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupid>com.google.gwt</groupid>
      <artifactid>gwt-user</artifactid>
      <version>${gwt.version}</version>  
      <scope>provided</scope>
    </dependency>
    <dependency>
     <groupid>org.jboss.jbossas</groupid>
     <artifactid>jboss-as-client</artifactid>
     <version>5.1.0.GA</version>
     <type>pom</type>
     <scope>provided</scope>
 </dependency>
 <dependency>
    <groupid>com.javacodegeeks</groupid>
    <artifactid>ejb-jar</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <type>ejb</type>
    <scope>provided</scope>
   </dependency>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
  <finalname>war</finalname>
    <outputdirectory>war/WEB-INF/classes</outputdirectory>
    <plugins>
      <plugin>
        <groupid>org.codehaus.mojo</groupid>
        <artifactid>gwt-maven-plugin</artifactid>
        <version>1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>generateAsync</goal>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <runtarget>com.javacodegeeks.war.Application/Application.html</runtarget>
        </configuration>
      </plugin>
      <plugin>
          <groupid>org.apache.maven.plugins</groupid>
          <artifactid>maven-compiler-plugin</artifactid>
          <version>2.0.2</version>
          <configuration>
            ${maven.compiler.source}
            <target>${maven.compiler.target}</target>
          </configuration>
      </plugin>
    </plugins>
  </build>

</project>
cd src/main/webapp/WEB-INF
gedit web.xml

web.xml

<web-app version="2.5" 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/web-app_2_5.xsd">
 <display-name>GWT-Maven-Archetype</display-name> 
 <servlet>
    <servlet-name>emp</servlet-name>
    <servlet-class>com.javacodegeeks.war.server.services.EmployeeServiceGWTImpl</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>emp</servlet-name>
    <url-pattern>/com.javacodegeeks.war.Application/emp</url-pattern>
  </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
</web-app>

web.xml should apply at 2.5 specification if we want to have ejb injection at servlet container.

Now let’s take a look at the gwt service.

EmployeeServiceGWT.java

package com.javacodegeeks.war.shared.services;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.javacodegeeks.war.shared.entity.EmployeeUtil;

@RemoteServiceRelativePath("emp")
public interface EmployeeServiceGWT  extends RemoteService  {
 public EmployeeUtil findEmployee(long employeeId);
 public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;
 public void deleteEmployee(long employeeId) throws Exception;
}

Notice tha the RemoteServiceRelativePath is related with the servlet path /com.javacodegeeks.war.Application/emp that we have defined in the web.xml

EmployeeServiceGWTImpl.java

package com.javacodegeeks.war.server.services;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.javacodegeeks.ejb.entity.Employee;
import com.javacodegeeks.ejb.session.EmployeeService;
import com.javacodegeeks.war.shared.entity.EmployeeUtil;
import com.javacodegeeks.war.shared.services.EmployeeServiceGWT;

public class EmployeeServiceGWTImpl extends RemoteServiceServlet implements EmployeeServiceGWT{

 /**
  * 
  */
 private static final long serialVersionUID = 5995064321382986251L;
 
 private EmployeeService employeeService;
 @Resource(name="java:UserTransaction") UserTransaction ut;


 
 @EJB
 public void setEmployeeService(EmployeeService employeeService) {
  this.employeeService = employeeService;
 }

 public void deleteEmployee(long employeeId) throws Exception {
  employeeService.deleteEmployee(employeeId);
  
 }

 public EmployeeUtil findEmployee(long employeeId) {
  
  Employee emp = employeeService.findEmployee(employeeId);
  if(emp != null) {
   try {
    ut.begin();
    EmployeeUtil util = new EmployeeUtil(emp.getEmployeeId(), emp.getEmployeeName(), emp.getEmployeeSurname(), emp.getJob());
    
    ut.commit();
    return util;
   } catch (Exception e) {
   }
   
  }
  return null;
 }

 public void saveEmployee(long employeeId, String name, String surname,
   String jobDescription) throws Exception {
  employeeService.saveEmployee(employeeId, name, surname, jobDescription);
  
 }

 public void saveOrUpdateEmployee(long employeeId, String name,
   String surname, String jobDescription) throws Exception {
  employeeService.saveOrUpdateEmployee(employeeId, name, surname, jobDescription);
  
 }

 public void updateEmployee(long employeeId, String name, String surname,
   String jobDescription) throws Exception {
  employeeService.updateEmployee(employeeId, name, surname, jobDescription);
  
 }

}

As you can see ,we inject the employeeService session bean using the annotation @EJB.
Notice that we use EmployeeUtil class for a basic reason:
GWT cannot handle lazy initialized relationship so we replace proxy collection with real collections.The replacement should be in a transaction scope .

package com.javacodegeeks.war.shared.entity;

import java.io.Serializable;

public class EmployeeUtil implements Serializable  {
 
 /**
  * 
  */
 private static final long serialVersionUID = -2732740011239267035L;

 private long employeeId;
 
 private String employeeName;
 
 private String employeeSurname;
 
 private String job;
 
 
 public EmployeeUtil() {
 }

 public EmployeeUtil(int employeeId) {
  this.employeeId = employeeId;  
 }

 public EmployeeUtil(long employeeId, String employeeName, String employeeSurname,
   String job) {
  this.employeeId = employeeId;
  this.employeeName = employeeName;
  this.employeeSurname = employeeSurname;
  this.job = job;
 }

 public long getEmployeeId() {
  return employeeId;
 }

 public void setEmployeeId(long employeeId) {
  this.employeeId = employeeId;
 }

 public String getEmployeeName() {
  return employeeName;
 }

 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }

 public String getEmployeeSurname() {
  return employeeSurname;
 }

 public void setEmployeeSurname(String employeeSurname) {
  this.employeeSurname = employeeSurname;
 }

 public String getJob() {
  return job;
 }

 public void setJob(String job) {
  this.job = job;
 }

}

The rest of the source code for the gwt interface is provided in the source code of this article.

Now we have on final step,to build the ear.We will create a module to do that .

mvn archetype:create -DgroupId=com.javacodegeeks -DartifactId=ear
cd ear
rm -rf src
gedit pom.xml

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelversion>4.0.0</modelversion>
  <parent>
    <artifactid>ejb-gwt</artifactid>
    <groupid>com.javacodegeeks</groupid>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupid>com.javacodegeeks</groupid>
  <artifactid>ear</artifactid>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>ear</packaging>
  <name>ear</name>
  <description>ear for gwt ejb3</description>
  <dependencies>
   <dependency>
    <groupid>com.javacodegeeks</groupid>
    <artifactid>ejb-jar</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <type>ejb</type>
   </dependency>
    <dependency>
    <groupid>com.javacodegeeks</groupid>
    <artifactid>war</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <type>war</type>
   </dependency>
  </dependencies>

 <build>
    <plugins>
      <plugin>
        <groupid>org.apache.maven.plugins</groupid>
        <artifactid>maven-ear-plugin</artifactid>
        <configuration>
          <generateapplicationxml>true</generateapplicationxml>
          <modules>
            <webmodule>
              <groupid>com.javacodegeeks</groupid>
              <artifactid>war</artifactid>
              <contextroot>ejb-gwt</contextroot>
            </webmodule>
            <ejbmodule>
              <groupid>com.javacodegeeks</groupid>
              <artifactid>ejb-jar</artifactid>
            </ejbmodule>
         </modules>
       </configuration>
     </plugin>
   </plugins>
  </build>
</project>

To build the project go to the root project and type :
mvn install

The source code for this article is located here .

Thanks

./pat

Related Articles :

Related Whitepaper:

Java Application Development on Linux

Linux is the fastest-growing Java development platform because it saves money and time by serving as a platform for both development and deployment. But developers face significant platform-specific challenges when managing and deploying Java applications in a controlled production environment.

This is the hands-on guide to the full Java application development lifecycle on Linux. It demonstrates the platform, tools, and application development by showing realistic, easy-to-follow examples. After a simple command-line application introduces basic tools, this program leads readers through business-logic object analysis, database design, Java servlet UIs, Java Server Pages (JSP) UIs, Swing GUIs, and Standard Widget Toolkit (SWT) GUIs. Scaling up to the enterprise level provides the opportunity to use both the JBoss Application Server and the Apache Geronimo Application Servers, and Enterprise JavaBeans (EJB).

Get it Now!  

One Response to "GWT EJB3 Maven JBoss 5.1 integration tutorial"

  1. Emilio Tirado says:

    Hi Pat,

    I am new to this maven world, and i am wondering if you know (or you can build) a project like the tutorial but with the technologies updated(EJB 3.1, JBoss AS 7.1.1.Final, GWT 2.5.1 Java 1.7 Maven 3 JUnit 4), because, changing the dependencies to the recent version is driving me crazy…

    hope you can

Leave a Reply


× 1 = seven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books