Вы находитесь на странице: 1из 9

EJB3, STEP BY

STEP BY
EXAMPLE

[Pick the date] Martin_m_nad@yahoo.com

This documentation will show you how you can build EJB3
application with help maven and eclipse.o
Ejb3, Step by step by example

STATELESS BEAN
This kind of the EJb-bean wouldn’t support any informal state of client.

Stateless beans can be any kind of java class and ithas an interface for business-logic and an implementation
of business-logic

You should use @stateless annotation in you implemented class or denoted stateless if you are using
descriptor method.

You should very carefull if you are using mixed mode, I mean if you are using both annotation and
descriptor. if you use @stateless on your implemented and denoted another kind of session bean on
descriptor, the deployment process will rewrite your annotation on your statless-bean.

example|one
By using eclipse just make a new maven project.

Whit this hierarchy:

You will need add those packages in your classpath:

Javaee.jar

Ejb3-api.jar
Servlet-api.jar

Make one interface and implemnet class for helloworld as following:


package martin.nad.org.ejb3.stateless.example1;

public class HelloWorldImpl implements HelloWorldInterface {

public String sayHello() {

return "Hello world";


}

package martin.nad.org.ejb3.stateless.example1;

public interface HelloWorldInterface {


public String sayHello();
}

Put @Remote on interface and @Stateless on implementation.


package martin.nad.org.ejb3.stateless.example1;
import javax.ejb.Remote;

@Remote
public interface HelloWorldInterface {
public String sayHello();
}

package martin.nad.org.ejb3.stateless.example1;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldImpl implements HelloWorldInterface {

public String sayHello() {

return "Hello world";


}

Write a ant-script to build your application as following:

You should to have build tre differnet kind of package


1- Jar files, for your stateless-session-bean application

2- War files, for your webapplications

3- Ear file

And you have at least three differents target, one for jar, one for war and one for ear

Here is an example how an ant-script can be:


<?xml version="1.0"?>

<project name="EJB3 Stateless Session Bean example1ant" default="all" basedir=".">

<target name="init">
<!-- Define -->
<property name="dirs.base" value="${basedir}" />
<property name="classdir" value="${dirs.base}/build/classes" />
<property name="src" value="${dirs.base}/src/java" />
<property name="web" value="${dirs.base}/src/main/webapp" />
<property name="resources" value="${dirs.base}/src/main/resources" />

<property name="warpackage" value="statelessExample1.war" />


<property name="earPackage" value="statelessExample1.ear" />
<property name="jarPackage" value="statelessExample1.jar" />

<property name="earDirectory" value="${dirs.base}/build/ear" />


<property name="warDirectory" value="${dirs.base}/build/war" />
<property name="jarDirectory" value="${dirs.base}/build/jar" />

<!-- classpath for Project -->


<path id="library.classpath">
<pathelement path="lib/servlet-api.jar" />
<pathelement path="lib/ejb3-persistence.jar" />
<pathelement path="lib/javaee.jar" />
<pathelement path="${classpath}" />
</path>

<mkdir dir="${warDirectory}/WEB-INF" />


<mkdir dir="${warDirectory}/WEB-INF/classes" />

<mkdir dir="${earDirectory}/META-INF" />


<mkdir dir="${jarDirectory}/META-INF" />
<mkdir dir="${classdir}" />

</target>

<!-- Main target -->


<target name="all" depends="init,build,buildWar,buildJar,buildEar" />

<target name="build">
<javac srcdir="${src}" destdir="${classdir}" debug="true"
includes="**/*.java">
<classpath refid="library.classpath" />
</javac>
</target>

<!-- Create the jar File -->


<target name="buildJar" depends="init">
<copy todir="${jarDirectory}">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>

<copy todir="${jarDirectory}/META-INF">
<fileset dir="${resources}/jar/" includes="ejb-jar.xml" />
</copy>

<jar jarFile="${earDirectory}/${jarPackage}" basedir="${jarDirectory}"


/>

</target>

</project>

Now you have made the jar file and it is fine.

We will look at the web application:

You should just make a jsp file and put it on the webapp with the default web.xml and put additional
target for webapp on the ant-script.

You should make a new target for ear-package too.

The ant-target for webapplication looks like:


<target name="buildWar" depends="init">
<copy todir="${warDirectory}/WEB-INF/classes">
<fileset dir="${classdir}" includes="**/*.class" />
</copy>

<copy todir="${warDirectory}/WEB-INF">
<fileset dir="${warDirectory}/WEB-INF" includes="web.xml" />
</copy>

<copy todir="${warDirectory}">
<fileset dir="${web}" includes="**/*.*" />
</copy>

<jar jarFile="${earDirectory}/${warpackage}" basedir="${warDirectory}"


/>
</target>
If you want to make ear-file you need an application.xml file too, to describe the servicenames and
moudule you want to use them.

For this example application.xml looks like:


<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd">

<display-name>Stateless Session Bean Example</display-name>


<module>
<web>
<web-uri>statelessExample1.war</web-uri>
<context-root>/statelessExample1</context-root>
</web>
</module>

<module>
<ejb>statelessExample1.jar</ejb>
</module>

</application>

And the target part for ear is:


<target name="buildEar" depends="init">

<copy todir="${earDirectory}/META-INF">
<fileset dir="${resources}/ear" includes="application.xml" />
</copy>

<jar jarFile="../${earPackage}" basedir="${earDirectory}" />


</target>

Run your ant and see if everything working or not, and if you want to debug your ant, it is good idea,
do it now.

And we will back to the index.jsp

Index.jsp/servlet, should call the beans which you want to use it.

In our case we will call helloworldbean as follows:

The jsp file can look like:


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="javax.naming.*, martin.nad.org.ejb3.stateless.example1.*"%>
<%!
HelloWorldStatefullInterface helloWorld = null;

public void jspInit() {

try {

InitialContext context = new InitialContext();

helloWorld =(HelloWorldStatefullInterface)
context.lookup("statefullExample1/HelloWorldImplStatefull/remote");

System.out.println("Loaded Account Bean");

} catch (Exception ex) {

System.out.println("Error:"+

ex.getMessage());

public void jspDestroy() {

helloWorld = null;

%>

<html>

<body>

<h2>Hello World!</h2>

<%String response2=helloWorld.sayHello(); %>

<h3><%=response2%>

<% helloWorld.setMember("is initated");%>

<%=helloWorld.sayHello() %>

<%=helloWorld.sayHello() %>

<%=helloWorld.sayHello() %></h3>

</body>
</html>

Congratulation, you make the first stateless helloworld.

There is som lines which we should look at them closer:

You need call the stateless-sesssion-bean by calling context. As showed before:


InitialContext context = new InitialContext();
helloWorld =(HelloWorldInterface)
context.lookup("statelessExample1/HelloWorldImpl/remote");
and the rest is as usual you can do what you want to do. That’s all.

When you create a statless session bean, you will get the same instance form pool.

If you just run this hello application and just press f5, you noticed that it will keep the state, but it's
not, there is not guarantee for this, if you just wait some minutes or houres, and if you press f5, it will
clean the proccess and recreate from new stance, and even in meddle of you running-application, it
can reset the state.

Statfull session bean


As you can understand and as before we look at it and the name of bean, with this kind of session
bean, you can keep the the state of session in you application-life.

We will look at an exampl.

We will have the same ant-script and same heirarchey.

But the differnces, wi will have the @Statefull anotation instead @Stateless

If you run the application, you will see the state of the bean should be keept and this kind of session
will guarantee that the state should be keept.

The statefull session bean has tre differents transaction notification:

afterbegain: intimate if a new transaction has begun

beforecompletion: intimate if a transaction is going to commited

aftercompletion: intimate if a transaction is complate.

Here you can also use SessionSynchronization interface to get the aftercompletion.

There are 4 callback event :

@PostConstruct: it happens after all dependency injection and before the first calling of the method

@PreDestroy: it run after any method with an @remove


@PostActivate: it callback if application will use an passviated session-bean

@PrePassivate: if instace get too longtime

Вам также может понравиться