JBoss-Net Hello World Example

JBoss-Net Hello World Example
This builds a simple example showing what can be done
with JBoss-Net which integrates Apache Axis.
Updated 29 August 2004

This example works with the newest builds of JBoss.NET that use Apache axis 1_1 for its core. Therefore you may need to change the code to work with the correct API, if you have an earlier version. In any case you need to download the Axis version so that you can code to the correct API, and have the Axis docs for details about the utilities, which come with Axis.
 
This assumes a number of things are already set up on your machine.

That you have a running version of JBoss with JBoss-Net installed. It shouldn't matter if you have the Jetty or Tomcat version. If not then you first need to go here to get the distribution.
That you have Jakarta Ant installed and ready to use, as discussed here.
That you have added the axis.jar from the relevant axis distro to your classpath. You need this for two things:
to use the tcpmon for debugging
to have the documentation and AP
 
If you are doing this exercise with a JBoss-3.0.x distro, then you will need to make a few changes to the code as we go along in order for it to work for you. Before you go any further, make sure that you've made the changes to your jboss-net.sar file discussed under the general jboss-net Guide page. Once you've done that, ie turned the jboss-net.sar file into a directory, then come back here.
This exercise assumes that you are using JBoss-3.2.x versions. This has jboss.net under the 'all' servewr option.
But if you first want to get your head around web services, then this will get you going on a smaller scale. In any case the principles are the same:

create a Java class
write a web-service.xml file
put these into a wsr archive
deploy on JBoss
access the service via a client application.
If you've looked over the Axis docs, then you'll have come across 'jws' files (ie 'java web service'), which are uncompiled Java source code, that is compiled on the fly by the Axis engine in the same way that jsp pages are compiled into servlets by Catalina.

JWS files don't seem to work in JBoss-Net. But that's not a big problem anyway. JBoss-Net remember is about integrating Axis into a J2EE server, and simple web services are unlikely to be deployed on it in anything other than testing and learning scenarios.

Instead, JBoss-Net needs to have a compiled class file deployed along with a deployment descriptor file bundled into an archive, which is then placed in the deploy directory of the JBoss server.

Axis by default uses a Web Services Deployment Descriptor (WSDD) for the configuration of web services. Under JBoss-Net this is a web-service.xml file.

JBoss-Net needs to have this web-service.xml file and put into a Web Service aRchive (WSR) along with the Java classes for the service. This lets you drop the wsr file into the running JBoss server where it will hot deploy.

However, it does not seem to update itself if you change the class file, rebundle it, and then drop in a new wsr file. This sort of change on the fly works with war files, but NOT at present with wsr files. If you want to make changes, then you will need to first undeploy the service. I've experimented with the <undeployment> WSDD files mentioned in the Axis docs. However, this didn't seem to work. So, for the moment, you need to delete the wsr file from the deploy directory, stop the server, and then restart it.

Now for the sample application, which uses Ant and build files for the project. Ok, it might be considered overkill, but as always Ant speeds up the edit, compile, copy, jar, etc routine.

You can download the code and build files in a zip file from here

The directory structure that you need to create looks like this:

jboss-axis

main

src

client

META-INF

web

WEB-INF

build.xml sit under jboss-axis (or whatever you want to call the directory).

web-services.xml sits under META-INF

testHelloWorld.java sits under main/src

testClient.java sits under main/client

Nothing actually goes into the web or WEB-INF directories, but are there for later use when you tie your application into web resources. If you want to, then you can put a placeholder index.html page under web.
 
First, the build.xml script, which is simple and short. You will need to make sure that you have JBOSS_DIST set in your environment.

<fileset dir="{jboss.home}/server/all/deploy/jboss-net.sar"> so that it becomes

<fileset dir="{jboss.home}/server/all/deploy/jboss-net.ear/jboss-net.sar"> and then it can find the axis.jar, wsdl4j.jar, etc which it needs to complile.
 
<project name="testHelloWorld" default="dist" basedir=".">

<property name="app.name" value="testHelloWorld"/>
<property environment="env" />
<property name ="jboss.home" value="{env.JBOSS_DIST}" />
<property name="deploy.home" value="{jboss.home}/server/all/deploy"/>

<property name="dist.home" value="{deploy.home}"/>
<property name="dist.src" value="{app.name}.jar"/>
<property name="dist.wsr" value="{app.name}.wsr"/>
<property name="dist.war" value="{app.name}.war"/>
<property name="dist.ear" value="{app.name}.ear"/>
<property name="javadoc.home" value="javadoc"/>

<property name="build.jboss-axis.dir" value="{basedir}/build"/>
<property name="build.classes.dir" value="{basedir}/build/classes"/>

<!-- need to add in details of src.dir -->
<property name="src.dir" value ="{basedir}"/>

<!-- the other jars for axis, servlets, etc are added to the classpath
This assumes they are under server/all
-->
<path id="base.libraries">

<fileset dir="{jboss.home}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="{jboss.home}/server/all/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="{jboss.home}/server/all/deploy/jboss-net.sar">
<include name="*.jar"/>
</fileset>

</path>

<target name="clean">
<delete dir="{build.jboss-axis.dir}"/>
</target>

<target name="validate">

<available property="classpath_id" value="base.libraries" file="{jboss.home}/server/all/lib/jboss.jar" />

</target>

<target name="fail_if_not_valid" unless="classpath_id">

<fail message="jboss.home={jboss.home} is not a valid JBoss dist directory " />

</target>

<target name="prepare" depends="clean, validate, fail_if_not_valid">
<mkdir dir="{build.classes.dir}"/>
<mkdir dir="{build.classes.dir}/client" />
<mkdir dir="{build.classes.dir}/WEB-INF"/>
<mkdir dir="{build.classes.dir}/WEB-INF/classes"/>
<mkdir dir="{build.classes.dir}/META-INF"/>
<mkdir dir="{javadoc.home}"/>


<!-- set the classpath for compiling files -->
<property name="classpath" refid="{classpath_id}" />


<!-- now copy across the files -->
<!-- <copy file="main/testHelloWorld.java" todir="{build.classes.dir}"/> -->
<copy todir="{build.classes.dir}">
<fileset dir="{src.dir}/web">
<include name="**/*"/>
</fileset>
</copy>

<copy todir="{build.classes.dir}/WEB-INF">
<fileset dir="{src.dir}/WEB-INF">
<include name="**/*"/>
</fileset>
</copy>

<copy todir="{build.classes.dir}/META-INF">
<fileset dir="{src.dir}/META-INF">
<include name="**/*"/>
</fileset>
</copy>
</target>

<target name="compile" depends="prepare">
<javac srcdir="main" destdir="{build.classes.dir}"
classpath="{build.classes.dir}"
debug="on" optimize="off" deprecation="on">
<classpath path="{classpath}" />
</javac>
<copy todir="">
<fileset dir="{src.dir}/main/src">
<include name="main/src/testHelloWorld.*"/>
</fileset>
</copy>
<!-- copy the client class file back to the client directory -->
<copy file="{build.classes.dir}/testClient.class" todir="{src.dir}/main/client"/>
</target>

<target name="javadoc" depends="prepare">
<javadoc sourcepath="src" packagenames="*"
destdir="{javadoc.home}"/>
</target>

<!-- axis doesn't like it this way, so will wrap the wsr file in a war file -->
<!-- build the wsr file -->
<target name ="wsr" depends="prepare,compile">


<jar jarfile="{build.jboss-axis.dir}/{dist.wsr}"
basedir="{build.classes.dir}">
<include name="testHelloWorld.class" />
<include name="META-INF/web-service.xml" />
</jar>
<copy file="{build.jboss-axis.dir}/{dist.wsr}" todir="{build.classes.dir}"/>
</target>

<!-- now build the war file -->
<target name="war" depends="wsr">
<jar jarfile="{build.jboss-axis.dir}/{dist.war}"
basedir="{build.classes.dir}">
<include name="index.html" />
<include name="WEB-INF/web.xml" />
<include name="WEB-INF/jboss-web.xml" />
</jar>
<copy file="{build.jboss-axis.dir}/{dist.war}" todir="{build.classes.dir}"/>
</target>

<!-- now build the ear file -->
<target name="ear" depends="war">
<jar jarfile="{build.jboss-axis.dir}/{dist.ear}"
basedir="{build.classes.dir}">
<include name="META-INF/application.xml" />
<include name="{dist.war}" />
<include name="{dist.wsr}" />
</jar>
</target>
<target name="dist" depends="wsr">
<copy file="{build.jboss-axis.dir}/{dist.wsr}" todir="{deploy.home}" />

</target>


</project>
 
Second, the web-service.xml file, which goes under the META-INF directory.
<!-- Example bare bones Web Service Deployment Descriptor -->
<deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="testHelloWorld" provider="java:RPC">
<parameter name="className" value="testHelloWorld"/>
<parameter name="allowedMethods" value=" getHelloWorldMessage"/>
</service>

</deployment>
 
Third, the Java source class, which goes under main/src.
public class testHelloWorld {
public testHelloWorld() {
}

public String getHelloWorldMessage(String name){
return "Hello world to " + name;
}
}
 
Fourth, the client to test the application, which goes under main/client.
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;


public class testClient
{
public static void main(String [] args) {
try {
String endpoint =
"http://localhost:8070/jboss-net/services/testHelloWorld";
String methodName = "getHelloWorldMessage";

Service service = new Service();
Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(methodName);

call.addParameter("name",
org.apache.axis.Constants.XSD_STRING,
ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);

String ret = (String) call.invoke( new Object[] { "AXIS!" } );

System.out.println(ret);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
 
Fifth, the runClient.bat file which goes under main/client and pulls together the appropriate classpath for you to run the client. You can probably do away with some of the jar files in the first batch. Indeed, you may find problems with the length of this line.
@echo off

rem bat to run client

set TEMP_CP=%TEMP_CP%

set JBOSS=C:/Java/jboss-tomcat/jboss rem now add client jars to classpath which jboss needs to run

set TEMP_CP=.;%JBOSS%/catalina/common/lib/servlet.jar;%JBOSS%/client/concurrent.jar;
%JBOSS%/client/jboss-common-client.jar;%JBOSS%/client/jboss-client.jar;
%JBOSS%/client/jboss-j2ee.jar;%JBOSS%/client/jaas.jar;
%JBOSS%/client/jboss-jsr77.jar;%JBOSS%/client/jcert.jar;
%JBOSS%/client/jboss-net-client.jar;%JBOSS%/client/gnu-regexp.jar;
%JBOSS%/client/jbosssx-client.jar;%JBOSS%/client/log4j.jar;%JBOSS%/client/jnet.jar;
%JBOSS%/client/jnp-client.jar;%JBOSS%/client/jsse.jar;%JBOSS%/client/jacorb.jar;
%JBOSS%/client/jbossmq-client.jar;%JBOSS%/client/getopt.jar;
%JBOSS%/client/jboss-iiop-client.jar;%JBOSS%/client/jbossmx-ant.jar;
%JBOSS%/client/jboss-system-client.jar;%JBOSS%/client/jboss-transaction-client.jar

rem add in the classpath to the axis required jar files

set TEMP_CP=%TEMP_CP%;%JBOSS%/server/all/deploy/jboss-net.sar/axis.jar;%JBOSS%/server/all/deploy/jboss-net.sar/jaxrpc.jar;
%JBOSS%/server/all/deploy/jboss-net.sar/saaj.jar;
%JBOSS%/server/all/deploy/jboss-net.sar/wsdl4j.jar;
%JBOSS%/server/all/deploy/jboss-net.sar/commons-logging.jar;
%JBOSS%/server/all/deploy/jboss-net.sar/commons-discovery.jar

set TEMP_CP=%TEMP_CP%;%CLASSPATH%

java -classpath %TEMP_CP% testClient %1 %2 %3 %4 %5
 
With all of the files in their right spot, open a console in the same directory as the build.xml file and enter this command: build

This will process the main target, which will compile the class files, build the wsr file, and copy the compiled client class back to the client directory. It also puts the wsr file into the deploy directory of your JBoss server. You should see some output in the console window telling you what is happening.

Any errors that you encounter are probably due to missing items in your classpath, as the build.xml script checks that all of the Axis required jars are in your classpath. If jars are missing, then they will appear as errors, class not found.

Open a browser and navigate to

http://localhost:8080/jboss-net/servlet/AxisServlet where you should see a list of items, including your newly deployed service at the bottom of the page. Now navigate to

http://localhost:8080/jboss-net/services/testHelloWorld?wsdl where you should see the wsdl details of your application. Note that it provides the targetnamespace of http://localhost:8080/jboss-net/services/testHelloWorld which is where we find the webservice.

Some of you will realise that the client goes to http://localhost:8070/jboss-net/services/testHelloWorld, and wonder what gives. Well, it's an excuse to introduce another debug tool, the tcpmon which comes with Axis. The tcpmon intercepts messages on one port (ie 8070), shows them in one window, and then redirects the message to its designated port (ie 8080) and picks up the response.

Open another console window and enter this command (assuming that you have added the axis.jar to your classpath. Unfortunately, I have found that this ONLY works if the axis.jar is in your system classpth.):

java org.apache.axis.utils.tcpmon

You should have a Swing application open up. Put 8070 in the 'Listen Port #' textfield, put localhost in the 'Target hostname' textfield, and 8080 in the 'Target Port #' textfield. The radio button for 'Act as a listener' should be selected. Now click 'add' and a new tab 'Port 8070' should appear. Select it.

Open another console window and navigate to the jboss-axis/main/client directory and run the command:

runClient

You should see "Hello World to AXIS!" appear in your client console window, and both the sent and received SOAP messages in the tcpmon windows.

That's it, you've done it. You built a simple application, deployed it on JBoss-Net and discovered how similar it is to the raw Axis, which sits on the server.

I've also done a Hello World using EJB-JSP example with JBoss-Net which you can find here.

You can also go through the jboss-net examples from the cvs files at Sourceforge to also browse through. Go to:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jboss/contrib/jboss.net/ and look under samples and testsuite. Needless to say, these only give you some indication of what's going on, and you really should build JBoss-Net from the sourcecode as described in a howto here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值