Tutorial: Using MosKito with CDI

Today I would like to show you how to integrate MosKito into your CDI Application.
I will show you the required dependencies and modifications to some files you need to do before you can use MosKito with CDI.

Preparation

For this tutorial

  • Glassfish version 3.x and
  • Gradle at least in version 1.0

is required.

The example application provides a RESTful webservice, which takes a document (as plain text) and calculates some fancy statistics for it.

Let’s start with the

Dependencies

The MosKito stuff can be retrieved from this repository url http://nexus.anotheria.net/nexus/content/repositories/releases/ .

For MosKito we need moskito-core  and moskito-cdi  at least in version 2.1.4.
The following build-script snippet includes everything we need at the moment.

As you may have noticed I excluded everything from group blowfish. It’s required to do this, otherwise we had some unresolvable dependencies.

RESTful service

Now let us define the RESTful services and the stub for the underlying service. (Notice: I omitted the imports to have less code)

At least we need two data transfer objects, one for the complete statistics and one for a single line

Ok, that was the easy part. But now the big question is, how to enable MosKito for the application. Because MosKito uses interceptors for a CDI-based application we need to activate them via the

beans.xml

The activation can be done very easily by adding the required interceptors to the beans.xml file in the interceptors section.
MosKito provides basically these interceptors:

  • net.anotheria.moskito.integration.cdi.CallInterceptor
    This interceptor produces several runtime statistics.
  • net.anotheria.moskito.integration.cdi.CountInterceptor
    This interceptor is, as the name says, for counting method calls
  • net.anotheria.moskito.integration.cdi.DaoCallInterceptor
    This interceptor extends the CallInterceptor and sets the category to “DAO” by default. It’s used when the @Monitor annotation is qualified with MonitoringCategorySelector.DAO
  • net.anotheria.moskito.integration.cdi.ServiceCallInterceptor
    This interceptor extends the CallInterceptor and sets the category to “SERVICE” by default. It’s used when the @Monitor annotation is qualified with MonitoringCategorySelector.SERVICE. This is default, if you didn’t specify anything.
  • net.anotheria.moskito.integration.cdi.WebCallInterceptor
    This interceptor extends the CallInterceptor and sets the category to “WEB” by default. It’s used when the @Monitor annotation is qualified with MonitoringCategorySelector.WEB

Combining the things together will end in this extend beans.xml

Ok, now let’s put the things together.

Using MosKito

But, what would we like to monitor? ‘uploadAndQueueDocument’ seems to be a good candidate for runtime monitoring the basic stuff like counting the calls and process time per request, whereas ‘getCountingForWord’ and ‘getWordStatistics’ looks like good candidates for counting.

Both types of monitoring can be enabled by using the specific annotations

  • @Monitor for runtime monitoring
  • @Count for simple counting

As you may have noticed I used MonitoringCategorySelector.WEB for @Monitor, because it’s the monitoring from a webservice. 

And that’s it. The configuration and other things could be found here.

The complete example is avaiable on github.

 

 
2 Kudos
Don't
move!

Leave a Reply

Your email address will not be published. Required fields are marked *