Tag: EJB3

Tuning EJB3 Application using Plan.xml

Hi,

Jay SenSharma

Jay SenSharma

In response to comment posted regarding tunning of EJB3 app:   http://middlewaremagic.com/weblogic/?p=1696#comment-1356

Here is a Simple Demonstration of Changing the Tunable Parameters of an EJB3.0 application using “Plan.xml”. As we have many annotations available as part of EJB3.0 Specifications But we dont have the Annotations available for many Tunable parameters like “max-beans-in-free-pool”, “initial-beans-in-free-pool”…etc. If we want to change/add these values for our EJB3.0/EJB2.x Application then we have have to change provide our own “weblogic-ejb-jar.xml” file.

But In Most of the production scenarios The WebLogic Administrator doesnt have priviledge to change any thing inside the EJB jar module. So in that case A WebLogic AdminiStartor can use the “Plan.xml” file to change these Tunable Parameters Settings…

Step1). Create a Directory somewhere in your file system.
Example: C:EJB3_Plan_XML_Demo

Step2). Create a Directory “EJB3App” inside Directory “C:EJB3_Plan_XML_Demo”

Step3). Provide the EJB3 Business Interface “CalculatorRemote.java” inside “C:EJB3_Plan_XML_DemoEJB3App”

package calculator;
import javax.ejb.*;
@Remote
public interface CalculatorRemote
{
public int add(int x,int y);
public int sub(int x,int y);
public int mult(int x,int y);
public int div(int x,int y);
}

Step4). Provide the EJB3 Stateless Bean Implementation class “CalculatorBean.java” inside “C:EJB3_Plan_XML_DemoEJB3App”

package calculator;
import javax.ejb.*;
@Stateless
public class CalculatorBean implements CalculatorRemote
{
public int add(int x,int y)
{
return (x+y);
}
public int sub(int x,int y)
{
return(x-y);
}
public int mult(int x,int y)
{
return (x*y);
}
public int div(int x,int y)
{
return (x/y);
}
}

Step5). Now Create a directory “META-INF” inside “C:EJB3_Plan_XML_DemoEJB3App” and then provide an Empty “weblogic-ejb-jar.xml” file as following:

<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar/1.0/weblogic-ejb-jar.xsd">
</weblogic-ejb-jar>

NOTE: Step5 is the Most important Step because if we want to override setting of any descriptor then at least that file must exist inside our application….doesn’t matter if it is Empty or having some contents inside it.

Step6). Now a command prompt and the run the “setWLSEnv.cmd” and then Compile the above two ejb stuffs.

Compiling_EJB3_App

Compiling_EJB3_Ap

Step7). Write the “build.xml” file to deploy your Application…inside “C:EJB3_Plan_XML_Demo” like following:

<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="EJB3App" source="EJB3App"
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="EJB3App" failonerror="false"
user="${wls.username}" password="${wls.password}" verbose="true"
adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" />
</target>

</project>

Step8). Now open a Command prompt and then run “setWLSEnv.cmd” then run the above ant “build.xml” file to deploy your application:

Deploying_EJB3_App_using_Ant

Deploying_EJB3_App_Using_Ant

Step9). Now Login to Admin Consoel to see how many initial beans have been created for our EJB3App.

Step10). Now Provide the “plan.xml” file inside “C:EJB3_Plan_XML_Demo” location as following to change the “initial-bean-in-free-pool” setting for our EJB3App…

<?xml version=’1.0' encoding=’UTF-8'?>
<deployment-plan xmlns="http://www.bea.com/ns/weblogic/deployment-plan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/deployment-plan http://www.bea.com/ns/weblogic/deployment-plan/1.0/deployment-plan.xsd" global-variables="false">
<application-name>EJB3App</application-name>

<variable-definition>
<variable>
<name>initialPoolVariable</name>
<value>15</value>
</variable>
</variable-definition>

<module-override>
<module-name>EJB3App</module-name>
<module-type>ejb</module-type>
<module-descriptor external="true">
<root-element>weblogic-ejb-jar</root-element>
<uri>META-INF/weblogic-ejb-jar.xml</uri>
<variable-assignment>
<name>initialPoolVariable</name>
<xpath>/weblogic-ejb-jar/weblogic-enterprise-bean/[ejb-name="CalculatorBean"]/stateless-session-descriptor/pool/initial-beans-in-free-pool</xpath>
</variable-assignment>
</module-descriptor>
</module-override>

<config-root>C:EJB3_Plan_XML_Demo</config-root>
</deployment-plan>

Step11). provide the “build_with_plan.xml” file inside “C:EJB3_Plan_XML_Demo” to redepoy the application with the settings provided as part of our “plan.xml” file. Like following :

Redeploying_EJB_Using_Pla

Redeploying_EJB_Using_Pla

Step12). Now again Login to AdminConsole and then see the Monitoring Tab of your EJB3App … like following:

AfterRedeploying_UsingPlan

AfterRedeploying_UsingPlan

.

.

Thanks

Jay SenSharma


Resource DataSource Injection in EJB3

Hi,

Jay SenSharma

Jay SenSharma

Here we are going to see how we can use the DataSources inside our EJB3.0 Application. Basically there are two Options to get the DataSource object inside the EJB3 Beans.

Option-1). Using Traditional Way we can get the dataSource Object by Looking up in the JNDI tree of the Server to get teh dataSource object.
Option-2). Using the resource Injection techniqueue we can get the DataSource object in two lines of codes…(it reduces the code length as well)
In the below demonstration we will use two fidderent methods to get the DataSource objects in above two ways using two different Business methods.

Step1). Please refer to the following Link to Create a DataSource through the Admin Console…http://middlewaremagic.com/weblogic/?p=2050

Step2). Now Create a Directory somewhere in your file system like “C:Resource_Injection” this directory is going to become our EAR Application.

Step3). Now Create another Directotry “MyEJB” inside “C:Resource_Injection”.

Step4). Write the EJB Interface “CalculatorRemote.java” inside “C:Resource_InjectionMyEJB” like following:

package calculator;
import javax.ejb.*;
import javax.sql.DataSource;
@Remote
public interface CalculatorRemote
{
public String getDataSourceUsingTraditionalWay(String query);
public String getDataSourceUsingResourceInjection(String query);
}

Step4a). Write the EJB Stateless Session Bean class “CalculatorBean.java” inside “C:Resource_InjectionMyEJB” like following:

package calculator;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import javax.annotation.*;

@Stateless(mappedName="calc")
public class CalculatorBean implements CalculatorRemote
{
@Resource(mappedName="TestDSJndi")
DataSource injectedDataSource;
public String getDataSourceUsingResourceInjection(String query)
{
System.out.println("inside betterWayToGetDataSourceUsingResourceInjection() method ");
System.out.println("betterWayToGetDataSourceUsingResourceInjection() got DS = "+injectedDataSource);
String result=this.executeSQL(query,injectedDataSource);
return result;
}

public String getDataSourceUsingTraditionalWay(String query)
{
DataSource traditionalDS=null;
try{
InitialContext ic=new InitialContext();
traditionalDS=(DataSource)ic.lookup("TestDSJndi");
System.out.println("nt Inside getDataSourceUsingTraditionalWay.....Got DataSource = "+traditionalDS);
}
catch(Exception e)
{
System.out.println("nt Unable to get DataSource using getDataSourceUsingTraditionalWay: "+e);
}
String result= this.executeSQL(query,traditionalDS);
return result;
}

public String executeSQL(String query,DataSource ds)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String resultString="";
try{
con=ds.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(query);
int tableCount=1;
System.out.println("nt Following Tables Are Available in Systables of Pointbase..");
while(rs.next())
{
String tableName=rs.getString(2);
System.out.println("t Table-"+tableCount+":t"+tableName);
tableCount++;
resultString=resultString+"<BR>"+tableName;
}
}
catch(Exception e)
{
System.out.println("nt Exception Occured: "+e);
}
finally
{
try { if(rs!=null) rs.close(); }
catch (Exception rse) {}
try { if(stmt!=null) stmt.close(); }
catch (Exception sse) {}
try { if(con!=null) con.close(); }
catch (Exception cse) {}
}
return "Result Of Query: "+query+" is Following: "+resultString;
}
}

Step5). Open a command prompt then run “setWLSEnv.cmd” here and then compile the EJBs like following:

Compiling EJB  Classes

Compiling EJB Classes

Step6). Now Create a Directory “MyWebApp” inside “C:Resource_Injection”

Step7). Now Create “WEB-INF” Directory and provide the following “web.xml” file inside “C:Resource_InjectionMyWebAppWEB-INF”

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


NOTE: Make Sure that you are using the Latest “web.xml” Schema because the Resource Injection and EJB Injection is supported from Servlet Specification 2.5…So make sure that u are not using any “web.xml” file which is using DTD declarations.

Step8). Provide the following “index.jsp” file inside “C:Resource_InjectionMyWebApp” directory.

<%@ page import="javax.sql.*,calculator.*, javax.naming.*" %>
<%
InitialContext ic=new InitialContext();
CalculatorRemote remote=(CalculatorRemote)ic.lookup("calc#calculator.CalculatorRemote");
String result=remote.getDataSourceUsingTraditionalWay("SELECT * FROM SYSTABLES");
System.out.println("nt Result TRADITIONAL WAY: "+result);
out.println("<h2>Result TRADITIONAL WAY</h2><BR> "+result);

System.out.println("<h2>Standard Way of Getting DataSource Using Resource Injection BELOW</h2>");
String resultInjected=remote.getDataSourceUsingResourceInjection("SELECT * FROM SYSTABLES");
System.out.println("nt Result RECOMMENDED INJECTED WAY: "+resultInjected);
out.println("<h2>Result RECOMMENDED INJECTED WAY</h2><BR> "+resultInjected);
%>

Step9). Create a “META-INF” directory inside “C:Resource_Injection” to provide the EAR deployment descriptors inside it. Create a file “application.xml” inside “C:Resource_InjectionMETA-INF” like following:

<?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">
<module>
<ejb>MyEJB</ejb>
</module>
<module>
<web>
<web-uri>MyWebApp</web-uri>
<context-root>MyWebApp</context-root>
</web>
</module>
</application>

Step10). Your Application is ready for deployment…Open Admin Console and then Deploy the “Resource_Injection” there….this is your EAR application.

Step11). Hit the Application like : http://localhost:7001/MyWebApp/index.jsp

.
.
Thanks
Jay SenSharma


EJB3 WebLogic10.3 and Generics Issue

Hi,

Jay SenSharma

Jay SenSharma

If we use any Generic implementation in EJB3.0 deployed on weblogic Server then we face some errors like following:

Example:
package test;
import java.util.List;
public interface TestGenericsInEJB3
{
public List callPlainMethod();
public List<User> callGenericMethod();
}

If our EJB3 Bean uses the above Interface as a remote Interface then we will find the following kind of exception while invoking it remotely.

java.lang.AssertionError: Can not find generic method public abstract java.util.List<test.
TestGenericsInEJB3> callGenericMethod() in EJB Object
at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.getTargetMethod(RemoteBusinessIntfProxy.java:159)
at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:53)
at $Proxy79.
callGenericMethod(Unknown Source)
at jsp_servlet.__index._jspService(__index.java:53)

There are two following possibilities for the above Exception:

Case-1: When the business interface extends java.rmi.Remote, and extends some generic methods from a super class, the deployment will fail.
Workaround: This case is a limitation in WLS 10.3 or greater and hence no workaround applicable.

Case-2: When the business interface doesn’t extend java.rmi.Remote, the invocation on the generic business methods will fail.
Workaround: It can be resolved by downloading the needed classes from the server side. If network downloading is disabled, however, the invocation will fail still.

If network downloading isn’t permitted in the user’s environment, it is recommended that you run appc first, then add the generated classes to the classpath of the client side. Please refer to the following link to see how we can generate the EJB Client Jar: http://middlewaremagic.com/weblogic/2010/04/02/generating-ejb3-clientjar/

.
.
Thanks
Jay SenSharma


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