Thursday, April 17, 2014

JACOCO as a coverage tool for Unit testing, Integration testing and Functional testing
This article explains how to generate coverage reports for unit testing,integration testing and functional testing using JACOCO.Instrumentation
Ability to alter byte code through classloaders for the purpose of gathering data such as profiling, coverage etc. is known as instrumentation. Instrumentation was introduced in 1.5.JACOCO
JACOCO is one of the most popular JAVA Coverage tool used in the industry. Using on the fly instrumentation, JACOCO will modify the byte code by inserting its hooks to record the coverage information making the integration with build tools, server simple. Through this feature, application code remains clean. Thanks to the instrumentation feature added in JDK 1.5!!!Technologies used

Java 1.7

Jboss AS 7.2

Maven

Spring 4.0.3

JACOCO 0.6.4

Overview of Classes

UserDao.java A simple DAO to save,read users from DB

UserService.java A simple service with an handle to DAOUserController.java A simple REST interface

Now running the maven build with install and package will generate a nice coverage report as shown below for all the unit tests.

While this looks simple, it gets little tricky when we have a bunch of tests cases to verify the functionality of an application. In real world one may have their application deployed on a server and functional test suites will run against the server. JACOCO agent can be added as an JVM option in the application server and on the fly instrumentation will kick in enabling coverage information. All the coverage information will be collected in exec file and reports can be generated from this file.

set "JAVA_OPTS=%JAVA_OPTS% -javaagent:c:\apps\jacoco\lib\jacocoagent.jar=destfile=c:\apps\reports\jacoco.exec,append=true,output=file"

Start the server and execute the test suites or execute some manual requests and stop the server once all the testing is done. JACOCO will dump the coverage information on JVM exit.
Below is the coverage report generated

Friday, February 7, 2014

Venkat Nandanavanam
What is HORNETQ?
HORNETQ is the default messaging system in JBOSS application server. It is an open source project used to build clustered, and asynchronous messaging system. More information can be found here. In my old blog posts I discussed about basic concepts such as how to create users to consume and produce messages in the messgaing system, and REST interface support . Please find tutorials below

While the above tutorial discuss about how to use HORNETQ through REST and JMS API, this tutorial will focus on how to integrate the highly scalable messaging system with Spring JMS module and the advantages one gets by doing so.
Technologies used in this tutorial are given below

Spring 3.2

JBOSS eap-6.1

Maven

HORNETQ 2.3.1

NOTE:Before getting starting create users,queues as shown in this post.
Spring-JMS has many advantages

Avoid repetitive code

Easy maintenance

Exception translation etc.

This tutorial can be mainly divided into three parts. First we will try to produce a message to the queue. Second we will consume it through spring JMS template. Third we will have a simple POJO to consume the message.A simple POJO to consume a message!!! How cool is that!!! Thanks to spring-jms module.
Below is the controller which will be used to produce messages. For simplicity sake we will create a TextMessage and push it to the queue

ConnectionFactory consists of various connection configuration parameters as defined by the admin user. A client i.e. SpringJMSTemplate uses it to create connections to the destinations. ConnectionFactories and destinations i.e. Queues/Topics are defined in standalone-full.xml . Below is the code snippet from standalone-full.xml

trueNIO2jms.queue.DLQjms.queue.ExpiryQueue010485760BLOCK10

Kindly note that ConnectionFactories are exposed as JNDI objects.We can use JNDI-lookups to get the ConenctionFactory
in our spring application context. Below is the code snippet of applicationContext.xml

While everything is good so far!!! Dont you see a big disadvantage with this approach? Consuming message through an explicit GET request. Well!!! JMS is all about asynchronous message processing. So we would like to react to a message in the queue rather than doing a polling. In my previous blog posts you can notice that I used message driven beans to consume the message asynchronously.
Message Driven Beans was included in EJB 2 specification.Basically MDBs are EJBs that process messages asynchronously. With EJB 3 specification, we can just implement MessageListener interface for asynchronous processing. Spring-JMS module has its own implementation of MessageDriven beans by which we can convert a simple POJO to process the messages asynchronously.
Below is the code snippet of our POJO

Happy learning!!!!
If you have any questions please do not hesitate to ask it in comments section. I will be very happy to answer them.
Dear Readers, kindly like this post on facebook or follow me on Google+

Sunday, November 10, 2013

Sometimes things which looks very minor end up taking more time than expected. My transition to Intelij idea from eclipse was pretty smooth with an exception to debugging. In the good old days I was using tomcat as server and eclipse as IDE. Just with a tomcat plugin for eclipse I was able to debug my code.
Expecting the same and reluctant to use the ant auto generated by intelij idea I wrote my own ant build and wrote some code.After the sweet message "build successful", I deployed it on jboss. Tried to debug the code by placing a break point which to my surprise never reached. After many long hours of experimenting I added debug attribute and turned it on in javac ant task and bingo!!! debug point reached. Hope this saves your time!!! Happy debugging!!! :)

Sunday, September 8, 2013

Venkat Nandanavanam
Note: For mobile users, kindly switch to web version on your mobile.
Security is a very basic requirement which every developer has to keep in mind while designing or developing an application. I would like to take this opportunity to quote a line from the one of the book I read "Every system can be broken, given enough time and money. Let
me say that again, every system can be broken". This very quote itself inspired me to blog a series of tutorials regarding security, cryptography and what else is the best place to start other than SHA-256. In the near future I will try to blog more tutorials on digital signatures, how to verify digital signatures, digital certificates based authentication, Keys .....
SHA-2
For a given input SHA-256 will generate a hash value through which the input can be validated and guaranteed that its contents are not tampered. Sender can generate a hash value and share it with the receiver through which receiver can validate the file. If the contents of the file is changed the hash value will also change.(This is what we basically see on many download sites). Below is an example using Message Digest.

Friday, September 6, 2013

Venkat NandanavanamIntroduction
HORNETQ is the defualt messaging system provided by JBOSS Application Server. In one of my previous tutorial I covered how to configure, produce and consume Messages through JMS API using HORNETQ as provider. You can find the tutorial @ HERE.
NOTE: This tutorial uses queue named 'restInterfaceQueue'. To know how to create this queue please take a look into the above tutorial.
This tutorial will focus on creating and consuming messages from HORNETQ using REST API.Why would one ever need to produce and consume messages through REST?
Well!!! I would say to support CROSS LANGUAGE CLIENTS.... and also to provide web based API for messaging. With these capabilities one no longer needs producer and consumer to be through JMS API. So anyone who understands HTTP can produce and consume messages. This one big advantage of REST interface. HORNETQ provides a very good REST interface. Some of the features of REST interface provided by HORNETQ are given below

Avoid posting of duplicate messages

Mix and match JMS and REST producers and consumers

Acknowledgement and auto acknowledgement

Technologies Used

JAVA

J2EE

RESTEasy

HORNETQ

HORNETQ REST Interface

Maven

Coplete list of features and more information about REST interface and HORNETQ can be found @here. Lets get started!!!!
Boot strapping HORNETQ with REST
To use HORNETQ REST interface in our application we first need to bootstrap it. This can be done by adding the below listener in web.xml

org.hornetq.rest.integration.RestMessagingBootstrapListener

REST Interface Configuration

We should add some configuration information for REST interface. This can be done in an xml file under WEB-INF/classes. The name of the file should be mentioned in web.xml using rest.messaging.config.file context param

hornetq-rest.xml

0falsetruetruetopic-push-storequeue-push-store1013000

The entries in above XML file are explained below Server-in-vm-id : This is to differentiate different HORNETQ servers running in
same VM. We can default it to zeroUse-link-headers: To tell HORNETQ how to publish the links. By default it publishes
using cutoms headers. This tutorial uses the default behaviour so we can mark it as
falsedefault-durable-send: Used to identify if the posted message needs to persisted in
DB.We will mark it as false. This default behaviour can be overriddden by sending
durable custom header as truedups-ok Should duplicate messages be allowed?client-window-size Used to identify buffering capabilities. By specifying it as zero we are saying we don't want any buffering to happen for delivery of messages.
Now lets take a look at our complete web.xml

Now build and deploy the application. We are now ready to consume and produce messages using HORNETQ REST interface.
DEMO
Start your server after deploying the war file

Now post our first message to the queue named "restInterfaceQueue" using REST WS call in OrderSrvc.java. It uses regular JMS API to post the message to queue. url to post the xml message using restclient firefox plugin 'http://localhost:8080/jms-rest-examples/resteasy/hornetq/postOrder'

XML posted

Mumbai300f238OR908765

The message counter for the queue should be 1 now as shown in admin console of our JBOSS server.
Now let us consume this message using REST interface.
First we will do a GET request to 'http://localhost:8080/jms-rest-examples/resteasy/hornetq/queues/jms.queue.restInterfaceQueue'. To understand how this URL is formed lets break it into pieces

/queues- All REST interface URLS should have queues followed by the original queue name itself which is jms.queue.restInterfaceQueue

As you may have already guessed HORNETQ will retrun us some bunch of custom headers with the GET request. Custom headers returned are shown below

Notice the msg-pull-consumer header returned. We will use that URL to create a service so that we can pull the message from the queue. To create a service do a empty post to the URL as mentioned by msg-pull-consumer header

Below are the custom headers returned from above request.

Notice the 'msg-consume-next' header value returned.Do a POST request to this URL to consume/pull the message from the queue.

below is the consumed message

This network roundtrip is just done once and to consume next message we need not issue the GET request again and start the cycle. Instead we can use 'msg-consume-next' header returned to consume next message and do the polling.If by any chance you miss the URL you can issue a get again to get url values as headers.
Now let us look at Produce an message using REST interface
Do a get request to 'http://localhost:8080/jms-rest-examples/resteasy/hornetq/postOrder' and use 'msg-create' header value and do a post

Notice the headers returned from the above POST request.'msg-create-next: http://localhost:8080/jms-rest-examples/resteasy/hornetq/queues/jms.queue.restInterfaceQueue/create'. Do a post to the URL returned as mentioned by 'msg-create-next' to create a message in the queue

This will create a new message in the queue.
This brings us to the end of the tutorial which covers how to post message and consume message using REST interface.
I was really excited to see producers and consumers via REST interface in JMS queue. If your application needs to support cross platform messaging I would strongly suggest to use REST interface.
Hope this tutorial helps you to understand REST interface. If you have any questions please do not hesitate to ask it in comments section. I will be very happy to answer them.
Dear Readers, kindly like us on facebook or follow me on Google+

Saturday, August 31, 2013

java.lang.RuntimeException: HornetQException[errorType=NOT_CONNECTED message=HQ119007: Cannot connect to server(s). Tried with all available servers.]
One of the exception I ran into while experimenting with HORNETQ REST INTERFACE. Hope this saves your time!!!!
This is caused because of not mentioning hornetq and netty dependencies in MANIFEST.MF file. Just add "Dependencies: org.hornetq, org.jboss.netty" in MANIFEST.MF in your WAR file or in your pom.xml.

As part of series of REST tutorials, I am blogging this simple HorneQ
JMS tutorial. HornetQ is the default meessaging system in JBOSS
application server.
A detailed documentation about HornetQ is available @ here.Lets get started with the tutorial
Technologies used

HornetQ

JBOSS eap 6.1

JAVA 7

RESTEasy

CDI

Maven

Now lets see what configuration we require before we start our coding. Before we start any configuration a basic understanding of JBOSS server is required. JBOSS server directory structure will look this

Different startup configurations are available in JBOSS server. To have the HORNETQ integrated and started with JBOSS AS one should use standalone-full.xml. For example we will start the server using this command
./standalone.sh --server-config=standalone-full.xml.
We will add the queues needed for our tutorial in standalone-full.xml. There are other ways too to declare the queues/topics which is out of scope of this post. I am gonna develop a order processing system which can receive a order which is a XML file and it will put the order in a queue which will be consumed by other consumers. Lets name that queue as "ticketOrderQueue". Here is how we declare in standalone-full.xml

To connect to this queues we will use JNDI connection factories. By default JBOSS gives us couple of connection factories like "InvmConnectionFactory" and "RemoteConnectionFactory" which will use netty connector. Code snippet of these factories is below

If our message producer or consumer is in same JAVA VM as HornetQ one should use "java:/ConnectionFactory" and if it is remote client consuming or producing messaging to JMS destinations one should use "RemoteConnectionFactory". Noteworthy about the contexts they use as shown in above snippet. For example, "RemoteConenctionFactory" uses "java:jboss/exported" context.
Once this is done, we will define users for JBOSS application server as administrator and for HORNETQ.
Use add-user utility to add application users.
Note: I am using UBUNTU as my dev env. However for windows it should be the same process with bat files
First let us add admin user. Please find the details in below screenshot

Now let us add some application users for HORNETQ. Please find the details below

Now start the server and we will add some security to our HORNETQ.
start server

type in and enter the credentials you configured for admin as mentioned above and select JMS destination from left hand menu. You should now be able to see this view with the queues which we defined

To add some security to our HORNETQ lets go to profile view by clicking on profile link on top right side on admin view and select Messaging/destination/view and select security settings and add the role permissions as shown below

This should bring us to end of configuration section. Now lets start coding our producer which is a RESTFUL web service and receives a XML order and pushes it to a queue.
OrderSrvc.java

In the above web service I used @Resource annotation which is nothing but through CDI. Inorder to make CDI work one should have a beans.xml defined under WEB-INF folder. This xml will tell JBOSS to enable CDI.Noteworthy that this xml can be empty. I will add tutorials specific to CDI in the near future.
For now,please find the xml below