WEBSERV
DeveloperIQ..APRIL-2004
R.S.RAMASWAMY ([email protected])
In DeveloperIQ ( March-2004) , we had seen parts 1 to 4 of this tutorial on exposing an EJB as XML-Webservice using Axis. This is a 7 part article.
part-1 : Overview
part-2 : deploying ejb in weblogic-7
part-3 : Drop-in-deployment in Axis
part-4 : deloyment using wsdd for javabean.
part-5 : using wsdd file for ejb-bean
itself)
a) java:RPC
b) java:EJB
part-6 : WAP client
a) for EJB
b) for EJB-WebService
part-7 : J2ME Client
a) for EJB
b) for EJB-WebService
We now proceed to
complete the remaining parts.
Part-5 ( using wsdd file for ejb-bean itself).
In part-4, we
created a javabean to accessthe EJB in weblogic enterprise server and usedit in
the wsdd file.This approach is preferable because, the best method is to use
a stateless-session bean to access other beans in the ejb-server.This is known
as 'Facade pattern'.
"It
is entirely possible to package your business logic into Java Beans and
not worryabout the EJB API.However,as your business logic becomes more
complex ,requiring the coupling of the logic with enterprise functionality like transactions,resource
management etc, the benefits of the EJB architecture rapidly become
apparent.The EJB container can provide all these system-level
services(lifecycle management,connection pooling,transations,etc)leaving the
developer to concentrate on developing thebusiness logic". Secondly, "Utilizing
existing applications is the key inan enterprise scenario. It is important
thatexisting business functionality in EJB components within the J2EE
Application can be invoked in a seamless fashion."
---Romin Irani
in "AXIS".
This argument
against using EJB, surfaces again and again from
different quarters often. For instance, Perl programmers who want to use XML-Webservice
also argue that simple functions will
do. Hence, it is worthwhile
to learn the
opinion of experts on SOAP on this question.
(quoted from: Programming
web service with SOAP
by James Snell,Doug Tidwell and Pavel Kulchenko)
UDDI::Lite modules for Perl.)
"Because a web service exposes an
application's functionality to any client in any programming language,they
raise interesting questions in both the programming and the business world.Programmers tend to raise questions like,
"how do we do two-phase commit transactions?" orHow do I do object inheritance?" or"
How do I make this damn thing run faster?"-questions typically associated
with going through the steps of writing code.
Business folks,on the other hand,tend to ask
questions like,"How do I ensure that the person using the service is
really who they say they are?" or "How can we tie together multiple
web services into a workflow?" or "How can I ensure the realiability
of web service transactions?" Their questions typically address business
concerns.
These tow perspectives go hand-in-hand
with one another. Every business issue
will have a software-based solution. But the two perspectives are also at odds
with each other:the business processes demand completeness, trust, security,
and reliability, which may be in incompatible with the programmers goals of
simplicity, performance, and robustness.
The outcome is that tools for implementing webservices
will do so from one of thesse two angles,but rarely will they do so from
both.For example ,SOAP::Lite,the perl based SOAP implementation written by the
coauthor of this book,Pavel kulchenko,is essentially written for programmers.It
provides a very simple set of tools for invoking Perl modules using
SOAP,XML-RPC,jabber,or any number of other protocols.
In contrast,Apache's Axis project(the next
generation of Apache's SOAP implemention)is a more complex web services
implementaion designed to make it easier to implement processes,or to tie
together multiple web services. Axis can perform the stripped down bare
essentials,but that is not its primary focus.
The important thing to keep in mind is that
both tools implement many of the same set of technologies(SOAP,WSDL,UDDI, and others,many of which we discuss
later on),and so they are capable of interoperating with each other.The
differences are in the way they interface with applications.This gives
programmers a choice of how their web services is implemented,without
restricting the users of that service."
Reverting back to our main theme now, some developers prefer to use the
Enterprise-bean itself
directly in the wsdd file as is about to be
shown below.
Here again, there are two possibilities. We can use either
a) java:RPC as pivot-handler
(or)
b) java:EJB as pivot-handler
There will be some
important differences
in the way in
which the wsdd file is created.
and these should
be carefully noted.
First, we will demonstrate the java:RPC pivot-handler method .
After that, we will create another wsdd to
use java:EJB pivot handler and test it .
This is our objective in this part of the tutorial..
( Kindly read this lesson, in
conjunction with
earlier parts, published last
month).
We have already deployed our stateless ejb
in Weblogic7.
(please refer to part-2 of
this lesson).
We are going to expose an ejb as webservice
mentioning ejb classes such as
sqlRemote,sqlHome,sqlBean
directly in wsdd file.
Our current working folder is C:\>sam.
with path and classpath as shown below.
path=c:\windows\command;
d:\jdk141\bin;
d:\bea\weblogic700\server\bin;
d:\bea\weblogic700\server\
lib\weblogic.jar;
c:\axis11\lib\axis.jar;
c:\axis11\lib\jaxrpc.jar;
c:\axis11\lib\saaj.jar;
c:\axis11\lib\commons-logging.jar;
c:\axis11\lib\commons-discovery.jar;
c:\axis11\lib\wsdl4j.jar;
d:\tomcat 4.1\common\lib\activation.jar;
d:\tomcat 4.1\common\lib\xerces.jar;
Now create sqlDeploy.wsdd file
in our
current working folder (ie)C:\sam
c:\sam> edit sqlDeploy.wsdd
<deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/
axis/wsdd/providers/java" >
<service name="sqlservice"
provider="java:RPC"
>
<parameter name="beanJndiName"
value="sqlJndi"
/>
<parameter name="homeInterfaceName"
value="sqlHome"
/>
<parameter name="remoteInterfaceName"
value="sqlRemote"
/>
<parameter name="className"
value="sqlBean"
/>
<parameter name="allowedMethods"
value="*"
/>
</service>
</deployment>
The next step is to deploy
the service in Axis.Remember, that
Tomcat4.1 and Weblogic7 , have already been
started.
( as given in part-2).Secondly , preliminary steps
for using Axis also have been already performed.(please refer to opening
remarks in part-3 of this tutorial in
March-2004 issue).
We now give the following command for deploying the service.
c:\>sam>java org.apache.axis.client.AdminClient
sqlDeploy.wsdd -lhttp://localhost:8080/axis/ services/AdminService
( this should be
typed in a continuous line).
After some time, if the wsdd
file is free from
syntax errors, the service will
be deployed
and we will get a message as
follows:
<Done Processing>
For verifying whether the
service is deployed,
we give the following command.
c:\>sam> java org.apache.axis.client.AdminClient
list -lhttp://localhost:8080/axis/ services/AdminService >
list.txt
After this, we can
see list.txt and check up
whether sqlservice
has been deployed there.
We can also test
the deployment by using the browser and typing the URL as:
http://localhost:8080/axis/services/sqlservice?wsdl
We will get a
wsdl file. Now we've exposed our ejb as webservice.
We test this service in two
ways
(ie)a) console client and
b) servlet client.
c:\sam> edit ejbaxisconsoleclient.java
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
{
{
try
{
String url = "http://localhost:8080/axis/services/sqlservice";
//Last parameter is service name in WSDD;
String method = "getdata";
System.out.println("method is: "+method);
( new QName("sqlservice",method));
System.out.println(s);
catch(Exception e1)
{
System.out.println(""+e1); }
}
}
c:\sam> javac ejbaxisconsoleclient.java
After it is complied
successfully, run the program and pass the query as commandline argument.
c:\sam> java ejbaxisconsoleclient "select * from table1"
We'll get the resultset as
per the query.
Next we are going to test
this service as a servlet.
c:>sam> edit ejbaxisservletclient.java
// ejbaxisservletclient.java
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
{
public void doPost (HttpServletRequest
request,
HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.getParameter("text1");
String url = "http://localhost:8080/axis/services/sqlservice";
String method = "getdata";
( new
QName("sqlservice",method));
("sql",XMLType.XSD_STRING,ParameterMode.IN);
System.out.println("Call
Ok");
new Object[] { sql };
out.println(s);
}
catch(Exception e) {
System.out.println(""+e); }
}
}
c:\sam> javac ejbaxisservletclient.java
( we will be able
to compile because classes required
for compiling the servlet are available in
weblogic.jar , to which
we have already
given classpath).
copy c:\sam\ejbaxisservletclient.class to
d:\tomcat 4.1\webapps\axis\web-inf\classes
Do not forget to edit web.xml in d:\tomcat 4.1\webapps\axis\web-inf
folder( this is very important in
tomcat4.1).
<servlet-name>
ejbaxisservletclient
</servlet-name>
<servlet-class>
ejbaxisservletclient
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
ejbaxisservletclient
</servlet-name>
<url-pattern>
\servlet\ejbaxisservletclient
</url-pattern>
</servlet-mapping>
c:\sam> edit ejbaxisservletclient.htm
ejbaxisservletclient.htm
<html>
<body>
<form method=post action="http://localhost:8080/axis/servlet/ejbaxisservletclient">
Query <input type=text
name="text1">
<input type=submit>
</form>
</body>
</html>
copy c:\sam\ejbaxisservletclient.htm to
d:\tomcat 4.1\webapps\axis
Some developers would prefer
to use java:EJB pivot-handler.Fortunately,
the changes required for that are just a few lines.First and foremost, we have
to create a suitable wsdd file. Let us
call it as 'directejbdeploy.wsdd'.
<deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
<service name="dataejbservice"
provider="java:EJB">
<parameter
name="beanJndiName"
value= "sqlJndi"
/>
<parameter
name="homeInterfaceName"
value= "sqlHome"
/>
<parameter
name="jndiContextClass"
value="weblogic.jndi.WLInitialContextFactory"
/>
<parameter
name="jndiURL"
value="t3://127.0.0.1:7001" />
<parameter
name="allowedMethods" value="*" />
</service>
</deployment>
If we carefully study the above file, we will
note that the value for jndiContextClass and jndiURL are just what we usually specify in the client file
for ejb program!So, nothing very special
there!
All the classes referred to ,
are already available and after deploying this wsdd
as usual , we are ready to use the service!--
So, let us deploy now:
( this should be typed in a continuous line). (note that it is exactly like what we did previously, except that the name of wsdd file is different).
After deploying, we
can check up by typing the URL in
browser as :
'http://localhost:8080/axis/services/dataejbservice?wsdl'
Please note that
'dataejbservice' is the name of
service that we have given in the wsdd file.As we have ensured
that both tomcat4.1 &weblogic7 are
running , without anything being changed, we
will get the corresponding wsdl. This shows
that our webservice is running
correctly.
We now create a
console-mode program in
c:\sam , as usual.
// c:\sam\ directejbconsoleclient.java
//===============================
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
{
{
try
{
String url = "http://localhost:8080/axis/services/dataejbservice";
//Last parameter
is service name in WSDD; not class name
System.out.println("endpoint is :"+url);
String method = "getdata";
System.out.println("name of method
is: "+method);
call.setTargetEndpointAddress ( url);
call.setOperationName
( new
QName("dataejbservice",method));
System.out.println("Call
Ok");
System.out.println(s);
catch(Exception e1) {
System.out.println(""+e1); }
}
}
After compiling , we run the
program as usual:
c:\sam>java
directejbconsoleclient "select * from table1 "
We will get the resultset , as before.( tested and found to be ok.)
That completes part-5 of our tutorial.
So far we've accessed our ejb
through console mode and servlet.Now Its time to write the client program for
wireless browsers.
This, we take up in part-6