JBoss-WS4ee Hello World Example

xiaoxiao2021-03-06  45

Http://www.csd.abdn.ac.uk/~bscharla/teaching/mtp_software/jboss/jboss-ws4ee-helloWorld.shtml

THIS Builds a Simple Example Showing What Can Be Done with jboss-4.0.0 using apache axis.Updated 3 September 2004

This Example Works with the Newest Builds of JBoss-4.0 and ITS Web Services Programming Model for J2EE 1.4 Using Apache Axis 1_1 for ITS Core. This is different from the jboss-net model under jboss-net model under jboss-3.2.x.

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-4.0 with jboss-ws4ee. If not the number 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 disrto to your classpath. You need this for things:

To Use the tcpmon for debugging

To Have the Documentation and AP

to copy the wsdl4j.jar (which might be missing from your jboss distro) to under jboss-home / server / default / deploy / jboss-ws4ee-sar The ExerciseThis exercise assumes that you are using JBoss-4.0.x versions. This has JBoss-ws4ee.sar Under the 'default' server option.

If you did the jboss.net version of the application, then you'll notice that it is more complex to set up and implement, due to extra deployment descriptors and the interface plus implementation model of programming. I'm sure that given time some Of these Extra Details Will Be Simplified with xdoclet or Other Tools, So Be Patient with The Examples.

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 interface and its implementation write, and generate the deployment description file put these into a war archive deploy on JBoss access the service via a client application. Now for the sample application, which uses Ant to build the 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 in Eclipse:

The Web Directory Just Has A PlaceHolder Index Page, AS this is to be a console application.

The WSDL Directory Is WHERE The Axis Generated WSDL File Will Be Placed, And Must Sit Under The Web-Inf Directory To Comply with J2EE 1.4.

The Main / Src Directory Will Hold The Java Application Files, While Main / Client Will Hold Our Client Classes.

THE

Axis-ws4ee.jar (Under Jboss-Home / Server / Default / Deploy / JBoss-WS4ee.SAR),

JBoss-jaxrpc.jar (Under Jboss-Home / Server / Default / LIB) and

Namespace.jar (Under Jboss-Home / LIB)

are there just to keep the build path in Eclipse complete. The Build FileFirst, the build.xml script, which is simple and short. You will need to make sure that you have set jboss.home correctly for your system.

< Mkdir Dir = "{build.classes.dir} / web-inf" />

The Interface and ImplementationSecond, actual Java classes that are to be exposed need to so via do a` public interface detailing the available methods and their signatures. You'll see that it must extend java.rmi.Remote. This is HelloWorld.java package com.myApp;

Import java.rmi.remote; import java.rmi.remoteexception;

public interface HelloWorld extends Remote {public String getHelloWorld (String name) throws RemoteException;}. Third, the implementation class for the interface, implements our interface plus javax.xml.rpc.server.ServiceLifecycle As you can see Eclipse added the requisite destroy ( ) and INIT (Object arg0) Methods for US. This is helloworld_impl.java. package com.myapp;

Import java.rmi.remoteException; import javax.xml.rpc. *; import javax.xml.rpc.server.serviceLifeCyle; public class helloworld_impl imports helloworld, serviceLifeCyle {

Public String GetHelloWorld (String Name) throws RemoteException {Return "Hello World to" Name;}

Public void init (Object arg0) throws serviceException {// Todo auto-generated method stub

}

Public void destroy () {// Todo auto-generated method stub

}

} The Deployment DescriptorsFourth, the web service needs to have a number of descriptors in order to successfully deploy on a J2EE 1.4 server. All of them must be deployed under the WEB-INF directory of your war file. You'll already be familiar with the web.xml and jboss-web.xml files, which detail the servlets and enterprise resources being used in the application. The new ones are webservices.xml, jaxrpc-mapping.xml and the hello.wsdl file under the 'wsdl' directory Which is in lowercase.

As Our Is A Simple Application, There Is Nothing In The Jboss-Web.xml File. However, if You Were Using JNDI To Connect To A Database, Or To An EJB, THEN THE DETAILS WOULD Go Here.

The web.xml file is more interesting. You see a 'HelloWorldServlet' listed there, but we've not written a servlet. However, you'll see that the servlet class is listed as our implementation class above . This is the magic of JAXRPC web services, and why we need to implement the ServiceLifecycle. This passes our implementation to the JAXRPC servlet to class via expose our its interface at the specified servlet mapping in the web.xml file. The welcome file is there as a placeholder, as this is a console app.The other interesting point is that the web-app tag at the start points to web-app_2_4.xsd instead of a DTD as in the past. in othere words, DTD declarations are a Thing of the Past for J2EE 1.4 Apps, As They Are Moving to Xml Schemas, Which Allow More Validation Power.

HelloWorldServlet com.myapp.helloWorld_Impl

HelloWorldServlet / exactpath / jse

index.html

The webservices.xml file explains which webservice you're exposing and its details. This includes the name and location of the WSDL file, the name of the interface class, the name of the servlet class, and the name of the jaxrpc-mapping file, as well as the name of the endpoint for your web service. As you can see, if there were more than one web service in the application, then it would comprise another 'webservice-description' set of tags HelloWorldService WEB-INF / wsdl / hello .wsdl web-inf / jaxrpc-maping.xml HelloWorld HelloWorld com.myapp.helloworld HelloWorldServlet The jaxrpc-maping.xml file does Not Have to Be Called by this name, but mu sticing the name specified in the 'jaxrpc-mapping-file'

tag in the webservices.xml file. The file here is about as simple as it can be, and will often end up being more complex. The main points here are that the file details the name of the Java package for the service, and the URI were it is located.The jaxrpc-mapping.xml file is there to help the JAXRPC compiler make sense of the objects detailed in the WSDL file. It helps the conversion from WSDL to Java objects, and if the Java objects are complex, then This file will be too. <.com-TyPINGAPP<<<-TyP://myapp.com/helloworld >< The hello.wsdl file does not have to be called the name of th Name of th E Application. IT Must Be Placed in A 'WSDL' Directory (Note LowerCase) Under the Web-inf (or meta-inf) Directory of Your application. You do not need to write this file by hand, as it can be generated by .

The main serctions in the file are the services, bindings, and port types. With these details you can use any WSDL file to generate a Java client using the WSDL2Java tool that comes with Axis. The image below shows how the different parts of the WSDL file fit together. This is generated from one of the web service components that is included as part of the Eclipse Web Tools project. The downloads are at the bottom of the page.

. The ClientWith all of our code and deployment descriptors sorted out, we can now develop the client to use the application There are three different types of clients, which we could develop with JAXRPC; those using generated stubs from the WSDL file, those using dynamic proxies, and those which use a dynamic invocation interface (DII). We'll use a DII type of client even though it is the trickier type of client to build, as it does not take advantage of the WSDL to make Writing the client.

The Other Reason To Start Using Dii Clients Is That It Means WE CAN Avoid Configuring Axis Client-Side Files.

If you're just wanting to connect plain ordinary Java objects (POJO) JAX-RPC stub derived clients to web services deployed under JBossWS, then you need to take the axis-client-config.xml file from under jboss / server / default / Deploy / JBoss-ws4ee.sar / Meta-Inf and Place It in The Directory from Which Your Client Will Be Run, And Rename It To The Default Name That Axis Uses, Client-Config.Wsdd. if you don't do this, then you will receive an error message complaining about axis not finding 'No engine configuration file'. This is because when you run wsdl2java, it uses the org.apache.axis.client.Service class, which then looks for a configuration file. package com.myapp; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.rpc.ServiceFactory; import Javax.xml.rpc.parametermode; public class diicylient {

// Modified from Sun J2EE JAXRPC EXAMPLE

private static String endpoint = "http: // localhost: 8070 / simple-ws4ee / exactpath / jse"; private static String qnameService = "HelloWorldService"; private static String qnamePort = "HelloWorld"; private static String ENCODING_STYLE_PROPERTY = "javax.xml .rpc.encodingstyle.namespace.uri "; private static string ns_xsd =" http://www.w3.org/2001/xmlschema" ;private static string uri_encoding = "http://schemas.xmlsoap.org/soap/ENCODING / ";

Public static void main (String [] args) {

System.out.println ("endpoint address =" endpoint);

Try {servicefactory factory = servicefactory.newinstance (); service service = factory.createservice (new qname (qnameservice));

Qname port = new qname (qnameport);

Call call = service.createCall (port); call.setTargetEndpointAddress (endpoint); call.setProperty (Call.SOAPACTION_USE_PROPERTY, new Boolean (true)); call.setProperty (Call.SOAPACTION_URI_PROPERTY, ""); call.setProperty (ENCODING_STYLE_PROPERTY, URI_ENCODING); QNAME QNAME_TYPE_STRING = New QNAME (ns_xsd, "string"); call.setreturntype (QNAME_TYPE_STRING);

Call.SetoperationName (New QName (Body_NameSpace_Value, "GetHelloWorld")); Call.AddParameter ("String_1", QNAME_TYPE_STRING, ParameterMode.in); string [] params = {"jboss!"};

String result = (string) Call.invoke (params); System.out.println (Result);

} catch (exception ex) {ex.printstacktrace ();}}} with all of the file in their right spot, Open a console in the same directory as the build.xml file and enter one command: Ant (or run it from Your IDE Such as Eclipse.

This Assumes That Apache Ant is in your environment path. If not, the you need to add it.

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

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 / WS4EE / WHERE You Should See A List of items as shown below.

Your Screenshot May Look Different from The One Above, Which Was Done with JBOSS-4.0.0.0RC1.

CHECK That the 'Validate' Page Works Ok. As Note The Top of the Page You Be You NEED To Add in The Wsdl4j.jar from Axis To The Jboss-ws4ee.SAR Directory.if You Go To The 'View' Page Our Service Should Be listed in The Available Services, Along with The link to its wsdl file.

Some of you will realise that the client goes to http: // localhost:.. 8070 / simple-ws4ee / exactpath / jse, 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 Theme in One Window, And The Redirects The Message To ITS Designated Port (IE 8080) And Chicks 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-ws4ee.jar under jboss / server / default / deploy / jboss-ws4ee . SAR 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 main / client directory and rain the commit:

Ant Run_Client

If you have a 'connection refused' Error, Then Check That You Have The TCPMON Started As Note, and That The App Has Deployed Correctly To Jboss.

You should see "Hello World to jboss!" 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 JAXRPC application and deployed it on JBoss -ws4ee.

You can also go through the ws4ee example from the wiki files at jboss to also browse through. Go to: http://www.jboss.org/wi/wiki.jsp? Page = jbossws

转载请注明原文地址:https://www.9cbs.com/read-73445.html

New Post(0)