Sparkles

that were shone when I got tempered!

ActiveMQ implementation with web based listener

leave a comment »

Installing and configuringInstalling and configuring

1. Download activemq from the ActiveMQ page and extract.  [link]

2. Start ActiveMq

$ cd /[path-to-activemq-base direcctory]/bin
$ ./activemq start

3. ActiveMQ default port will be 61616. And defual port for activemq UI will be 8161.

4. So, you can see the ActiveMQ UI via localhost:8161.

5. Queue details can be used using the below link http://localhost:8161/admin/queues.jsp

Creating the producer :

You can create an activemq connection and send a simple message to the queue as mentioned below

private void initializeNSendMessage(){

	Session session = null;
	Connection connection = null;
	try {

		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		connection = connectionFactory.createConnection();
		connection.start();

		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue("mf_engine.demo-queue");

		MessageProducer producer = session.createProducer(destination);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

		for (int i = 0; i < 225; i++) {

			String strMessage = "Test message #"+UUID.randomUUID()+"-"+i;
			TextMessage message = session.createTextMessage(strMessage);
			System.out.println(" !!! sendMessage(). : Sent message : " + strMessage);
			producer.send(message);	

		}

	} catch (Exception e) {
		
		System.err.println(e);
		
	}finally {

		try {session.close();	} catch (Exception e) {session = null;}
		try {connection.close();} catch (Exception e) {connection = null;}
		
	}

}

Creating the listener

Once you send the message from activemq producer it enques at the activemq. You need to create a listener to listen to those message.
To make the lister keep alive, I have created a ServletContextListener and start a n number of threads to create the connectin with the activemq instance. There, we plug a MessageListener to listen to the activemq messages. In this case, onMessage will be called when the message recieved to the activemq instance.

QueueConsumerInitializer.java

public class QueueConsumerInitializer implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("QueueConsumerInitializer.contextDestroyed()");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("QueueConsumerInitializer.contextInitialized()");
		
		int noOfConsumers = 3;
		System.out.println("contextInitialized().noOfConsumers : " + noOfConsumers);
		
		for (int i = 0; i < noOfConsumers; i++) {
		
			System.out.println("contextInitialized().QueueConsumer #"+i+" started!!! ");
			
			QueueConsumer queueConsumer = new QueueConsumer();
			Thread thread = new Thread(queueConsumer);
			thread.start();
			
		}
		
	}
	
}

QueueConsumer.java

public class QueueConsumer implements Runnable{

	public void run() {
		System.out.println("run(). : Consuming the message..");
		consumeMessage();
		
	}
	
	private void consumeMessage() {
	
		try {

			String 	URL 		= "tcp://localhost:61616";
			String 	queueName 	= "mf_engine.demo-queue";
			
			javax.jms.Connection connection = null;
			Session session = null;
			Destination destination = null;
			MessageConsumer consumer = null;
			

			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
			connection = connectionFactory.createConnection();
			connection.start();
			
			
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue(queueName);
			   				
			
			consumer = session.createConsumer(destination);
			consumer.setMessageListener(new QueueListener());
				

		} catch (Exception e) {
			
			System.out.println("QueueConsumer.consumeMessage() Error"+e);
		}

	}

}

QueueListener.java

public class QueueListener  implements MessageListener {
	
	private static	Logger 			logger 						= Logger.getLogger(QueueListener.class);	

	public void onMessage(Message message) {

		if (message instanceof TextMessage) {

			try {
				TextMessage 		textMessage 		= (TextMessage) message;
				String 				messageStr 			= textMessage.getText();
				System.out.println("onMessage()." + (messageStr != null ? messageStr.length() + " lengthed" : "NULL") + " Message received!" + " [body : " + messageStr+"]");
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
}

Source :

Advertisements

Written by Namal Fernando

August 25, 2017 at 7:31 am

Posted in ActiveMQ

CouchDB – Updates and Deletes

leave a comment »

You can delete document using the _id and rev as mentioned below

curl -X DELETE http://localhost:5984/couch_dev/document_id_100?rev=1-f87d6fa9b847b8ac0f92f3210cc02db5

But this will only make it invisible (anyway it IS-A delete according to the CouchDB Lingo). Space will still not be released. If you want to permanently delete it and release the space, you must purge it like mentioned below.

curl -X POST -d '{"document_id_100":["1-f87d6fa9b847b8ac0f92f3210cc02db5"]}'http://localhost:5984/couch_dev/_purge -H "Content-Type:application/json"

But if we want to use this to delete temporary/expired data according to the experts, this is inappropriate use of couchdb. In this case we can delete whole db periodically.

Written by Namal Fernando

August 17, 2017 at 5:31 am

CouchDB – Retreiving Data

leave a comment »

Once Documets are available in the couchdb, they can be directly accissible using the _id value as below.

 http://localhost:5984/couch_dev/search-CMB-001 

You can use simple GET request to retrieve this data.

 curl -X GET http://localhost:5984/couch_dev/search-CMB-001 

 

Querying Documents

Documents can be queried also by creating views.

These views can be created using the JavaScript or CofeeScripts. They can be used to query multiple documents.

In this case first you need to create views. This can be easily created using the UI as mentioned below

Then you can add a view. Here create a design document also. If you already have a designdocument, create a view on that one.

Once you click on that view it will be selected.

 

After this documents can be retreived through these views using below like get query.

http://{couchDB-host}:{couchDB-port}/{couchDB-dbName}/_design/{couchDB-design document name}/_view/{couchDB-view name}?key=”{couchDB-document key}”

Note : couchDB-document key must be the key that was mentioned in the emit function.

 

Sample Functions

01. doc

function(doc) {
emit(doc._id, doc);
}

This will provide whole json providing the _id as the key.
So, You can retreive a particular doc using id as mentioned below.

curl -X GET 'http://localhost:5984/couch_dev/_design/hotels/_view/doc?key="search-CMB-001"'

02.rev

function(doc) {function(doc) {
emit(doc._id, doc._rev);
}

This will provide _rev id providing the _id as the key.
So, you can retreive the particular rev using the id as mentioned below

curl -X GET 'http://localhost:5984/couch_dev/_design/hotels/_view/rev?key="search-CMB-001"'

 

03.rooms

function(doc) {function (doc) {
for (var x = 0, htlLen = doc.hos.length; x < htlLen; x++) {
emit(doc._id+'-'+doc.hos[x].hotelcode, doc.hos[x].rms);
}
}

This will provide all rooms as a list for each hotel, providing the “id-hotelcode” as the key.
So, you can retreive all rooms for a particular hotel as mentioned below

curl -X GET 'http://localhost:5984/couch_dev/_design/hotels/_view/rooms?key="search-CMB-001-GLD"'

 

04.room

function(doc) {function (doc) {
for (var x = 0, htlLen = doc.hos.length; x < htlLen; x++) {
for (var y = 0, rmLen = doc.hos[x].rms.length; y < rmLen; y++) {
emit(doc._id+'-'+doc.hos[x].hotelcode+'-'+doc.hos[x].rms[y].roomcode, doc.hos[x].rms[y]);
}
}
}

This will provide all room objects one by one for each hotel, providing the “id-hotelcode-roomcode” as the key.
So, you can retreive a particular room object providing the room code as below.

curl -X GET 'http://localhost:5984/couch_dev/_design/hotels/_view/room?key="search-CMB-001-GLD-SGL-2-A"'

 

Written by Namal Fernando

August 17, 2017 at 4:57 am

Posted in CouchDB, Retreive Data

CouchDB – Insert Data

leave a comment »

Data can be updated to the CouchDB as json using a HTTP POST call. In this case, content-type must be used as “application/json“. This also can be done using any http client such as Postman, SoapUI, java.net.HttpURLConnection or even simple curl command.

In this case first you must create a Database in the CouchDB. You can do this using the fotonUI as meentioned below or via curl command.

 

Then you can POST the JSON you want to insert as mentioned below.

curl -X POST -d '{"_id":"document_id_01", "Name":"Namal Fernando", "Age":28}' http://localhost:5984/couch_dev -H "Content-Type:application/json"

 

In this case, this _id filed (eg : document_id_01)  is very important. Because, document will be identified using this id. Once the document is updated, it will be displayed in Futon UI as mentioned below.

 

We can write json documents to couchdb in java as mentioned below.

URL mUrl = new URL("http://localhost:5984/couch_dev");
HttpURLConnection con = (HttpURLConnection) mUrl.openConnection(); 
con.setRequestMethod("POST"); con.setDoOutput(true); con.setUseCaches(false); 
con.setDoInput(true); con.setRequestProperty("user-agent","Mozilla(MSIE)"); 
con.setRequestProperty("Accept-Encoding","gzip,deflate"); 
con.setRequestProperty("Content-Type", "application/json"); 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
byteArrayOutputStream.write(payLoad.getBytes()); 
byteArrayOutputStream.writeTo(con.getOutputStream()); 
System.out.println("writeToCouchDB().con.getHeaderFields() : [Response Code : " + con.getResponseCode() + "], [Headers : " + con.getHeaderFields()+"]");

Bulk insert.

When it is needed to insert large number of json documents, you can insert them as bulk insert.Here, there are two main changes will happen.

1. URL changes :    URL must be changed from

http://localhost:5984/couch_dev to http://localhost:5984/couch_dev/_bulk_docs.

2. Payload : Payloads must be mentioned as a JSONArray as mentioned below.

{"docs":[{"_id":"document_id_02", "Name":"Lakmini Weerasekara", "Age":30},{"_id":"document_id_03", "Name":"Ahas Rehan", "Age":3},{"_id":"document_id_100", "Name":"Ranjith Fernando", "Age":66}]}

eg :

curl -X POST -d '{"docs":[{"_id":"search-KLT-003","hos": [{"hotelcode": "CTS","hotelname": "Citrus Hotel","rms": [{"roomcode": "SGL-1-G","roomname": "Single Room1G"},{"roomcode": "DBL-1-H","roomname": "Double Room1H"},{"roomcode": "TRP-1-I","roomname": "Triple Room1I"}]},{"hotelcode": "AVN","hotelname": "Avani Hotel","rms": [{"roomcode": "SGL-2-G","roomname": "Single Room2G"},{"roomcode": "DBL-2-H","roomname": "Double Room2H"},{"roomcode": "TRP-2-I","roomname": "Triple Room2I"}]},{"hotelcode": "RYP","hotelname": "Royal Palms Hotel","rms": [{"roomcode": "SGL-3-G","roomname": "Single Room3G"},{"roomcode": "DBL-3-H","roomname": "Double Room3H"},{"roomcode": "TRP-3-I","roomname": "Triple Room3I"}]}]},{"_id":"search-PLN-002","hos": [{"hotelcode": "SDA","hotelname": "Sudu Araliya Hotel","rms": [{"roomcode": "SGL-1-D","roomname": "Single Room1D"},{"roomcode": "DBL-1-E","roomname": "Double Room1E"},{"roomcode": "TRP-1-F","roomname": "Triple Room1F"}]},{"hotelcode": "MHN","hotelname": "Maha Nuge Hotel","rms": [{"roomcode": "SGL-2-D","roomname": "Single Room2D"},{"roomcode": "DBL-2-E","roomname": "Double Room2E"},{"roomcode": "TRP-2-F","roomname": "Triple Room2F"}]},{"hotelcode": "RNK","hotelname": "Ranketha Hotel","rms": [{"roomcode": "SGL-3-D","roomname": "Single Room3D"},{"roomcode": "DBL-3-E","roomname": "Double Room3E"},{"roomcode": "TRP-3-F","roomname": "Triple Room3F"}]}]},{"_id":"search-CMB-001","hos": [{"hotelcode": "HIL","hotelname": "Hilton Hotel","rms": [{"roomcode": "SGL-1-A","roomname": "Single Room1A"},{"roomcode": "DBL-1-B","roomname": "Double Room1B"},{"roomcode": "TRP-1-C","roomname": "Triple Room1C"}]},{"hotelcode": "GLD","hotelname": "Galadari Hotel","rms": [{"roomcode": "SGL-2-A","roomname": "Single Room2A"},{"roomcode": "DBL-2-B","roomname": "Double Room2B"},{"roomcode": "TRP-2-C","roomname": "Triple Room2C"}]},{"hotelcode": "KNB","hotelname": "Kingsbury Hotel","rms": [{"roomcode": "SGL-3-A","roomname": "Single Room3A"},{"roomcode": "DBL-3-B","roomname": "Double Room3B"},{"roomcode": "TRP-3-C","roomname": "Triple Room3C"}]}]}]}' http://localhost:5984/couch_dev/_bulk_docs -H "Content-Type:application/json"

This will update all the documents at once.

 

Written by Namal Fernando

August 16, 2017 at 10:37 pm

Posted in CouchDB, Insert

CouchDB – Installation and Setup

leave a comment »

First Time Installation

Note OS is Ubuntu 16.04 (xenial). Please refer the couchdb-docs for other versions.

Run below commands in there


echo "deb https://apache.bintray.com/couchdb-deb xenial main" \
| sudo tee -a /etc/apt/sources.list

curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc \
| sudo apt-key add -

sudo apt-get update

sudo apt-get install couchdb

In this case you have to pass through a series of initial setup steps.Here, I have selected standalone version

And finally it will be installed and run in default 5984 port.

You can access the UI at http://127.0.0.1:5984/_utils/#

CouchDB will be installed at /opt/couchdb folder. (in older versions this was installed in /etc/couchdb).

Change the configurations.

This can be done via couchdb config area (http://127.0.0.1:5984/_utils/#_config/couchdb@localhost) as mentioned below

In the older version this could be done by accessing /etc/couchdb/default.ini file.

Restarting couchdb


sudo service couchdb restart

Written by Namal Fernando

August 16, 2017 at 5:52 am

Posted in CouchDB, Installation

Create / Build / Package and Deploy web service(s) with Axis2

leave a comment »

Create a java project using the created WSDL mentioned post Create a WSDL for a WebService

Initially the project structure will be like mentioned below.

1_pub_projectstructure

Generate the server code using Axis2CodeGen pluggin :

Select File > Select New > Click Other…

Select Axis2 Code Generator under the Axis2 Wizrds and Click Next

Select Generate the Java Source code from the WSDL file option and Click Next

Provide the WSDL file location eg : /rezsystem/workspace/MathService/src/gradethree/wsdl/GradeThreeMath.wsdl Use Browse button if you need.

Select
CodeGen Option : custom
Generate Service Side Code
Generate a default services.xml
Generate interface for Skeleton

Provide an Output Path (eg : /home/namal/Desktop/WORKS/WS/Math/Grade3). Use Browse button if needed.

Click Finish.

Take generated files to the project

This will generate the files mentioned in the LHS of the below image. Put those files in to the project as mentioned in the RHS.
Here, I have added resources and the server code to a seperate package (gradethree) since I have an intension to add another WebService within this same Project. You need to modify the package declarations in the Server Code and the services.xml

2_pub_folder_structure

Implement the logics in the GradeThreeMathSkeleton class.

eg : for Addition Operation


public gradethree.org.tempuri.AdditionResponse Addition(gradethree.org.tempuri.AdditionRequest additionRequest0){

AdditionResponse additionResponse = new AdditionResponse();

int operand1 = additionRequest0.getOperand1();
int operand2 = additionRequest0.getOperand2();

additionResponse.setResult(operand1+operand2);
additionResponse.setRemarks("This is enough for Grade Three Addition!");

return additionResponse;

}

Packaging : Generate the aar and jar files using the build.xml

Ant task :

<echo>Building GradeThreeMath AAR</echo>
<delete dir="${classes}/META-INF" />
<copy toDir="${classes}/META-INF" failonerror="false">
<fileset dir="${resources}/gradethree">
<include name="*.xml" />
<include name="*.wsdl" />
<include name="*.xsd" />
</fileset>
</copy>

<jar destfile="${lib}/GradeThreeMath.aar">
<fileset dir="${classes}">
<include name="META-INF/**" />
</fileset>
</jar>

<jar destfile="${lib}/GradeThreeMath.jar">
<fileset dir="${classes}">
<include name="gradethree/**" />
</fileset>
</jar>

So, this will return the GradeThreeMath.jar and GradeThreeMath.aar as follows.

3_pub-packaging

 

Deploy :

You can deploy them as mentioned below
4_pub-dployment

Start the server.

WSDL : http://localhost:8081/axis2/services/GradeThreeMath?wsdl

You can test the service using the SOAP UI with above WSDL as below

5_pub_soapui

You can add another service also within the same project, build/package with another ant task and deploy as mentioned in the below project.

6_pbl

Project lin : https://github.com/namalfernandolk/MathService.git

Written by Namal Fernando

October 25, 2016 at 9:09 am

Posted in WebServices

Tagged with ,

Understanding and Creating a WSDL for WebService

leave a comment »

We are creaeting this web service from Top-Down approach. That means we are creating the WSDL first and then create the web service using that.

Understanding a WSDL:
There are main 6 sections in a WSDL.

  • wsdl:definitions
  • wsdl:service
  • wsdl:binding
  • wsdl:portType
  • wsdl:message
  • wsdl:types

wsdl:service

The element defines the ports supported by the web service. For each of the supported protocols, there is one port element. So, the service element is a collection of ports.

Web service clients can learn the following from the service element:

  1. where to access the service,
  2. through which port to access the web service, and
  3. how the communication messages are defined.

The service element includes a documentation element to provide human-readable documentation.

eg :

	<wsdl:service name="GradeThreeMath">
		<wsdl:port name="GradeThreeMathPort" binding="tns:GradeThreeMathBinding">
			<soap:address location="http://localhost:8081/axis2/services/GradeThreeMath" />
		</wsdl:port>
	</wsdl:service>

The binding attributes of port element associate the address of the service with a binding element defined in the web service.

wsdl:binding

The element provides specific details on how a portType operation will actually be transmitted over the wire.

  • The bindings can be made available via multiple transports including HTTP GET, HTTP POST, or SOAP.
  • The bindings provide concrete information on what protocol is being used to transfer portType operations.
  • The bindings provide information where the service is located.
  • For SOAP protocol, the binding is , and the transport is SOAP messages on top of HTTP protocol.
  • You can specify multiple bindings for a single portType.

The binding element has two attributes : name and type attribute. The name attribute defines the name of the binding, and the type attribute points to the port for the binding, in this case the “tns:Hello_PortType” port.

SOAP Binding

WSDL 1.1 includes built-in extensions for SOAP 1.1. It allows you to specify SOAP specific details including SOAP headers, SOAP encoding styles, and the SOAPAction HTTP header. The SOAP extension elements include the following:

  • soap:binding
  • soap:operation
  • soap:body

soap:binding

This element indicates that the binding will be made available via SOAP. The style attribute indicates the overall style of the SOAP message format. A style value of rpc specifies an RPC format. Normally we are using document in this case.

The transport attribute indicates the transport of the SOAP messages. The value http://schemas.xmlsoap.org/soap/http indicates the SOAP HTTP transport, whereas http://schemas.xmlsoap.org/soap/smtp indicates the SOAP SMTP transport.

soap:operation

This element indicates the binding of a specific operation to a specific SOAP implementation. The soapAction attribute specifies that the SOAPAction HTTP header be used for identifying the service.

soap:body

This element enables you to specify the details of the input and output messages. Here, the body element specifies the SOAP encoding style and the namespace URN associated with the specified service. We are using the literal as use to include XML as is.

eg :

	<wsdl:binding name="GradeThreeMathBinding" type="tns:GradeThreeMathPortType">

		<!-- Use document style and not rpc -->
		<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />

		<!-- Use 'literal' to include OTA XML as-is -->
		<wsdl:operation name="Addition">
			<soap:operation soapAction="Addition" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="Devision">
			<soap:operation soapAction="Devision" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

	</wsdl:binding>

wsdl:portType

The element combines multiple message elements to form a complete one-way or round-trip operation.

For example, a can combine one request and one response message into a single request/response operation. This is most commonly used in SOAP services. A portType can define multiple operations.

eg :

	<wsdl:portType name="GradeThreeMathPortType">

		<wsdl:operation name="Addition">
			<wsdl:input message="tns:AdditionRequest" />
			<wsdl:output message="tns:AdditionResponse" />
		</wsdl:operation>

		<wsdl:operation name="Devision">
			<wsdl:input message="tns:DevisionRequest" />
			<wsdl:output message="tns:DevisionResponse" />
		</wsdl:operation>

	</wsdl:portType>

The portType element defines two operations, called Addition and Devision.

The Addition operation consists of a single input message AdditionRequest and an output message AdditionResponse.

Patterns of Operation

WSDL supports four basic patterns of operation:

One-way
The service receives a message. The operation therefore has a single input element.

Request-response
The service receives a message and sends a response. The operation therefore has one input element, followed by one output element. To encapsulate errors, an optional fault element can also be specified.

Solicit-response
The service sends a message and receives a response. The operation therefore has one output element, followed by one input element. To encapsulate errors, an optional fault element can also be specified.

Notification
The service sends a message. The operation therefore has a single outputelement.

wsdl:message

The element describes the data being exchanged between the web service providers and the consumers.

Each Web Service has two messages: input and output.

The input describes the parameters for the web service and the output describes the return data from the web service.

Each message contains zero or more parameters, one for each parameter of the web service function.

Each parameter associates with a concrete type defined in the container element.

eg : In Our Service, there are four messages as follows.

	<wsdl:message name="AdditionRequest">
		<wsdl:part name="AdditionRQ" element="tns:AdditionRequest" />
	</wsdl:message>
	
	<wsdl:message name="AdditionResponse">
		<wsdl:part name="AdditionRS" element="tns:AdditionResponse" />
	</wsdl:message>

	<wsdl:message name="DevisionRequest">
		<wsdl:part name="DevisionRQ" element="tns:DevisionRequest" />
	</wsdl:message>
	
	<wsdl:message name="DevisionResponse">
		<wsdl:part name="DevisionRS" element="tns:DevisionResponse" />
	</wsdl:message>

wsdl:types

A web service needs to define its inputs and outputs and how they are mapped into and out of the services. WSDL element takes care of defining the data types that are used by the web service. Types are XML documents, or document parts.

– The types element describes all the data types used between the client and the server.
– WSDL is not tied exclusively to a specific typing system.
– WSDL uses the W3C XML Schema specification as its default choice to define data types.
– If the service uses only XML Schema built-in simple types, such as strings and integers, then types element is not required.
– WSDL allows the types to be defined in separate elements so that the types are reusable with multiple web services.

Types can be mentioned within the WSDL as follows

<types>
   <schema targetNamespace="http://example.com/stockquote.xsd" xmlns="http://www.w3.org/2000/10/XMLSchema">
			
      <element name="TradePrice">
         <complexType>
            <all>
               <element name="price" type="float"/>
            </all>
         </complexType>
      </element>
		
   </schema>
</types>

or can be imported via a XSD as follows.

	<wsdl:types>
		<xs:schema>
			<xs:import namespace="http://tempuri.org/" schemaLocation="xsd/Math.xsd" />
		</xs:schema>
	</wsdl:types>

 

wsdl:definitions

The element must be the root element of all WSDL documents. It defines the name of the web service.

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
 xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://tempuri.org/" name="GradeThreeMathService">

From the above example, we can conclude that definitions:

– is a container of all the other elements.
– specifies the name of the service. eg : MathService.
– specifies multiple namespaces such as targetNamespace attribute which is a convention of XML Schema that enables the WSDL document to refer to itself, default namespace that all elements without a namespace prefix, (such as message or portType), are therefore assumed to be a part of this and many custome namespaces.

NOTE: The namespace specification does not require the document to be present at the given location. The important point is that you specify a value that is unique, different from all other namespaces that are defined.

Based on the above information we have created a WSDL (GradeThreeMath.wsdl) as follows using the below mentioned XSD (Math.xsd).

GradeThreeMath.wsdl :

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tempuri.org/" name="GradeThreeMathService">

	<wsdl:types>
		<xs:schema>
			<xs:import namespace="http://tempuri.org/" schemaLocation="xsd/Math.xsd" />
		</xs:schema>
	</wsdl:types>




	<!-- Define request and response messages -->
	
	<wsdl:message name="AdditionRequest">
		<wsdl:part name="AdditionRQ" element="tns:AdditionRequest" />
	</wsdl:message>
	
	<wsdl:message name="AdditionResponse">
		<wsdl:part name="AdditionRS" element="tns:AdditionResponse" />
	</wsdl:message>

	<wsdl:message name="DevisionRequest">
		<wsdl:part name="DevisionRQ" element="tns:DevisionRequest" />
	</wsdl:message>
	
	<wsdl:message name="DevisionResponse">
		<wsdl:part name="DevisionRS" element="tns:DevisionResponse" />
	</wsdl:message>




	<!-- Define operation and reference messages -->

	<wsdl:portType name="GradeThreeMathPortType">

		<wsdl:operation name="Addition">
			<wsdl:input message="tns:AdditionRequest" />
			<wsdl:output message="tns:AdditionResponse" />
		</wsdl:operation>

		<wsdl:operation name="Devision">
			<wsdl:input message="tns:DevisionRequest" />
			<wsdl:output message="tns:DevisionResponse" />
		</wsdl:operation>

	</wsdl:portType>


	<!-- Define SOAP binding -->

	<wsdl:binding name="GradeThreeMathBinding" type="tns:GradeThreeMathPortType">

		<!-- Use document style and not rpc -->
		<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />

		<!-- Use 'literal' to include OTA XML as-is -->
		<wsdl:operation name="Addition">
			<soap:operation soapAction="Addition" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

		<wsdl:operation name="Devision">
			<soap:operation soapAction="Devision" style="document" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>

	</wsdl:binding>

	<wsdl:service name="GradeThreeMath">
		<wsdl:port name="GradeThreeMathPort" binding="tns:GradeThreeMathBinding">
			<soap:address location="http://localhost:8081/axis2/services/GradeThreeMath" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

Math.xsd :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://tempuri.org/" targetNamespace="http://tempuri.org/" elementFormDefault="qualified" version="1.0" id="MathRQ">
	
	<xs:element name="AdditionRequest">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="operand1" type="xs:int" />
				<xs:element name="operand2" type="xs:int" />
				<!-- <xs:element name="decimalPlaces" type="xs:int" /> -->
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="AdditionResponse">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="result" type="xs:double" />
				<xs:element name="remarks" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="DevisionRequest">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="operand1" type="xs:int" />
				<xs:element name="operand2" type="xs:int" />
				<!-- <xs:element name="decimalPlaces" type="xs:int" /> -->
			</xs:sequence>
		</xs:complexType>
	</xs:element>

	<xs:element name="DevisionResponse">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="result" type="xs:double" />
				<xs:element name="remarks" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	
</xs:schema>

Written by Namal Fernando

October 25, 2016 at 7:57 am

Posted in WebServices

Tagged with , ,

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!