Tag: sample

EJB3.0 TimerService Example/Demo

Hi,

Jay SenSharma

Jay SenSharma

The EJB3 timer service is an EJB-container provided service that allows you to create timers that schedule callbacks to occur when a timer object expires. Timer objects can be created for entity beans, message-driven beans, and stateless session beans. Timer objects expire at a specified time, after an elapsed period of time, or at specified intervals. For instance, you can use the timer service to send out notification when an EJB remains in a certain state for an elapsed period of time.  http://download.oracle.com/docs/cd/E15051_01/wls/docs103/ejb/implementing.  html

Here we are going to create a very simple EJB3 Stateless Timer.

Step1).  Create a Directory somewhere in your filesystem: F:DELETEEJB3_TimerService

Step2).  Create a Directory “EJBTimerTest” inside “F:DELETEEJB3_TimerService

Step3). Now Create 3 folders inside “F:DELETEEJB3_TimerServiceEJBTimerTest” with the following names “META-INF”, “MyEJBApp” and “MyWebApp”.

Step4). Write the “JackTimer.java” interface inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyEJBApp” directory.

package ejbs;
import javax.ejb.*;
@Local
public interface JackTimer
{
public void createTimer();
}
Step5). Write the StatelessBean “JackTimerBean.java” inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyEJBApp” directory
package ejbs;
import static javax.ejb.TransactionAttributeType.NOT_SUPPORTED;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.Local;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

@Stateless (name="JackTimerBean", mappedName="JackTimerBean")
@Local (JackTimer.class)
@TransactionManagement(value=TransactionManagementType.BEAN )
@TransactionAttribute(value=NOT_SUPPORTED)
public class JackTimerBean implements JackTimer
{

@Resource
TimerService timerService;
public void createTimer()
{
System.out.println("ntinside createTimer() ....GOT the injected TimerService: "+timerService);
System.out.println("ntCrating timer");
System.out.println("nttimerService [" + timerService + "]");
Timer timer = timerService.createTimer(20000,"Created new timer");
}

@Timeout
public void timeout(Timer timer)
{
System.out.println("ntTimeout occurred !!!");
}
}
Step6). compile the above two classes…   (Your EJB3.0 Timer is ready)

javac -d . *.java

Step7). Now move inside your WebApplication “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebApp” and then write “index.jsp” page…

<html>
<body>
<h3>
<center><BR><BR>
<a href="TestServlet">Click Here to See EJB Injection in a Servlet...(It will Succeed)</a>
</body>
</html>
Step8). Place the following “web.xml” file indide “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebAppWEB-INF”
<?xml version="1.0"?>
<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" version="2.5">

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>TestServlet</url-pattern>
</servlet-mapping>

</web-app>

Step9). Now write “TestServlet.java” class inside “F:DELETEEJB3_TimerServiceEJBTimerTestMyWebAppWEB-INFclasses”
package servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.EJB;
public class TestServlet extends HttpServlet
{
@EJB
private ejbs.JackTimer reconTimer;

public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
  {
  PrintWriter out=res.getWriter();
  out.println("ntTestServlet service()nt EJBBean injection: reconTimer = "+reconTimer);
  System.out.println("nnt EJBBean injection: reconTimer = "+reconTimer);
  reconTimer.createTimer();
 }
}
Step10). Compile the “TestServlet.java”
javac -d . TestServlet.java
Step11). write “application.xml” file inside… “F:DELETEEJB3_TimerServiceEJBTimerTestMETA-INF” directory
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
<description>Split Directory Example</description>
<display-name>Split Directory Example</display-name>

<module>
<ejb>MyEJBApp</ejb>
</module>

<module>
<web>
<web-uri>MyWebApp</web-uri>
<context-root>MyWebApp</context-root>
</web>
</module>

</application>
.
Step12). Write the following “build.xml” file to deploy and undeploy your Enterprise application “EJBTimerTest”.(your EAR application Name)…
Please place the build.xml file in the following location (Just beside your EAR application): “F:DELETEEJB3_TimerService”
<project name="webservices-hello_world" default="deploy">
<!-- set global properties for this build -->
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />

<target name="deploy">
<wldeploy action="deploy"  name="EJBTimerTest" source="EJBTimerTest" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />
</target>

<target name="undeploy">
<wldeploy action="undeploy" name="EJBTimerTest" failonerror="false" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />
</target>
</project>

.
Step 13). Now open a Shell prompt and then run “setWLSEnv.sh” then move to “F:DELETEEJB3_TimerService” directory in the Shell …and finally run the ANT
Step 14). Hit the URL:    http://localhost:7001/MyWebApp/index.jsp
As soon as you hit the Servlet …you will see that the EJB3.0 Timer created…and after “20000 millisecond” (20-seconds) it will timeout…as mentioned in the Bean class “JackTimerBean.java”.  The output of this program execution you can see in the Server STDOUT.
.
.
.
Thanks
Jay SenSharma

WebLogic 10.3 EJB3 Local Lookup Sample

Hi,

Jay SenSharma

Jay SenSharma

Here is a very simple Step by Step instructions to develope a EJB3 Stateful Session Bean in WebLogic Server which allows us to perform a Local JNDI lookup.

Developing Local Lookup EJB3 in WebLogic 10.3 . . . .

This Sample demonstrates that how we can develop an EJB 3 Stateless Session Bean in WebLogic 10.3 by providing a custom Local JNDI Name.

Step 1). First of all we need to develop a EJB Local business interface “ReplicableSFSLocalInt.java” like following.

package sfsb3;
public interface ReplicableSFSLocalIntf
{
public void removeWithRetain() throws Exception;
public void removeWithoutRetain() throws Exception;
public String printBean ();
public int testIncrement() throws Exception;
public int testDecrement() throws Exception;
public int getCount() throws Exception;
}

Step 2). Now we are going to develop the EJB3 Stateless Session Bean class using @Stateful annotation.

package sfsb3;
import javax.ejb.*;
@Stateful(mappedName = "ejb/ReplicableSFSBean")
@Local(ReplicableSFSLocalIntf.class)
public class ReplicableSFSBean mplements ReplicableSFSLocalIntf
{
int count = 0;
public ReplicableSFSBean()
{
System.out.println("nt ReplicableSFSBean-->  ReplicableSFSBean() invoked");
}

@Init("create")
public void initMethod() throws CreateException
{
System.out.println("nt ReplicableSFSBean-->  public void initMethod() invoked");
}

@Remove(retainIfException=true)
public void removeWithRetain() throws Exception
{
System.out.println("nt ReplicableSFSBean-->  removeWithRetain() invoked");
}

@Remove
public void removeWithoutRetain() throws Exception
{
System.out.println("nt ReplicableSFSBean-->  removeWithoutRetain() invoked");
}

public String printRemoteIntf ()
{
System.out.println("nt ReplicableSFSBean --->  public String printRemoteIntf ()  invoked");
return "ReplicableSFSRemoteObjectIntf";
}

public String printLocalIntf ()
{
System.out.println("nt ReplicableSFSBean --->  public String printLocalIntf ()  invoked");
return "ReplicableSFSLocalObjectIntf";
}

public String printBean ()
{
System.out.println("nt ReplicableSFSBean --->  public String printBean ()  invoked");
return "ReplicableSFSBean";
}

public int testIncrement() throws Exception
{
System.out.println("nt ReplicableSFSBean ---> public int testIncrement()  invoked");
count=count+5;
return count;
}

public int testDecrement() throws Exception
{
System.out.println("nt ReplicableSFSBean --->  public int testDecrement()  invoked");
count=count-2;
return count;
}

public int getCount() throws Exception
{
System.out.println("nt ReplicableSFSBean --->  public int getCount()  invoked");
return count;
}
}

Step3).  We need to compile above two classes using

javac  –d  .  ReplicableSFSLocalIntf.java
javac  -d . ReplicableSFSBean.java

Step 4). We Need to Create a Jar file out of these two classes like below

jar  -cvf  Test.jar  sfsb3  *.java

Step 5). Now we need to develop a Web Application to invoke this EJB using its Local JNDI Name.

Developing “index.jsp” like following…

<html>
<head> <title>Test Local Lookup in WebLogic 10.3</title> </head>
<body>
<centre> <h2> Sample For EJB3 Local Lookup in WebLogic 10.3<br>
<h3><a href=first.jsp>Lookup for Local EJB3 Click Here</a>
</center>
</body>
</html>

Step 6). developing the “first.jsp” to write the Local Lookup code.

<%@ page import="java.util.*,javax.naming.*" %>
<%
Context env = (Context) new InitialContext();
try{
Object obj=env.lookup("java:comp/env/ejb/ReplicableSFSBean");
out.println("<BR><BR>obj=>"+obj.getClass()+"t"+obj);

sfsb3.ReplicableSFSLocalIntf  local=(sfsb3.ReplicableSFSLocalIntf )obj;
out.println("<BR><BR>local=>"+local.getClass()+"t"+local);

out.println("<BR><BR>local testIncrement() =>"+local.testIncrement());
}
catch(Exception e)
{
out.println("<BR><BR>Exception => "+e);
e.printStackTrace();
}
%>

EJB3 Stateless Session Bean invoked Successfully in WebLogic 10.3 Using Local Lookup.

Step7). Provide the “web.xml” like following:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<error-page>
<error-code>500</error-code>
<location>/error.html</location>
</error-page>

<ejb-local-ref>
<ejb-ref-name>ejb/ReplicableSFSBean</ejb-ref-name>
<local>sfsb3.ReplicableSFSLocalIntf</local>
</ejb-local-ref>

</web-app>

———————————-

Step 8). Creating an EAR file with above Web Application and Test.jar EJB, using META-INF/application.xml  file like following

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>
<application>
<display-name></display-name>
<module>
<ejb>Test.jar</ejb>
</module>
<module>
<web>
<web-uri>WebApp1</web-uri>
<context-root>WebApp1</context-root>
</web>
</module>
</application>

Step 9). Now deploy the EAR File on WebLogic Server and access the WebApp1 index.jsp page.

.

==============================================================
But if you are trying to inject an EJB  Bean inside your Servlet (WebApplication) using @EJB annotation then you must make sure that you are using the following link of “webxml”   which uses Servlet 2.5  XSD specification:

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

</webapp>

.

Regards
Jay SenSharma


Copyright © 2010-2012 Middleware Magic. All rights reserved. |