Passa ai contenuti principali

Tomcat axis tutorial

Axis is essentially a SOAP engine, that is a framework for constructing SOAP processors such as clients, servers, gateways, etc.

Installing Axis on Tomcat

At first download Axis from the Apache project:

http://ws.apache.org/axis/java/index.html

Explode the zip folder. Now you need to deploy the Axis engine to Tomcat: Here's how to do it:

Rename the folder axis under AXIS_HOME/webapps to axis.war and copy the folder under "webapps" of your tomcat

Verify that Axis has been deployed correctly:

http://localhost:8080/axis/



















Publishing Web Services with Axis

Let's say we have a simple class like the following:



package test;

public class HelloWorld {

public String hello(String message) {

return "Invoked with" +message;

}

}
How do we go about making this class available via SOAP? There are a couple of answers to that
question, but we'll start with an easy solution.

At first compile the class :

javac -d . HelloWorld.java

Now copy the class under WEB-INF/classes of your axis.war

Ok, now the last step is registering your Web Service so that Axis is aware of it. A Web Service can be registered with a Deployment Descriptor (WSDD) file. A deployment descriptor contains a bunch of things you want to "deploy" into Axis - i.e. make available to the Axis engine.

Here's a simple wsdd file for our WebService:

<deployment
xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="HelloWorld" provider="java:RPC">
<parameter name="className" value="test.HelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>

Pretty simple, really - the outermost element tells the engine that this is a WSDD deployment,and defines the "java" namespace. Then the service element actually defines the service for us. A service is a targeted chain which means it may have any of: a request flow, a provider, and a response flow.

In this case, our provider is "java:RPC", which is built into Axis, and indicates a Java RPC service. We need to tell the RPCProvider that it should instantiate and call the correct class (e.g. test.HelloWorld), and another to tell the engine that any public method on that class may be called via SOAP (that's what the "*" means)

Now save your wsdd file as deploy.wsdd and run the AdminClient which is an Utility to deploy the WebService

java -classpath %AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.client.AdminClient deploy.wsdd

If everything was Ok now you should see the WebService deployed on Axis: check the "List" option from the URL http://localhost:8080/axis/

The client

Let's take a look at an example Web Service client that will call the hello method of the HelloWorld Web Service


package test;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.net.URL;

public class Client {

public static void main (String args[]) {

try {

String url;

url="http://localhost:8080/axis/services/HelloWorld";

Service service = new Service();

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

call.setTargetEndpointAddress(new URL(url));

call.setOperationName(new QName("HelloWorld", "hello"));

Object[] params = new Object[1];

params[0] = "Hello Message";

Object result = call.invoke(params);

System.out.println("result is " + result);

}

catch(Exception e) {

e.printStackTrace();

}


}


}


In order to invoke our Web Service we use the Service and Call objects. These are the
standard JAX-RPC objects that are used to store metadata about the service to invoke.

With this we set up our endpoint URL - this is the destination for our SOAP message.

call.setTargetEndpointAddress(new URL(url));

With this we define the operation (method) name of the Web Service.

call.setOperationName(new QName("HelloWorld", "hello"));

Then we actually invoke the desired service, passing in an array of parameters - in this case just one String.

Object result = call.invoke(params);
Obtaining WSDL for deployed services
When you make a service available using Axis, there is typically a unique URL associated with that service. For our service it's http://localhost:8080/axis/services/HelloWorld?wsdl
When you point your browser to that location, Axis will automatically generate a service description for the deployed service, and return it as XML in your browser :





<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
targetNamespace="http://localhost:8080/axis/services/HelloWorld"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://localhost:8080/axis/services/HelloWorld"
xmlns:intf="http://localhost:8080/axis/services/HelloWorld"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:message name="helloRequest">
<wsdl:part name="in0" type="xsd:string" />
</wsdl:message>
<wsdl:message name="helloResponse">
<wsdl:part name="helloReturn" type="xsd:string" />
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="hello" parameterOrder="in0">
<wsdl:input message="impl:helloRequest" name="helloRequest" />
<wsdl:output message="impl:helloResponse"
name="helloResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="hello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="helloRequest">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://test" use="encoded" />
</wsdl:input>
<wsdl:output name="helloResponse">
<wsdlsoap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://localhost:8080/axis/services/HelloWorld"
use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding"
name="HelloWorld">
<wsdlsoap:address
location="http://localhost:8080/axis/services/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



Generating Java Classes from WSDL

Another approach is to generate Java Classes starting from WSDL contract. You can use the
AdminClient to follow this approach:

java -classpath %AXIS_HOME%/lib/wsdl4j-1.5.1.jar;%AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/HelloWorld?wsdl

This will generate the following classes under the service namespace which is localhost\axis\services\HelloWorld

HelloWorld.java
HelloWorldService.java
HelloWorldServiceLocator.java
HelloWorldSoapBindingStub.java

Commenti

Post popolari in questo blog

Tomcat maxThreads configuration

Tomcat maxThreads represents the maximum number of request processing threads to be created by the HTTPConnector. < Connector port= " 8443 " protocol= " org.apache.coyote.http11.Http11Protocol " maxThreads= " 250 " SSLEnabled= " true " scheme= " https " secure= " true " clientAuth= " false " sslProtocol= " TLS " connectiontimeout= " 20000 " /> This determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to the default value of 200. How the process works: At server startup, the HTTP Connector will create a number of processing threads based on the value configured for the minSpareThreads attribute. Each incoming request requires a thread for the duration of that request. If the number of simultaneous requests cannot be handled by the currently available request processing threads, additio...

Tomcat Websocket example

A WebSocket is a full-duplex communication mechanism that allows both textual and binary messages to be sent between clients and servers, without the HTTP request/response life cycle. WebSockets allow either the client or the server to send a message at any time, providing an asynchronous solution for working with data while the user is performing a task. Websockets API are included in Tomcat 7 Web server distribution so you don't have to download any extra library: In this tutorial we will show how to create a WebSocket example using Apache Tomcat and Eclipse. Start by creating on Eclipse a new Dynamic project named websocket-example : We will now create a server side class named WebSocketDemo that is going to echo messages from a Javascript client: package com . sample ; import java . io . IOException ; import java . nio . ByteBuffer ; import javax . websocket . OnMessage ; import javax . websocket . Session ; import javax . websocket . server . ServerEndp...

Tomcat session listener example

As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server. You can use two interfaces as listener for your Session: HttpSessionListener triggers the listener when a new session is created or destroyed HttpSessionBindingListener triggers the listener when an Object is bound/unbound from the Session HttpSessionListener example: package com.sample; import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionEvent; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent se) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; } public static int getActiveSessions() { return activeSessions; } } As you can see, all you have to do is implementing two meth...