Ejb3, Step by Step by Example: (Pick The Date)
Ejb3, Step by Step by Example: (Pick The Date)
STEP BY
EXAMPLE
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.
Javaee.jar
Ejb3-api.jar
Servlet-api.jar
package martin.nad.org.ejb3.stateless.example1;
@Remote
public interface HelloWorldInterface {
public String sayHello();
}
package martin.nad.org.ejb3.stateless.example1;
import javax.ejb.Stateless;
@Stateless
public class HelloWorldImpl implements HelloWorldInterface {
3- Ear file
And you have at least three differents target, one for jar, one for war and one for ear
<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" />
</target>
<target name="build">
<javac srcdir="${src}" destdir="${classdir}" debug="true"
includes="**/*.java">
<classpath refid="library.classpath" />
</javac>
</target>
<copy todir="${jarDirectory}/META-INF">
<fileset dir="${resources}/jar/" includes="ejb-jar.xml" />
</copy>
</target>
</project>
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.
<copy todir="${warDirectory}/WEB-INF">
<fileset dir="${warDirectory}/WEB-INF" includes="web.xml" />
</copy>
<copy todir="${warDirectory}">
<fileset dir="${web}" includes="**/*.*" />
</copy>
<module>
<ejb>statelessExample1.jar</ejb>
</module>
</application>
<copy todir="${earDirectory}/META-INF">
<fileset dir="${resources}/ear" includes="application.xml" />
</copy>
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.
Index.jsp/servlet, should call the beans which you want to use it.
try {
helloWorld =(HelloWorldStatefullInterface)
context.lookup("statefullExample1/HelloWorldImplStatefull/remote");
System.out.println("Error:"+
ex.getMessage());
helloWorld = null;
%>
<html>
<body>
<h2>Hello World!</h2>
<h3><%=response2%>
<%=helloWorld.sayHello() %>
<%=helloWorld.sayHello() %>
<%=helloWorld.sayHello() %></h3>
</body>
</html>
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.
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.
Here you can also use SessionSynchronization interface to get the aftercompletion.
@PostConstruct: it happens after all dependency injection and before the first calling of the method