EJB

WebLogic WorkManager with EJB3 And MaxThread Constraint

Hi,
Jay SenSharma

Jay SenSharma

WebLogic Provides us a feature to manage the WebLogic Threads per application using WorkManagers. In this demo we will see how we can instruct WebLogic to allocate only one thread to process the Clients request. We will develop a simple EJB3 application and we will assign a WorkManager with MaxThreadConstraint as 2. It means for Any number of Clients request to that EJB, the WebLogic will allocate only 1 Thread for request processing.
.
Step1). Please follow the instructions mentioned in the  link: http://middlewaremagic.com/weblogic/?p=2691 to create WebLogic WorkManager in seconds using WLST. Just make sure that you chose the following values for Max and Min Threads to see how this sample works. Later you can use your own values.
MaxThread=2
MinThread=2
.
Step2). Now create a Directory somewhere in your file system like:  “C:tempEJB3_With_WorkManager”
.
Step3). Now create a EJB remote Interface “CalculatorRemote.java” inside “C:tempEJB3_With_WorkManager” like following:
//CalculatorRemote.java
package calculator;
import javax.ejb.*;
@Remote
public interface CalculatorRemote
{
  public int add(int x,int y);
}
Step4). Now ssame way create a Stateless Session Bean “CalculatorBean.java” inside “C:tempEJB3_With_WorkManager” like following:
//CalculatorBean.java
package calculator;
import javax.ejb.*;
@Stateless
public class CalculatorBean implements CalculatorRemote
 {
   public int add(int x,int y)
    {
	  System.out.println("Current ThreadName Is: "+Thread.currentThread().getName());
      return (x+y);
    }
 }
Step5). Now create a directory with name “META-INF” inside “C:tempEJB3_With_WorkManager” and then create a file “weblogic-ejb-jar.xml”  inside “C:tempEJB3_With_WorkManagerMETA-INF”  directory like following so that we can assign the WebLogic WorkManager to the EJB using “dispatch-policy” tag:
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/10.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <weblogic-enterprise-bean>
    <ejb-name>CalculatorBean</ejb-name>
    <stateless-session-descriptor>
      <pool></pool>
      <stateless-clustering>
        <home-is-clusterable>false</home-is-clusterable>
        <stateless-bean-is-clusterable>false</stateless-bean-is-clusterable>
      </stateless-clustering>
    </stateless-session-descriptor>
    <transaction-descriptor></transaction-descriptor>
    <jndi-name>X</jndi-name>
    <dispatch-policy>JackWorkManager</dispatch-policy>
    <remote-client-timeout>0</remote-client-timeout>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>
Step6). Now run the “setWLSEnv.cmd” in a command prompt to set the classpath then Compile and EJBs and then make a Jar of it like following:
compiling_Ejb3

compiling_Ejb3

Step7). Now deploy the above created EJB3 jar file “Ejb3.jar” in WebLogic Server.
.
Step8). Check the AdminConsole where you are able to see the WorkManager properly assigned to your EJB3 or Not?
 EJB3_Workmanager_Console_look

EJB3_Workmanager_Console_look

Step9). Now Create a Client somewhere in your file system and set the Classpath to Ejb3.jar which we created above…. Client program like following “Ejb3Client.java”.  This client is going to send 5000 fresh requests to the WebLogic Server.
package calculator;
import javax.naming.*;
import java.util.*;
public class  Ejb3Client
{
	public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
	private static String serverUrl ="t3://localhost:7001";
	public static void main(String[] ar)  throws Exception
	{
		for(int i=0;i<=5000;i++)
		 {
           InitialContext ic=null;
           try{
		        Hashtable env = new Hashtable();
		        env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
		        env.put(Context.PROVIDER_URL, serverUrl);
  		        ic = new InitialContext(env);
              }
            catch(Exception e)
             {
			    System.out.println("nt Didnot get InitialContext: "+e);
			 }
           try
             {

                Object obj=ic.lookup("calc#calculator.CalculatorRemote");
                CalculatorRemote remote=(CalculatorRemote)obj;
                System.out.println("nntRemote=>"+remote.getClass());

				System.out.println("nntClassLoading=>1");
                Class.forName("calculator.CalculatorRemote");
				System.out.println("nntClassLoading=>2");

                System.out.println("nnt SUM = "+remote.add(Integer.parseInt(ar[0]),Integer.parseInt(ar[1])));
             }
           catch(Exception e)
             {
                System.out.println("nnt jack Exception => "+e);
                e.printStackTrace();
             }
           System.out.println("nt Iteration ------------ "+i);
	    }
	}
}

Step10). Run the client and then check the Server STDOUT that all the 5000 request will be processed only by a Two Execute Thread….It means due to MaxThreadConstraint=2 the WebLogic will not allocate more than two thread to process clients request.
EJB3_Workmanager_Client_Console_Output

EJB3_Workmanager_Client_Console_Output

In the output (STDOUT) of the Server you will see that there are only 2 Threads which are processing all the 5000 Clients request…..

Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'
Current ThreadName Is: [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'
.
.
Thanks
Jay SenSharma

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


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