Monday 27 April 2009

3-SIMPLE AND COMPLETE DATA SOURCE EXAMPLE

Finally my program which accesses the MySQL database using the above context.xml ‘s datasource definition follows:

package dsweb;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DStest extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head><title>Data Source test</title></head><body>");
out.println("<h2>Data Source test by ARS</h2>");

try{
getData(out);
} catch(Exception e){out.println("problem "+ "<BR>"); e.printStackTrace();};

out.println("finitooooooooooo"+ "<BR>");
out.println("</body></html>");
} //end doGet



public void getData(java.io.PrintWriter out) throws Exception {
out.println("getData "+ "<BR>");
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
out.println("datasource is read " + "<BR>");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
conn = ds.getConnection();
out.println("connection is created "+ "<BR>");
stmt = conn.createStatement();
String textQuery = "select * from users";
rs = stmt.executeQuery(textQuery);

while (rs.next()) {
System.out.println(rs.getString("FIRST_NAME") + " " + rs.getString("LAST_NAME") + " "
+ rs.getString("AGE"));
out.println(rs.getString("FIRST_NAME") + " " + rs.getString("LAST_NAME") + " "
+ rs.getString("AGE")+ "<BR>");
}
stmt.close();
stmt = null;

conn.close();
conn = null;
} finally {
/*
* close any jdbc instances here that weren't
* explicitly closed during normal code path, so
* that we don't 'leak' resources...
*/

if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlex) {
// ignore -- as we can't do anything about it here
}

stmt = null;
}

if (conn != null) {
try {
conn.close();
} catch (SQLException sqlex) {
// ignore -- as we can't do anything about it here
}

conn = null;
}
}
}
}

Please be informed that you need to have DB named “testars” and a table on this DB named “users”.
I obtained the definition of the “users” with TOAD:

CREATE TABLE `users` (
`USER_ID` varchar(30) DEFAULT NULL,
`FIRST_NAME` varchar(30) DEFAULT NULL,
`LAST_NAME` varchar(30) DEFAULT NULL,
`AGE` int(11) DEFAULT NULL,
`EMAIL` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The DATA is:
USER_ID FIRST_NAME LAST_NAME AGE EMAIL
userid1 first1 last1 21 first1@some.com
userid2 first2 last2 21 first2@some.com
userid3 first3 last3 21 first3@some.com
userid4 first4 last4 21 first4@some.com
userid5 first5 last5 21 first5@some.com
6 Ali SARAL 40 ali@ya.com
userid7 Deniz Gezici 3 first6@some.com
admin admin
adnim 0



After the program ran, the output was:

Data Source test by ARS
getData datasource is read connection is created

first1 last1 21
first2 last2 21
first3 last3 21
first4 last4 21
first5 last5 21
Ali SARAL 40
Deniz Gezici 3
admin adnim 0
finitooooooooooo

( prev )

2-SIMPLE AND COMPLETE DATA SOURCE EXAMPLE

After you make sure that you can run a Servlet, you create the real servlet which does the DB access.
But in order to access the MySQL DB I had to create the datasource definition in the context.xml file.
The context.xml file may be placed in the WEBCONTENT file(whatever its name) at the same level with WEB-INF.

FILE: context.xml

<!-- The contents of this file will be loaded for each web application -->
<Context >
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="3391309"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testars?autoReconnect=true"
maxActive="8"
maxIdle="4" />
</Context>

After my program worked I also tried the below context.xml with pooling options. (It looked as if
it worked OK. I did not have the means to measure it and also my data was very small).

<!-- The contents of this file will be loaded for each web application -->
<Context >
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="3391309"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testars?autoReconnect=true"
maxActive="8"
maxIdle="4" />
<ResourceParams name="jdbc/MySQLDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
</Context>

Apache TOMCAT’s “JNDI Datasource HOW-TO”
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html gives more information on this matter, using the server.xml file of TOMCAT and using
The related JARs etc.

( prev ) ( next )


1-SIMPLE AND COMPLETE DATA SOURCE EXAMPLE

This is an implementation of the logic given on the MySQL site. The difference is: My solution is simple and complete. A step by step explanation follows:

I used Jdev 11g, and a small DB table on MySQL, I deployed my application to Tomcat due to some problems with WebLOGIC.

The first step is to test whether you can run servlets directly clicking on them and selecting the run command:
For this, you have to set the web.xml first.

FILE: web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" uri="http://java.sun.com/xml/ns/j2ee" >

<display-name>
Test ARS data source
</display-name>

<servlet>
<servlet-name>RequestDisplay</servlet-name>
<servlet-class>dsweb.RequestDisplay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestDisplay</servlet-name>
<url-pattern>/RequestDisplay</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DStest</servlet-name>
<servlet-class>dsweb.DStest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DStest</servlet-name>
<url-pattern>/DStest</url-pattern>
</servlet-mapping>

<resource-ref>
<res-ref-name>jdbc/MySQLDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<welcome-file-list>
<welcome-file>Welcome.html</welcome-file>
</welcome-file-list>
</web-app>

Here, I use RequestDisplay as a test servlet to check whether I can run a servlet.

FILE: RequestDisplay.java
package dsweb;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDisplay extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {

response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head><title>Request Attribute</title></head><body>");
out.println("<h2>Request attribute values</h2>");

out.println("outttttttttttttt");
out.println("</body></html>");


} //end doGet

}



( next )

Tuesday 21 April 2009

10 - Writing JAVA Clients for Calling BEA WebLogic EJBs


X- Deployment, Client adaptation and output

I have used a calculator stateless EJB of Rose India’s Deepak Kumar’s webClient to produce my JAVA Client and WebLOGIC EJBs. This is also an example of the conversion of a WEB Client EJB application to a JAVA Client EJB application. One must change the EJB definitions in order to convert them from JBOSS to WebLOGIC based on my few experiences on this matter. Please, note that I have removed transaction related entries from Kumar’s WebClient ejb.jar

What’s the result? It follows:
C:\EJBstateless>ant
Buildfile: build.xml

clean:
[delete] Deleting directory C:\EJBstateless\build
[mkdir] Created dir: C:\EJBstateless\build

copy-resources:
[copy] Copying 3 files to C:\EJBstateless\build
[copy] Copied 2 empty directories to 1 empty directory under C:\EJBstateles
s\build

compile:
[javac] Compiling 9 source files to C:\EJBstateless\build

deploy:
[jar] Building jar: C:\EJBstateless\deploy\java2s.jar

BUILD SUCCESSFUL
Total time: 15 seconds
---------------------------------------------------------------
wls:/DefaultDomain/serverConfig> deploy("java2s","C:/EJBstateless/deploy/java2s.
jar" ,"DefaultServer",securityModel="Advanced", block="true")
Deploying application from C:\EJBstateless\deploy\java2s.jar to targets DefaultS
erver (upload=false) ...
<20.nis.2009>
.Completed the deployment of Application with status completed
Current Status of your Deployment:
Deployment command type: deploy
Deployment State : completed
Deployment Message : no message
wls:/DefaultDomain/serverConfig>
-----------------------------------------------------------------
C:\EJBstateless>ant run
Buildfile: build.xml
copy-resources:
compile:
run:
[java] JVM args ignored when same JVM is used.
[java] ARSSSsssssssss try getting ctx
[java] 7000
[java] 7000
[java] Got Context1: WLContext ()
[java] Multiply 3 and 2
[java] Multiply result= 6
BUILD SUCCESSFUL
Total time: 9 seconds
C:\EJBstateless>


( prev )

9-Writing JAVA Clients for Calling BEA WebLogic EJBs


IX- The naming solution that works for WebLOGIC

File : CalcARSBean.java
package bean;

/**
* Remote interface for CalculatorBean.
*/
public interface CalcARSBean
extends javax.ejb.EJBObject
{
/**
* The method that returns the multiplied value
*/
public int multiply( int val1,int val2 )
throws java.rmi.RemoteException;

}
------------------------------------------------------------------
File : CalcARSBeanHome
package bean;

/**
* Home interface for CalculatorBean.
*/
public interface CalcARSBeanHome
extends javax.ejb.EJBHome
{
public static final String COMP_NAME="java:comp/env/ejb/CalculatorBean";
public static final String JNDI_NAME="CalculatorSessionBean";

public bean.CalcARSBean create()
throws javax.ejb.CreateException,java.rmi.RemoteException;

}

------------------------------------------------------------------
File : CalcARSBeanLocal
package bean;

/**
* Local interface for CalculatorBean.
*/
public interface CalcARSBeanLocal
extends javax.ejb.EJBLocalObject
{

}
------------------------------------------------------------------
File : CalcARSBeanLocalHome
package bean;

/*
* Local home interface for CalculatorBean.
*/
public interface CalcARSBeanLocalHome
extends javax.ejb.EJBLocalHome
{
public static final String COMP_NAME="java:comp/env/ejb/CalculatorBeanLocal";
public static final String JNDI_NAME="CalculatorBeanLocal";

public bean.CalcARSBeanLocal create()
throws javax.ejb.CreateException;

}
------------------------------------------------------------------
File : CalcARSSessionBean
package bean;


/*
* SessionBean.java
*
*/

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email deepak@roseindia.net
*/

/**
* This is the Test Session Bean
*
* @ejb:bean type="Stateless"
* name="CalculatorBean"
* jndi-name="CalculatorSessionBean"
* display-name="EJB Calculator Session Bean"
*
* @ejb:env-entry name="CalculatorBean" type="java.lang.String" value="calculatorbean"
*/

public class CalcARSSessionBean implements SessionBean{

public void ejbCreate() throws CreateException {
System.out.println("ejbCreate remote");
}

public void setSessionContext( SessionContext aContext ) throws EJBException {

}



public void ejbActivate() throws EJBException {

}

public void ejbPassivate() throws EJBException {

}

public void ejbRemove() throws EJBException {

}

/**
* The method that returns the multiplied value
*
* @ejb:interface-method view-type="remote"
*/
public int multiply(int val1, int val2){
System.out.println("I am from multiply");
System.out.println("I am from multiply : " + val1 + " * " + val2);
return val1*val2;
}

}
------------------------------------------------------------------
File : Main.java
import bean.CountLocal;

import javax.naming.InitialContext;

import bean.CountRemote;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

public class Main {

public static void main(String[] a) throws Exception {
boolean debug = true;
String name = "java2s";

Context ctx1 = null;
System.out.println("ARSSSsssssssss try getting ctx");
try {
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
environment.setProviderUrl("t3://localhost:7101");
environment.setSecurityPrincipal("weblogic");
environment.setSecurityCredentials("weblogic");
environment.setRMIClientTimeout(7000L);
environment.setRequestTimeout(7000L);
ctx1 = environment.getInitialContext();
environment.setCreateIntermediateContexts(true);
System.out.println(environment.getRMIClientTimeout());
System.out.println(environment.getRequestTimeout());
System.out.println("Got Context1: " + ctx1);
}
catch ( NamingException ne ) {
ne.printStackTrace();
}

System.out.println("ARSSSsssssssss beg");
CountRemote service = (CountRemote) ctx1.lookup("ejb.CountBean/remote");

int countVal = 9;

service.set(countVal);
countVal = service.count();
System.out.println(countVal);
System.out.println("Calling count() on beans...");

countVal = service.count();
System.out.println(countVal);

service.remove();

}

}
----------------------------------------------------------------------------------



( prev ) ( next )

8-Writing JAVA Clients for Calling BEA WebLogic EJBs

VIII- java2s JBOSS ejb still does not work and the solution

File : ejb.jar in the src/META-INF lib

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD

Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar >

<description>EJB Examples</description>
<display-name>EJB Examples</display-name>

<enterprise-beans>

<!-- Session Beans -->
<session >
<description>EJB Calc ARS Session Bean</description>
<display-name>EJB Calc ARS Session Bean</display-name>

<ejb-name>CalcARSBean</ejb-name>

<home>bean.CalcARSBeanHome</home>
<remote>bean.CalcARSBean</remote>
<local-home>bean.CalcARSBeanLocalHome</local-home>
<local>bean.CalcARSBeanLocal</local>
<ejb-class>bean.CalcARSSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

<env-entry>
<env-entry-name>CalcARSBean</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Calc ARS bean</env-entry-value>
</env-entry>

</session>

</enterprise-beans>

<!-- Relationships -->

<!-- Assembly Descriptor -->
<assembly-descriptor >

<method-permission >
<description>Session Bean</description>
<unchecked/>
<method >
<description>Session Bean</description>
<ejb-name>CalcARSBean</ejb-name>
<method-name>*</method-name>
</method>
</method-permission>
</assembly-descriptor >
</ejb-jar>

File : weblogic-ejb-jar in the src/META-INF lib
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.

//DTD WebLogic 6.0.0 EJB//EN" "http://www.bea.com/servers

/wls600/dtd/weblogic-ejb-jar.dtd">

<weblogic-ejb-jar>
<description>java2s Bean Example</description>
<weblogic-enterprise-bean>
<ejb-name>CalcARSBean</ejb-name>
<stateless-session-descriptor>
</stateless-session-descriptor>
<reference-descriptor>
</reference-descriptor>
<jndi-name>CalcARSSessionBean</jndi-name>
<local-jndi-name>CalcARSBeanLocal</local-jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

( prev ) ( next )

7-Writing JAVA Clients for Calling BEA WebLogic EJBs


VII-ejb-jar.xml and other xml file issue for identifying EJBs

After this point I began to think about the way I defined my EJBs. I certainly, deployed them but they did not work so there must be something related to the way I defined them. So, I began to chang ejb-jar.xml, and tried to get error messages by injecting intentional mistakes. I got the below messages:

VALIDATION PROBLEMS WERE FOUND
problem: cvc-complex-type.2.4a: Expected elements
'remote@http://java.sun.com/xml/ns/javaee
local-home@http://java.sun.com/xml/ns/javaee
local@http://java.sun.com/xml/ns/javaee
business-local@http://java.sun.com/xml/ns/javaee
business-remote@http://java.sun.com/xml/ns/javaee
service-endpoint@http://java.sun.com/xml/ns/javaee
ejb-class@http://java.sun.com/xml/ns/javaee
session-type@http://java.sun.com/xml/ns/javaee
timeout-method@http://java.sun.com/xml/ns/javaee
init-method
@http://java.sun.com/xml/ns/javaee
remove-method@http://java.sun.com/xml/ns/javaee
transaction-type@http://java.sun.com/xml/ns/javaee
around-invoke@http://java.sun.com/xml/ns/javaee
env-entry@http://java.sun.com/xml/ns/javaee
ejb-ref@http://java.sun.com/xml/ns/javaee
ejb-local-ref@http://java.sun.com/xml/ns/javaee
serv
ice-ref@http://java.sun.com/xml/ns/javaee
resource-ref@http://java.sun.com/xml/ns/javaee
resource-env-ref@http://java.sun.com/xml/ns/javaee
message-destination-
ref@http://java.sun.com/xml/ns/javaee
persistence-context-ref@http://java.sun.com/xml/ns/javaee
persistence-unit-ref@http://java.sun.com/xml/ns/javaee
post-cons
truct@http://java.sun.com/xml/ns/javaee
pre-destroy@http://java.sun.com/xml/ns/javaee
post-activate@http://java.sun.com/xml/ns/javaee
pre-passivate@http://java.
sun.com/xml/ns/javaee
security-role-ref@http://java.sun.com/xml/ns/javaee
securi
ty-identity@http://java.sun.com/xml/ns/javaee'
instead of 'bogus@http://java.sun
.com/xml/ns/javaee' here in element session@http://java.sun.com/xml/ns/javaee:.
I got deployment errors when I played with the ejb-jar.xml…
wls:/DefaultDomain/serverConfig> deploy("java2s","C:/EJBstateless/deploy/java2s.
jar" ,"DefaultServer",securityModel="Advanced", block="true")
Deploying application from C:\EJBstateless\deploy\java2s.jar to targets DefaultS
erver (upload=false) ...
<20.nis.2009>
......Failed to deploy the application with status failed
Current Status of your Deployment:
Deployment command type: deploy
Deployment State : failed
Deployment Message : weblogic.application.ModuleException: Exception prepari
ng module: EJBModule(java2s)


Unable to deploy EJB: java2s.jar from java2s.jar:

There are 6 nested errors:

weblogic.ejb.container.compliance.ComplianceException: In EJB CountBean, the hom
e interface of a stateless session bean must have one create method that takes n
o arguments.
and
weblogic.ejb.container.compliance.ComplianceException: Home methods are not allo
wed on session beans: CountBean.count()
and
weblogic.ejb.container.compliance.ComplianceException: In EJB CountBean, the hom
e interface bean.CountRemote must extend the javax.ejb.EJBHome interface.
and
weblogic.ejb.container.compliance.ComplianceException: In EJB CountBean, method
count() on the home interface does not throw java.rmi.RemoteException. This is a
required exception.
at weblogic.ejb.container.compliance.HomeInterfaceChecker.checkMethodsTh
and
weblogic.ejb.container.compliance.ComplianceException: In EJB CountBean, method
remove() on the home interface does not throw java.rmi.RemoteException. This is
a required exception.
and
weblogic.ejb.container.compliance.ComplianceException: In EJB CountBean, method
set(int) on the home interface does not throw java.rmi.RemoteException. This is
a required exception.

Traceback (innermost last):
File "", line 1, in ?
File "", line 234, in deploy
WLSTException: Error occured while performing deploy : Target exception thrown w
hile deploying application: Error occured while performing deploy : Deployment F
ailed.Error occured while performing deploy : Deployment Failed. Use dumpStack()
to view the full stacktrace
wls:/DefaultDomain/serverConfig>

The message was clear. To convert JBOSS EJBs to WebLOGIC you must change their definitions.
Here follows the outputs for my JAVA Client program calling the WebLOGIC EJBs.


(
prev ) ( next )

6-Writing JAVA Clients for Calling BEA WebLogic EJBs


VI- How to check the health of deployment-listing EJBs

I used a very usefull tool to find out whether my EJB is deployed. It looked OK. The problem was I did not know how the output should look. For that purpose I included below the output for a correctly created and deployed EJB.
Find WebLogic MBeans with Ease
http://monduke.com/2007/01/14/find-weblogic-mbeans-with-ease/

“By the way, if you’ve never used weblogic.Admin before, you must first run the setEnv (or setDomainEnv) script in your domain directory to set up environment variables and paths.
Now, that output is all well and good but it might not make finding things any easier. Time for a little redirection, my friend:
java weblogic.Admin -username weblogic -password weblogic -url t3://localhost:7101 query -pretty -pattern *:* > MBeans.txt”
MBeanName: "DefaultDomain:Name=java2s,Location=DefaultServer,Type=ApplicationConfig"
AltDescriptorPath:
AltWLSDescriptorPath:
CachingDisabled: false
Components: java2s
ConnectorComponents:
DeploymentTimeout: 3600000
DeploymentType: COMPONENT
EJBComponents: java2s
FullPath: C:\EJBstateless\deploy\java2s.jar
InternalApp: false
LoadOrder: 100
Name: java2s
Notes:
ObjectName: java2s
Parent: DefaultDomain
Path: C:\EJBstateless\deploy\java2s.jar
Registered: true
StagedTargets:
StagingMode: stage
StagingPath:
TwoPhase: true
Type: Application
WebServiceComponents:
-----------------------------------------------------
MBeanName: "DefaultDomain:ServerRuntime=DefaultServer,Name=java2s,ApplicationRuntime=java2s,Location=DefaultServer,Type=EJBComponentRuntime"
CachingDisabled: true
DeploymentState: 2
EJBComponent: java2s
EJBRuntimes: CalcARSBean
KodoPersistenceUnitRuntimes:
ModuleId: java2s.jar
Name: java2s
Parent: java2s
Registered: true
Type: EJBComponentRuntime
---------------------------
MBeanName: "DefaultDomain:ServerRuntime=DefaultServer,Name=java2s,Location=DefaultServer,Type=ApplicationRuntime"
ApplicationName: java2s
ApplicationVersion:
CachingDisabled: true
ComponentRuntimes: java2s
HealthState: Component:null,State:HEALTH_OK,MBean:null,ReasonCode:[]
KodoPersistenceUnitRuntimes:
LibraryRuntimes:
Name: java2s
OptionalPackageRuntimes:
Parent: DefaultServer
QueryCacheRuntimes:
Registered: true
Type: ApplicationRuntime
WseeRuntimes:
------------------------------------------------------------
MBeanName: "DefaultDomain:Name=java2s,Location=DefaultServer,Type=EJBComponentConfig,ApplicationConfig=java2s"
ActivatedTargets:
Application: java2s
CachingDisabled: false
DeploymentOrder: 1000
EJBComponentRuntime:
ExtraEjbcOptions:
ExtraRmicOptions:
ForceGeneration: false
JavaCompiler:
JavaCompilerPostClassPath:
JavaCompilerPreClassPath:
KeepGenerated: true
Name: java2s
Notes:
ObjectName: java2s
Parent: java2s
Registered: true
Targets: DefaultServer
TmpPath: tmp_ejb
Type: EJBComponent
URI: java2s.jar
VerboseEJBDeploymentEnabled: false
-----------------------------------------------------------------
MBeanName: "DefaultDomain:ServerRuntime=DefaultServer,Name=CalcARSBean,ApplicationRuntime=java2s,Location=DefaultServer,Type=EJBTransactionRuntime,EJBComponentRuntime=java2s,StatelessEJBRuntime=CalcARSBean"
CachingDisabled: true
Name: CalcARSBean
Parent: CalcARSBean
Registered: true
TransactionsCommittedTotalCount: 0
TransactionsRolledBackTotalCount: 0
TransactionsTimedOutTotalCount: 0
Type: EJBTransactionRuntime
------------------------------------------
MBeanName: "DefaultDomain:ServerRuntime=DefaultServer,Name=CalcARSBean,ApplicationRuntime=java2s,Location=DefaultServer,Type=EJBPoolRuntime,EJBComponentRuntime=java2s,StatelessEJBRuntime=CalcARSBean"
AccessTotalCount: 0
BeansInUseCount: 0
BeansInUseCurrentCount: 0
CachingDisabled: true
DestroyedTotalCount: 0
IdleBeansCount: 0
MissTotalCount: 0
Name: CalcARSBean
Parent: CalcARSBean
PooledBeansCurrentCount: 0
Registered: true
TimeoutTotalCount: 0
Type: EJBPoolRuntime
WaiterCurrentCount: 0
WaiterTotalCount: 0

My problem was still the below error message after I ran the JAVA client which called deployed EJB functions:

[java] javax.naming.NameNotFoundException: While trying to lookup 'ejb.Coun
tBean.remote' didn't find subcontext 'CountBean'. Resolved 'ejb' [Root exception
is javax.naming.NameNotFoundException: While trying to lookup 'ejb.CountBean.re
mote' didn't find subcontext 'CountBean'. Resolved 'ejb']; remaining name 'Count
Bean/remote'

( prev ) ( next )

5-Writing JAVA Clients for Calling BEA WebLogic EJBs

V- How to deploy and test

Unfortunately, my WebLogic CONSOLE does not and has not ever worked. So, I use WLST console.
1st step: Start Jdev
2. step: Start WebLogic server from the menu.
3. step:Start WLST from main computer Programs menu, Weblogic Server, Tools, WebLogic Scripting Tool
4. step Connect connect('weblogic','weblogic','t3://localhost:7101')
5.th step undeploy(‘java2s’) if necessary
6.th step deploy("java2s","C:/EJBstateless/deploy/java2s.jar","DefaultServer",securityModel="Advanced", block="true")
Here, C:/EJBstateless… is the directory where I have put the generated EJB jar. DefaultServer is the server I use.
7th step: Return back and run the client with the
ant run
command.

IMPORTANT NOTE: Converting JBOSS EJBs to WebLogic has a terrible pitfall. You generate the jar file than deploy it but when it comes to testing the client does not do anything. The worst of it there is not any warning etc message. It seems all is well, deployment is successfull but there is not anything wrong.
Some things and tools I used for debugging:

The use of other deployment options such as:
deploy("java2s","C:/EJBstateless/deploy/java2s.jar" ,"DefaultServer", 'nostage', libraryModule="true", subModuleTargets='CountBean@DefaultServer', securityModel="Advanced", securityValidationEnabled="false", block="true")


Check whether the EJB exists: Which looks OK.
listApplications()

tried to use NodeManager with no success, I have to fix it:
nmConnect('weblogic','weblogic','localhost','7101','DefaultDomain', 'DefaultDomain', 'C:/oracle/Middleware/user_projects/applications/DefaultDomain','plain')

tried to start and stop the application: Looked OK.
startApplication('java2s')
stopApplication('java2s')

tried to change the EJB definitions such as:
You should : 1 - Declare the JNDI name on the ejb annotation : @Stateless(name = "TestBean", mappedName = "TestBean") public class TestBean implements Test { ...

I read that WebLOGIC EJB2s must be serialized in order to work. I tried that one with no success either for the EJBs that I was trying to convert from JBOSS.


It always came down to this error:

[java] javax.naming.NameNotFoundException: While trying to lookup 'ejb.Coun
tBean.remote' didn't find subcontext 'CountBean'. Resolved 'ejb' [Root exception
is javax.naming.NameNotFoundException: While trying to lookup 'ejb.CountBean.re
mote' didn't find subcontext 'CountBean'. Resolved 'ejb']; remaining name 'Count
Bean/remote'

(
prev ) ( next )

4-Writing JAVA Clients for Calling BEA WebLogic EJBs

IV- wlfullclient.jar issue and how to find wlfullclient.jar

Oracle® WebLogic Server
Programming Stand-alone Clients
10g Release 3 (10.1.3)

“For WebLogic Server 10.0 and later releases, client applications need to use the wlfullclient.jar file instead of the weblogic.jar. Note that not all functionality available with weblogic.jar is available with the wlfullclient.jar.”

You can not find wlfullclient.jar on the internet. You have to create one with a jar tool. For that:

Programming Stand-alone Clients
Using the WebLogic JarBuilder Tool
http://edocs.bea.com/wls/docs103/client/jarbuilder.html
Creating a wlfullclient.jar for JDK 1.6 client applications
Use the following steps to create a wlfullclient.jar file for a JDK 1.6 client application:
Change directories to the server/lib directory.
cd WL_HOME/server/lib
Use the following command to create wlfullclient.jar in the server/lib directory:
java -jar wljarbuilder.jar
You can now copy and bundle the wlfullclient.jar with client applications.
Add the wlfullclient.jar to the client application’s classpath.

Creating a wlfullclient5.jar for JDK 1.5 client applications
Change directories to the server/lib directory.
cd WL_HOME/server/lib
Use the following command to create wlfullclient.jar in the server/lib directory:
java -jar wljarbuilder.jar -profile wlfullclient5
You can now copy and bundle the wlfullclient5.jar with client applications
Add the wlfullclient5.jar to the client application’s classpath.

IMPORTANT NOTE: Adding only wlfullclient.jar helps to stop interceptor related messages that come from the JBOSS EJBs. My Weblogic.jar(length=34992886bytes) that comes with WebLogic server’s lib misses weblogic/security/acl/UserInfo
while the one I found on the internet(length=37363256bytes) has them. The use of javax.interceptor.interceptors in the JBOSS example forced me to keep the jboss-ejb3x.jar, jbossall-client.jar and ejb3-persistence.jar. I lost a lot of time, before I found out that I must use wlfullclient.java. It solved all the jar problems.

(The below error gets corected by the use of weblogic or wlclient.jar:
run:
[java] JVM args ignored when same JVM is used.
[java] java.lang.NoClassDefFoundError: weblogic/security/subject/AbstractSubject )

One issue about wlfullclient was, my Jdeveloper11g used JDK1.6 while ant used JDK1.5. So, I chose to use the 1.5 wlfullclient. Nevertheless, if you want to control the JDK you can set it in the ant build file:




I had some problems while I was using wlfullclient.jar. Some libs did not exist or stg like that. But I can not remember exactly. My solution uses wlfullclient5.jar.


( prev ) ( next )

3-Writing JAVA Clients for Calling BEA WebLogic EJBs

III- Different ways to get context

try {
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
environment.setProviderUrl("t3://localhost:7101");
environment.setSecurityPrincipal("weblogic");
environment.setSecurityCredentials("weblogic");
environment.setRMIClientTimeout(7000L);
environment.setRequestTimeout(7000L);
ctx1 = environment.getInitialContext();
System.out.println("Got Context1: " + ctx1);
}
catch ( NamingException ne ) {
ne.printStackTrace();
}
This prints WLContext () at the System.out.
OR:
try {

Properties env = new Properties();
env.put("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
env.put("java.naming.factory.host", "127.0.0.1");
env.put("java.naming.factory.port", "7101"); env.put("java.naming.factory.url.pkgs","weblogic.jndi.factories:weblogic.corba.j2ee.naming.url:weblogic.corba.client.naming");
env.put("java.naming.security.principal", "weblogic");
env.put("java.naming.security.credentials", "weblogic");

ctx = new InitialContext(env);
System.out.println("Got Context: " + ctx);
}
catch ( NamingException ne ) {
ne.printStackTrace();
}

OR without security: Hashtable env = new Hashtable(5);

env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL,"t3://weblogicServer:7101");

Context ctx = new InitialContext(env);

IMPORTANT NOTE: The PORT number may be 7001 instead of 7101 depending on the installation.


( prev ) ( next )

2-Writing JAVA Clients for Calling BEA WebLogic EJBs

I- JNDI Conversion from JBOS to WebLOGIC issue

JBOSS - File: jndi.propertiesjava.naming.factory.initial=org.jnp.interfaces.NamingContextFactoryjava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfacesjava.naming.provider.url=localhost:1099


WebLOGIC - File: jndi.properties

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.factory.url.pkgs=weblogic.jndi.factories:weblogic.corba.j2ee.naming.url:weblogic.corba.client.naming
java.naming.provider.url=t3://localhost:7101
java.naming.security.principal=weblogic
java.naming.security.credentials=weblogic

II- WLInitialContextFactory issue
Important note: WebLOGIC ‘s July 2008 book
Oracle® WebLogic Server
Programming Stand-alone Clients
10g Release 3 (10.1.3) :
“In obtaining an initial context,
you must use weblogic.jndi.WLInitialContextFactory
when defining your JNDI context factory.
Use this class when setting the value for the "Context.INITIAL_CONTEXT_FACTORY" property that you supply as a parameter to new InitialContext().”

( prev ) ( next )




1-Writing JAVA Clients for Calling BEA WebLogic EJBs

This series of articles are about:

1- Writing a JAVA client for calling EJB’s located on WebLogic servers
2- Converting EJBs and JAVA Clients residing from JBOSS to WebLogic
3- Converting WEB applications calling WebLogic EJBs to JAVA clients(calling WebLogic EJBs)

Recently I tried successfully RoseIndia’s Introduction To Enterprise Java Bean(EJB) Tutorial for WebLogic
(
http://www.roseindia.net/javabeans/javabeans.shtml). I use Jdeveloper11g and WebLogic(10.3) witk JDK1.6, but when I run batch JAVA it is JDK1.5 which comes from ny JDeveloper10g. I could not deploy the sample ‘sessionbeans’ from the Jdev11g but I used the build with ant and created the example.jar successfully. My weblogic console does not work(even at the moment I have installed it), so I used WLST to deploy the three EJBs of the sessionbeans example of RoseINDIA. After deployment I could launch my webClient which sucessfully called the three EJBs as required.

Then, I began to work on the “Stateless Session Bean With Three Methods” of java2s.com at (
http://www.java2s.com/Code/Java/EJB3/StatelessSessionBeanWithThreeMethods.htm). I tried dearly to have it work but no way, it did not.

This series is a result of my 3-4 days work at the end of which I managed to hava a JAVA client work calling an EJB located on a WebLOGIC server. I will be carefull about giving every tiny detail, and the compact results of my researches in regards to various aspects of the issue in a step by step fashion… I will provide also the outputs for various cases related to the problem so that others will not lose time unproductively. The plateora of information related to different versions of WebLogic, existance of various Application Servers, various products coming from various Companies which are all a result of open systems philosophy causes a high level of complexity which can not be justified in any terms. After all JMS etc were simple to use facilities in mainframe systems such as CICS a couple of ten years ago…


Please feel free to contact arsaral [ at ] yahoo.com for further assistance.

Kind regards.

Ali R+ SARAL

( next )