Three years with Java Simon (3)

I should finish this series before it should be called “Four years with Java Simon” – but we still have some time. I’ll show you what possibilities callbacks brought to the Simon, but first I’d like to deal with our Webnode site.

It was three years back (January 13th, 2009) when I posted on our Webnode site that we need some better web for presentation than our project site on Google Code. But then we found we can’t post bunch of HTML files (Javadoc) on Webnode – and with mime-types SVN props we can do that on Google Code… oh, how quickly things change.

Webnode site could be good if Java Simon gained some bigger momentum on our side – more committers and contributors, people writing blogs or tutorials or success stories (or not so success stories too if they can help :-)). But this did not happen and I felt as rather annoying obligation to update this site. Especially because the edit functionality is on a separate URL – editing a Wikipedia page or a blog post on WordPress is always just one click away – but not so on Webnode.

Three years later I decided to redirect javasimon.org on our Google+ page because it is so much easy to update, posting even very short posts is not inappropriate (which would be on a blog) and it’s just so much closer to my way of living on the Internet right now. I’ll go through javasimon.webnode.com and it will soon be a matter of past.

Last time I discussed some changes from version 1 to version 2. And to preserve the little from Webnode site that has any “historical” value I hereby copy one blog post covering just these differences:

Major changes in the core of the Java Simon v2
2009-01-28 14:30
While there are some important extensions to the Java Simon (JMX, Spring integration, etc.) there are a few important changes in the core part of the API that are probably even more important. If you’ve already managed to use Java Simon 1 I strongly suggest that you use version 2 even in its alpha stages. The thing is:

  • If your project is finished or close to finish (month or two) stay with version 1.
  • If your project continues and you’re just experimenting with Simon, definitely use version 2! There is v2-alpha1 which is basically rework of the v1 after a few changes in Stopwatch. If there is newer alpha out (check Featured Downloads on the right on our project page) take that one of course, because it contains more features from v2.
  • Version 2 is planned to be out during March or April 2009, which is really soon.

Now what are the changes and why we made them?

  • Important change happened in the Stopwatch. While in the v1 it contained various start/stop methods that took care of multi-threaded environment now it has only one start method and this start doesn’t return this anymore but it returns new Split object instead. You have to take care of the Split object, you have to take care of your multi-threading, you have to call stop method on the Split. This makes our code safer as the Stopwatch doesn’t contain internal maps that were prone to memore-leaks if client forgot to stop some split. Thanks to Erik van Oosten and his great Java Simon evaluation.
  • Based on the same post we changed sample methods so that they return Java Bean objects now instead of the field.
  • While in v1 you had to use SimonManager now you can use non-static Manager implementation directly. SimonManager still stays your favourite convenient class full of static methods, of course. 😉 This allows to have multiple separeated Simon hierarchies which may be handy in Java EE environment.
  • To provide some extensibility for the API we introduced Callback interface. This allows to hook onto various events and process these events in any way you want – to log them, send JMX notifications, whatever.

There are more features to come with version 2 and I covered only those in the core part of the API. Stay tuned, download, use, test, let us know what you think. 🙂

Now let’s take a look at those Callbacks. Based on good-old Observer pattern, Callback is a listener that performs some actions on various events. First question was where the Callback should be registered – and we decided that Manager will hold its Callbacks. We didn’t want to scatter Callbacks across various Simons because typical usage would lead to a situation where many Simons call (and point to) the same Callback. We rather decided we will centralize Callback management on a Manager (that is per Manager of course) and bring some way how to filter events based on Simon name for instance.

Simple example of Simon is in our CallbackExample:

    SimonManager.callback().addCallback(new CallbackSkeleton() {
           public void onStopwatchStart(Split split) {
               System.out.println("\nStopwatch " + split.getStopwatch().getName() + " has just been started.");
           }

           public void onStopwatchStop(Split split) {
               System.out.println("Stopwatch " + split.getStopwatch().getName()
                   + " has just been stopped (" + SimonUtils.presentNanoTime(split.runningFor()) + ").");
           }
       });

       Stopwatch sw = SimonManager.getStopwatch(SimonUtils.generateName());
       sw.start().stop();

When you work with Simon (last two lines) you don’t care about Callbacks – they will be called. Their configuration can be based on some configuration and they should do whatever you want to hook on various Simon events.

BTW: If you use Java Simon 3.1 method onStopwatchStop is still called stopwatchStop. This is quite serious flaw and poor choice of method name on my part (and I’m terribly sorry for that). While this method doesn’t show it clearly, there was another method – clear (now onManagerClear). This method is called – as you may guess from the new name – when clear method on the manager is called. Let me explain composite callbacks first to show you the whole problem…

To add more callbacks to the manager is all right but if you want to filter Simons (by name, for instance) that fire an event on a Callback you actually need to do it in the event itself. Or wrap the Callback into another one – that is exactly what FilterCallback idea is all about. Another thing is that you may need to call various callbacks for the same filter – to group them – and that is what composite callback does – holds more callbacks (children) and relays the event to all of them. There is no interface CompositeCallback – instead all these methods are on Callback already, but they are not implemented in the CallbackSkeleton for instance (used in the example above). There is one implementation called CompositeFilterCallback – and you probably can guess what it does. It can hold more callbacks and call them when the common configured filter is passed. See CallbackFilteringExample for simple use case.

Now guess how people tried to remove callbacks from composite callback. It’s just a collection of callbacks after all, right? Ah, method “clear” must do exactly what I need here. But it didn’t. And if you didn’t implement this event method (which is not very common, but JmxRegisterCallback is nice example where it is very handy) it simply did nothing. There was method to remove one callback, but not all of them (this one was in SimonUtils). This is all finally fixed with version 3.2 – all names are much better and removeAllCallbacks is in the Callback interface.

JMX is nice example why we needed callbacks just as much as we wanted to offer them to our users. There are two ways how to access Simons via JMX – you can use single point MX bean, or let Simon instantiate MX beans per Simon. The latter however requires some actions when Simon is created, destroyed or the whole manager is cleared. I mentioned JmxRegisterCallback already – check how it’s done there. Now the Simon manager knows about Callback mechanism – but it doesn’t have to know about JMX – or anything else you want to drive by these events.

Split introduction and Callbacks are two very important changes that happened in version 2 – and these things are now well proven and will probably last (though some names can change as will happen in version 3.2 :-)). Next time I’ll try to wrap up the rest of the story.

Advertisements

About virgo47
Java Developer by profession in the first place. Gamer and amateur musician. And father too. Naive believer in brighter future. Step by step.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s