martes, 17 de septiembre de 2013

Analyzing Threads

Hi there,

So I could take the time to write something about how to analyze threads right?

Thing is there is are tons of data around this out there, so instead of that I'll only share this post from dZone I've find:

http://architects.dzone.com/articles/how-analyze-java-thread-dumps

It is actually brilliant not only gives you context but it shows you how to read the dumps and gives you a hint on some patters.

The other thing I've got to share is a profiling tool I've been checking out (didn't know it existed) and it quite nice:

http://www.yourkit.com/

The tool allows you to hock to a VM and check what's going on in regards to CPU usage, thread count, memory (and it generations) etc.
It's really quite descriptive but it comes with a catch, you have to pay for it. Then again it has a trial period.

Well that's all folks!!!

martes, 16 de julio de 2013

GIT rever to a particular commit


That stupid little silly  thing we always forget how to do, right?

So how, HOW many times did you find your self having to do such thing due to, lets say, a less than brilliant coworker eh?

In my case, usually the less than brilliant coworker is me!!! 
And I always forget how to do this, so this is how


 git reset --hard d7517455bb118142bf12d08628a780d5372da045


It doesn't get easier than this.

In a near future (that is when I have time) I plan to post a summary of all this GIT stuff.

Hope it helps guys and girls....
Who am I laying to we know no girls read this :P

martes, 21 de mayo de 2013

The Secret language of WSDL - The entry point

Hi there, if you like me has to deal with a lot of service definition you'll be wise if you read how that WSDL actually works.

If you, like me, are to lazy to actually learn all that you may want to read this post.

So  this post series (at least I hope to create a series out of this) will try to sum up the main thing one should know about how to read from a WSDL.

We will start by the main thing, at least for me, the ENTRY POINT.

So what to I mean by Entry Point.
For me it's just the Java class that will actually allow you to access all the operations defined in the service definition.

Where is this defined in a WSDL, easy just look for this tag:

<wsdl:service name="UserServiceImplService">
    <wsdl:port binding="tns:UserServiceImplServiceSoapBinding" name="UserServiceImplPort">
        <soap:address location="https://fake.service.provider.com/Some-path-dev/services/admin/UserService"/>
    </wsdl:port>
</wsdl:service>
 


Now this portion of the WSDL will basically describe the entry point of some service.

Once you run your wsdl2java tool to generate the Java code,  what class should I look for?

Well for starters I'll go with: UserServiceImplService
Which is the value of the name parameter in the wsdl:node service.

This class will allow you to access the proxy class (which is no other than the port), this proxy class will expose all the operations that are defined to be accessible for this service.

How do I find this port/proxy class?
Well..... try looking for: UserServiceImplPort
Which is no other than the value of the name parameter in the node: wsdl:port


This has worked out for me in the past I hope it helps you!

BTW do thank my good friend the Master of CXF Gustavo

Cheers

martes, 14 de mayo de 2013

Multipart message with jersey

Hi there, so I'm really happy to see that there is people at least accessing this blog.
Honestly I was intented only for me but if it helps others BRILLIANT, one thing though: DO COMMENT.

Most useful data is usually find in the comments.

Now to today's topic: multi part messages.
In short multipart is a message that could be compose by more than one content type.
All this is part of the HTTP specification and in different RFC of it the guys creating the specification had defined different multiparts.

Today I'll show you how to handle two of them, which are more or less the same, with Jersey Client.



MULTIPART-MIXED
In this example I'll be sending a form with part of the message as XML and the other is the content of a file encoded in BASE64.

            // Asume that this method will just return you a builder 
        WebResource.Builder builder = getBuilder(user, password, url, null);

        // Add headers
        builder = builder.type(MultiPartMediaTypes.MULTIPART_MIXED);

        MultiPart multiPart = new MultiPart();

        // XML Document

         String xmlContent = getXMLContent();
         multiPart = multiPart.bodyPart(new BodyPart(xmlContent, MediaType.APPLICATION_XML_TYPE));

        // File content
        BodyPart bodyPart = new BodyPart();
        multiPart = multiPart.bodyPart(bodyPart);


        InputStream fileInputStream = getFileInputStream();
        bodyPart.setEntity(getFileContentInBase64(fileInputStream));

        ClientResponse clientResponse = builder.post(ClientResponse.class, multiPart);




MULTIPART-FORMDATA
For this example I'll be just sending some plaint text fields in the form and then the content of a file.

            // Asume that this method will just return you a builder 
        WebResource.Builder builder = getBuilder(user, password, url, null);

        // Add headers
        builder = builder.type(MediaType.MULTIPART_FORM_DATA);

        FormDataMultiPart form = new FormDataMultiPart();
        form.field("document[file_name]", fileName);
        form.field("document[description]", fileDescription);
        form.bodyPart(new StreamDataBodyPart("document[attached_file]", fileInputStream));

        ClientResponse clientResponse = builder.post(ClientResponse.class, form);




As you can see there is not much difference but the content-type header.
The other big difference is that the multipart-formdata does has the shape of a form.
This is you set a name of a property in the form and it's value.

But in multipart-mixed you just add body parts that can be what ever you want.
Of corse under the hod the serialized message looks quite different. In the mixed message you'll see some separators and the fact that the file is encoded, this is plain text but encoded.

Any ways this may save you some time.

Hope you like it.      

martes, 16 de abril de 2013

Gson and how nice it is

Tired of Jackson are you?
Do you want to serialize a POJO and not have to add all those annotations?
Just want to try a different way to deal with Json?

The I have an answer for you: Google Gson.

Gson is the Google library to deal with JSON and serialization, but in a easier way.
For starters you don't need to use annotations for the properties or the methods, it uses (out of the box) the POJO convention and introspection to serialize the class.

So for example if you just want to serialize some Java class you have you'll have to do the following:
Gson gson = new GsonBuilder().create();
gson.toJson(anObject);

And to de-serialize it you'll do:
Gson gson = new GsonBuilder().create();
gson.fromJson(aJsonString, anObject.getClass());


Now one thing I just keep forgetting how to do (hence the name of the blog) is how to deserialize a plain old collections let say a List of Strings, and here is how:
 
import java.lang.reflect.Type;
Type type = new TypeToken<List<String>>() { }.getType();
List<String> aList =  new Gson().fromJson(jsonData, type);

As you can see it is quite simple to use, and of course this is just a really really really small port of what you can do with it, but it was a nice excuse for me to post how to deserialize a collection :P.  

For more information just check the Gson site here :

https://sites.google.com/site/gson/gson-user-guide

Any way I hope you can use it.
 

Dealing with GZIP Content

So .... Long time no see eh?
Oh well there are many reasons for that but truth is "you don't care about that".
Let's jump then into this post topic, which is ZIPED content.

As you may or may not know web servers can send you the content you are requesting in a compress way. Main reason for this is, of course, cause you save bandwidth.
Problem being, you have to deal with that content.
As it turns out you usually come across this when you are creating some sort of HTTP client to consume something and when you try to print the data you can no read it (this is what happened to me).

Easiest way to know for sure that the server is doing this is, checking the following HTTP header:

Content-Encoding: gzip 

This tells you that the "content" has been "gziped", quite obvious rigtht?

Now lets talk about code, how do I deal with this.
Well if you are a smart chap and you are using Jersey Client, this is how:


import com.sun.jersey.api.client.filter.GZIPContentEncodingFilter;

List<ClientFilter> clientFilters = new ArrayList<ClientFilter>();
clientFilters.add(new GZIPContentEncodingFilter(false));


if (clientFilters != null) {
  for (ClientFilter clientFilter : clientFilters) {
      client.addFilter(clientFilter);
  }
}


As you can imagine the client is the Jersey Client, and basically what this code does is to add a filter.
For Jersey filters are plugin of code that in the end knows how to do certain stuff with the payload you send.
In this case this filter "GZIPContentEncodingFilter" knows how to add the HTTP header I showed you before BUT ALSO, it knows how to zip the payload you're seeing.

Now that I've showed you how to handle this with Jersey I've also thought it'll be a good idea to show you just how to decode and encode any content in the same way, and this is how:

Decode: 
InputStream io = (InputStream) someInputStreamYouHad;
String payload = new String(IOUtils.toByteArray(new GZIPInputStream(io)));


Encode:

OutputStream os = new ByteArrayOutputStream();
OutputStream realOs = new GZIPOutputStream(os);
      
realOs.write(payload.getBytes());

realOs.close();
payload = os.toString();


And that's it, hope it helps you/me.

viernes, 11 de enero de 2013

Maven Archetype from Existing Project

Hi there,

So this weeks I've been having to mess around with Maven, in particular with archetypes.
Being new at this particular aspect of the Maven world I started where almost anyone will the Maven documentation page.

Any way.......

Thing is, this helped me a lot to understand the structure of a Maven archetype project but it didn't get me to create an archetype in an easy way.
In the end I found this blog.

As it turns out, the easiest way to create a Maven archetype is using an existing maven project.
Which makes senses.
As with anything in life if you like to create a template for something it's easier to create that something an then extract the template.

So this is the case, and the way to achieve this is:

mvn archetype:create-from-project

This will create a maven archetype from a project and will leave this new archetype project in:

target/generated-sources/archetype/

Now that you have you're base archetype you can start to tune it, and when you're ready to go just do:

mvn install

This will install the archetype in your local m2 repo.

Finally for those of you who don't know how to use this archetype one it's in place, this is the way:

mvn archetype:generate -DarchetypeGroupId={group.id.defined.in.the.archetype} -DarchetypeArtifactId={artifact-id-defined-in-the-archetype}



And that's it, have fun


martes, 8 de enero de 2013

Out of Memory with Maven & MAVEN_OPTS

Have you ever got an error like this:


java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
at java.util.jar.JarVerifier.update(JarVerifier.java:194)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:322)
at java.util.jar.JarFile.getInputStream(JarFile.java:388)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:69


Well this is the easy solution, increase the memory of the JVM.

BUT HOW IF I'M BUILDING WITH MAVEN???

God question, and the answer is:

export MAVEN_OPTS=-Xmx512m


Of course this will follow the same rules as for the vm arguments for the JVM, so please modify your memory limit accordingly.

Also this export command, for you Mac users, will only set the env var for the current terminal not for all the future ones.

Hope you find this useful I know I'll keep coming back to this post just to remember this.