Monday, September 08, 2008

How can I make sure Terracotta is wired into my application?

Per the title of this blog post, I'm going to show you how to make sure that Terracotta is enabled in your application.
One of the funny things about Terracotta is that applications typically don't know it's there.  This is normally a really good thing - it means you can use the 'ole binary search debug trick.  (Just remove Terracotta from one half of the application one debug session at a time to quickly narrow down the issue).
But of course, in production, we want to make doubly sure Terracotta is actually running - yeah sure the application might be happy as a clam churning out txns, but we want to make sure those txns are getting replicated for high availability, right? :)
Fortunately, finding out if Terracotta is wired into your app is really easy.  We can make use of the fact that most of the regular Java classes are instrumented by default, so we can use reflection to interrogate one of them.  I chose String since I think it's a well known class.  Here's the code:

public static final boolean isTCEnabled()
{
try {
String.class.getMethod("__tc_decompress");
return true;
} catch (Exception e) {
return false;
}
}
That's it! We just test to see if a special Terracotta method is present, and if so, we know Terracotta is wired in. All you have to do is put that into your application startup somewhere, and complain loudly if the method returns false :)

1 comment:

Unknown said...

Make it possible for guild wars 2 keyAnet complete what they really want, just what exactly if you dont get some special item or whatever, take care of it! Expensive in addition to beloved GW2 Content management system! I'm sure we brazilians will not be countless ingame, but will there be any Diablo III goldopportunity we can a little bit of really like and have the opportunity to have some of the ingame gives?