Monday, September 12, 2011

JAX-WS Dispatch Clent

In this article i am going to discuss about how to create JAX-WS dispatch client As we know JAX-WS is an embeded feature of Java 6 which beats its counter part JAX-RPC because its faster than its counter part and adds flexibility to user to construct his own document literal style of Payload for advanced developer and also leverages to use any kind of parser and marshalling/unmarshalling api. But the current parser which JAX-WS uses is STAX which no doubt is the fasted parser till date.But for marshalling/unmarshalling the embedded JAX-WS uses JAXB if you like you can use other third party Data binding tool for your reference please read the link below from there you can decide which one to use :)
Data Binding performance
Now lets come to the real thing.Here goes my Dispatch client.
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
import org.services.encrypt.EncryptionException;
import org.services.encrypt.ShaEncrypter;

/**
*
* @author ajay.biswal
*/
public class ClientTest {

public static void main(String arg[]) throws SOAPException, EncryptionException {
QName serviceName = new QName("http://org.services.ajaywebservices/", "TestService");
QName portName = new QName("http://org.services.ajaywebservices/", "TestPort");
String endpointAddress =
"http://localhost:8080/myapps/Test";
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
Dispatch dispatch =
service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
Map> headers = new HashMap>();
ShaEncrypter encryptor = new ShaEncrypter();
String userEnc = encryptor.encrypt("ajaybiswal");
System.out.println("*******userEnc****" + userEnc);
String passEnc = encryptor.encrypt("ajaybiswal");
System.out.println("*******passEnc****" + passEnc);
String data = new String(userEnc + ":" + passEnc);
headers.put("Authorization", Collections.singletonList("Basic " + new String(data)));
BindingProvider bp = (BindingProvider) dispatch;
Map rc = bp.getRequestContext();
rc.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage request = factory.createMessage();
SOAPPart part = request.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPBody body = env.getBody();
SOAPElement operation = body.addChildElement("hasWorkflowgroup", "ns1",
"http://org.services.ajaywebservices/");
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("ajaybiswal");
request.saveChanges();
SOAPMessage response = dispatch.invoke(request);
QName responseName = new QName("http://org.services.ajaywebservices/", "hasWorkflowgroupResponse");
SOAPBodyElement bodyElement = (SOAPBodyElement) response.getSOAPBody().getChildElements(responseName).next();
String message = bodyElement.getFirstChild().getTextContent();
System.out.println("****REsponse******" + message);
}
}

Friday, May 13, 2011

A very simple way to send attachement to a webservices

As i know the SOA has a standard way i'e MTOM for sending and recieving attachement which i have described in one of my previous blog.What i found from this a some sort of inter-operatability issue as well as performance issue as i have to unnessarily encode the content(which one can think that the data is protected but the truth is its not bcos decoding is very easy) which even increases the size of the attachment also uncessary xml syntaxes are incorporated which even increases the size of data to be send while invoking the webservice.So i thought what if i want to have my own style of sending and recieving attachemts.Then what i can do is simply get the bytes of the attachment.using java.io.* api and send it as a parameter to the operation.But this solution can be used if you only want to send/recieve attachments.If you want to send more complex parameters then my suggestion is to use MTOM.

Thursday, October 14, 2010

G1(Garbage First) vs CMS Garbage collector

G1(Garbage First) vs CMS Garbage collector.

CMS garbage collector is garbage collector mechanism present in Java 6.In this garbage collector mechanism.The entire heap world is divided into two regions.
1.Young Generation heap
2.Old Generation heap(perm gen)
The young generation heap is again divided into 2 parts
1.Eden space 2.Survivor space
CMS(Concurrent mark and sweep algorithm) basically concentrates on the Young generation heap.
as in this space short lived objects are created which needs to be frequently garbage collected.
All objects in this space which are fit be to garbage collected are marked and concurrently sweeped and all those objects which may survive for longer time are either shifter to survivor space or Old generation heap example (Session objects,static variables).
As the entire heap is divided into 2 sets hence searching for dead object and memory defragmentation is little hectic for JVM as we know the heap size can be GB's.

So to ease the stress on JVM the Sun hotspot team in JAVA 7 has introduced a new mechanism for garbage Collection that is called G1(Garbage First).This approach is same as that Java 6 the only difference is the entire heap space is divided into a region of 1MB size except the Perm gen memory as they will be rarely touched the same is the case in Java 6.So here in G1 collector we can undestand from our basic instict that as heap is divided into regions of smaller size so marking and claming of dead Objects becomes easier.As JVM can run the gc in number of steps as a result the hold of JVM on memory will be for shorter time for each region unlike CMS in java 6 where JVM has to deal with memory for longer time.And we know that any secondary memory operations is time consuming.Hence we can see from here Java 7 will be faster than Java 6.But let us wait for it and test practically .

Friday, August 27, 2010

SOAP UI

I am writing this blog after a long time.As today i am feeling very happy :) so had inward feeling to pen down some of my learnings which will help IT Professionals especially QA guys.As we know that Web technology is moving with a great speed with the innovation of Ajax,Ruby,Webservices,Hibernate,Spring and many RAD tools we are now in web 2.0.By the way we are moving towards web 3.0 infact we are already in web 3.0 and still moving further.
So today i am going to discuss about a tool called SOAP UI.It is basically a tool to test webservices without writing a single piece of code.So imagine if you are a tester or even developer who has to test webservices all the time.And for developer to test his own webservice,then this tool is of great help.The good thing about this tool is you can test live Webservices also(I mean thirdy party)without writing a single piece of code.Only you need to know the url of your wsdl file.And lo you are ready to test.For your reference i am giving the url for this tool.

http://www.soapui.org/

Happy testing.Have a nice day.:)

Thursday, August 27, 2009

Webservice Client for sending attachments using apace axis

Today i am going to share for my experience how to send binary attachments to a webservice using apache axis.The following piece of code will help you to do the same.

String endpoint = "http://localhost:9000/ecm/yourwebservice.jws";
javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(
new FileDataSource(
"C:\\WINDOWS\\TEMP\\user\\data\\metadata.xml"));
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
QName qnameAttachment = new QName("uploadUser", "DataHandler");
call.addParameter("source", qnameAttachment, ParameterMode.IN);
call.registerTypeMapping(dataHandler.getClass(), // Add serializer for attachment.
qnameAttachment, JAFDataHandlerSerializerFactory.class,
JAFDataHandlerDeserializerFactory.class);
call.setOperationName("uploadUser");
call.invoke(new Object[] { dataHandler});

The above code is the client.


The webservice will be something like this

public void uploadUser(DataHandler handler){
InputStream in = handler.getInputStream();
//in is the bytes send by the client to the webservice

}

Thats all how we can send attachments to webservice.

Happy Coding

Wednesday, July 29, 2009

Escape Analysis

Well today i have come for blogging after great frustration as i was whole day in my office to get the LDAP url for testing my application by i didnt got till now as now it is evening and i raised the request in the morning.Well let me now come to the key feature of the article.Today i am going to write something on Escape Analysis.It a new terminology used by Sun Microsytem
in their current JVM version 6.What this Escape Analysis does is it allocates object memory if necessary and also converts Heap Based memory to Stack based as per when required.As a result it improves application performance in terms of memory creation as we know that memory creation is time consuming .If you haven't started using Java 6 please start using as it has lots of performance improvement as comapred to previous version.

Wednesday, May 20, 2009

XML Vs JSON

All these days we were learing about XML as only the data transfer technology and the whole world were crazy for learning XML and institutes were earning bucks in the name of XML as it was highly demanded in the era of web technology.But after the incubation of Java Script object notation(JSON) XML has lost its glossiness to some extent in the field of web application.Because the truth behind this is JSON is much more faster than XML as it occupies less memory than its counterpart XML.So imagine if u have to transfer data and with XML it comes 1 MB .By using JSON you can transfer the same data with just 1/2 MB of size.So start using JSON for any server and client data transfer.Also we have java based JSON library a good news for java developer