/java/

JEE EAR & WAR. Code separation.

2015-03-08 17:41:02

In this post you will find some simple example how to prepare application architecture base on EAR,EJB & WAR approach.

First things first. What we can use and why ?

As you know the application logic is not a business logic. That is why you want to keep them separately.

What we can use for spliting ?

We can use *.ear & *.war files.

In the *war file we will keep our application logic that mean controller, helper service & static resource such as js, css & html.

In the *ear file we will keep business logic, by this, I mean all service connecting and solving business problem. Important fact. We will store all this file in *ejb & *ejb-client package inside *ear file.

To do this we have to prepare some special structure.

Why we have to split our business logic into two packages ?

Because in ejb-client we store interface and DTO connecting whit them, and in ejb we have implementation of this interface.

Why we do something like this ?

Because we want to keep our business logic separately (ejb) from application logic (war), but we have to somehow connect them, and that is why we have to have ejb-client to share the interface.

Time for coding ... ah no... configuration

pom.xml - parent

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-v4_0_0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>project-parent</artifactid>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Parent Project</name>
<modules>
<module>../project-ejb-client</module>
<module>../project-ejb</module>
<module>../project-ear</module>
<module>../project-war</module>
</modules>
<build>
<pluginmanagement>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                        <target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginmanagement>
</build>
<dependencies></dependencies>
</project>

pom.xml - war

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<groupid>pl.btbw</groupid>
<artifactid>project-war</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<modelversion>4.0.0</modelversion>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>pl.btbw</groupid>
<artifactid>project-ejb-client</artifactid>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalname>${project.artifactId}</finalname>
</build>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
<failonmissingwebxml>false</failonmissingwebxml>
</properties>
</project>

pom.xml - ear

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<artifactid>project-ear</artifactid>
<groupid>pl.btbw</groupid>
<version>1.0-SNAPSHOT</version>
<packaging>ear</packaging>
<modelversion>4.0.0</modelversion>
<dependencies>
<dependency>
<groupid>pl.btbw</groupid>
<artifactid>project-ejb</artifactid>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                    <target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-ear-plugin</artifactid>
<configuration>
<version>1.4</version>
<modules>
<ejbmodule>
<groupid>pl.btbw</groupid>
<artifactid>project-ejb</artifactid>
</ejbmodule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.xml - ejb

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<artifactid>project-ejb</artifactid>
<groupid>pl.btbw</groupid>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<modelversion>4.0.0</modelversion>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>pl.btbw</groupid>
<artifactid>project-ejb-client</artifactid>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                    <target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-ejb-plugin</artifactid>
<configuration>
<ejbversion>3.1</ejbversion>
<archive>
<manifest>
<adddefaultimplementationentries>true</adddefaultimplementationentries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.xml - ejb-client

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<artifactid>project-ejb-client</artifactid>
<groupid>pl.btbw</groupid>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<modelversion>4.0.0</modelversion>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                    <target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-ejb-plugin</artifactid>
<configuration>
<ejbversion>3.1</ejbversion>
<archive>
<manifest>
<adddefaultimplementationentries>true</adddefaultimplementationentries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

Ok, we can create our first class, ok - interface

TestHelloServiceRemote.java in project-ejb-client

package pl.btbw;

import javax.ejb.Remote;

@Remote
public interface TestHelloServiceRemote {
    String getSomeText();
}

Time for implementation

TestHelloService.java in project-ejb

package pl.btbw;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
@LocalBean
public class TestHelloService implements TestHelloServiceRemote {
    @Override
    public String getSomeText() {
        return "Hi from Ejb!";
    }
}

Ok this is our business logic, very complicated :)

if you deploy project-ear.ear (I use Wildfly 8.1.0.Final), in the console, you should be able to see something like this:

but how to use service from ear in the war ?

we have to use @EJB with lookup:

HelloController.java in project-war

package pl.btbw.web.controller;

import pl.btbw.EjbConfig;
import pl.btbw.TestHelloServiceRemote;

import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/")
public class HelloController {

    @EJB(lookup = "java:global/project-ear-1.0-SNAPSHOT/project-ejb-1.0-SNAPSHOT/TestHelloService!pl.btbw.TestHelloServiceRemote")
    private TestHelloServiceRemote testHelloServiceRemote;

    @GET
    @Path("/ejb")
    @Produces(MediaType.APPLICATION_JSON)
    public Response doHiEjb() {
        return new Response(testHelloServiceRemote.getSomeText());
    }

}

This project you can find on GitHub