<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Rambling Comments</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/" />
    <link rel="self" type="application/atom+xml" href="http://www.lenholgate.com/blog/atom.xml" />
    <id>tag:www.lenholgate.com,2010-12-10:/blog//12</id>
    <updated>2010-12-26T06:51:20Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 5.12</generator>

<entry>
    <title>3 days with JMock</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/12/3-days-with-jmock.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.641</id>

    <published>2005-12-09T11:32:16Z</published>
    <updated>2010-12-26T06:51:20Z</updated>

    <summary>As I mentioned last week, I&apos;m currently doing some Java work with an investment banking client. This week I added JMock to their toolset. JMock is a dynamic mock object generation tool that works with JUnit to allow you to...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Java" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Testing" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>As I mentioned <a href="http://www.lenholgate.com/archives/000586.html">last week</a>, I'm currently doing some Java work with an investment banking client. This week I added <a href="http://www.jmock.org/">JMock</a> to their toolset.</p>

JMock is a dynamic mock object generation tool that works with JUnit to allow you to create mock objects without writing code. Since most of my TDD work has been in C++, most of the mock objects that I've used in the past have been hand coded. There are <a href="http://www.lenholgate.com/archives/000409.html">similar tools for C++</a> but, due to the lack of reflection in C++, they can be a bit painful to use. I actually like writing my mocks by hand in C++. I find that writing the mocks really helps you to think about the design of your code from the perspective of the user of the code; since you have to actually implement the interfaces that you want your clients to use you're forced to think about their design a little more than you might otherwise. With JMock you don't need to write the mocks yourself. JMock uses reflection to allow it to mock interfaces and, if you use the CGLib extensions, a mixture of reflection and code generation to allow it to mock concrete classes. It's a useful tool and, most of the time, it works well.]]>
        <![CDATA[<p>I'm currently writing some Java CORBA code and last week I spent a couple of days getting the environment set up and doing some TDD with JUnit. I wrote a few hand coded mocks because I'd already spent a little while getting everything moving and I wanted to actually deliver some working code. The first thing that I did this week was to get JMock installed and to see how it worked in comparison to my hand coded mocks. I decided to continue writing new code using TDD and the first thing that I needed to mock was an interface. This went pretty smoothly and allowing the tool to generate the mock for me saved me somewhere between 10 and 30 minutes I expect. Once the first test was passing I got to the real reason that I had wanted to try out a dynamic mocking system; I needed to mock a concrete class.</p>

<p>The code I'm working on is a client to some CORBA objects and as such it works in terms of object proxys that are created from IDL specifications by the CORBA IDL compiler. Whilst there are interfaces in use here the interface that you end up with for your object extends several standard CORBA interfaces that are fairly "chunky"; there's a lot of functionality to mock by hand and most of it we're not really interested in. My original install of JMock couldn't mock these objects; it required the installation of the CGLib extension jar and a version of CGLib and then a version of ASM. Once the extras were installed I switched to using the code generation JMock test base class and was successfully able to mock my CORBA objects.</p>

<p>By this point I was pretty happy. Even allowing for the time it took me to track down the dependency on ASM that wasn't mentioned that clearly, I was still ahead on time compared to trying to mock even one of the CORBA objects by hand. Once I'd mocked my objects I then mocked a CORBA ORB, ran my test and implemented the required functionality. Without JMock I would probably have spent an hour or so mocking up the CORBA objects and then designed my way around having to mock the ORB; I only needed a single function from the ORB, <code>object_to_string()</code>, and I expect I would have created a very specialist interface with just that method, mocked that and eventually created a thin wrapper that implemented the interface and passed the call on to a real CORBA ORB. Whilst I like the fact that by being able to directly mock the complicated ORB object I didn't need to introduce the wrapper class I'm in two minds about whether the design is better or worse for passing the actual ORB into my new object. I like to restrict what code can do, so I like narrow interfaces. If I pass an interface into something and the something only uses a fraction of the interface then I'm often tempted to create a more specialised interface for the something in question to use. I find that this clearly communicates what the something is expecting to be able to do with the object that it's given. Being given a wide interface means that during maintenance it's often very easy to make the wrong decision and use one of the methods on the interface that the original designer never really intended for you to use; using interfaces that are limited to what you actually should be doing protects you from that... In this instance the only service we want from the ORB is its ability to convert an object reference to a string, we definitely do not want the class to be able to jiggle around with the more complex settings of the ORB, all of which are accessible though the same interface that the nice safe object conversion operation is available from. So at this point I'm happy with the speed gain from being able to automatically mock but a little less happy with the fact that automatically mocking may be leading to slightly less than ideal designs...</p>

<p>Whilst getting to this point I'd been getting to grips with the whole idea of setting expectations for my mocks. As I've <a href="http://www.lenholgate.com/archives/000421.html">mentioned before</a>, my hand coded C++ mocks use simple text logs to record their interactions with other objects. Earlier in the year I had a <a href="http://www.lenholgate.com/archives/000423.html">long discussion</a> with <a href="http://weblogs.asp.net/rosherove/">Roy Osherove</a> about mocks that use logs verses mocks with expectations and in the end I started to see the light. Expectations are a very powerful feature and they're something that I've been adding to some of my own C++ mocks. The problem with simple logs is that you're not always interested in the same things. One usage of the mock may require that you log all the parameters to be sure that the interaction is correct whereas another may simply require that you log the fact that the call happened, you don't care what was passed. As your mocks are used they get more complex to handle the various different situations. Mocks with expectations are more complex from the start but if you're using JMock you don't need to care about that complexity because the objects are created for you. Of course I could write a generic C++ expectation solution and have all of my hand crafted C++ mocks use it but, well, I have work to do... I find the setting of expectations somewhat messy, but that's probably just because I'm used to the streamline nature of checking interaction using logs. Most of my mocks can be checked with a single line of code whereas setting up a series of complex interaction can take several rather noisy lines. I'm sure I'll get used to it.</p>

<p>The next issue was how to set up an expectation for a method that called back into the object under test. One of the mocks that I was using was an "object store" and it had a <code>load()</code> method that tool an interface to a sink. When <code>load()</code> was called the store needed to iterate over its objects and pass each one to the supplied sink interface. Thankfully this was easy to achieve in JMock by simply creating a custom <code>Stub</code> object that knew what to do. This stub was slightly more complex that a hand crafted mock would have been, mainly due to the standard boilerplate that you had to provide and the fact that the stub has a single method gets called for any method signature on the object that's being mocked and this single method is generic which means you need to extract the arguments you need yourself... It's not hard, it's well documented but it is a little more code than you'd need to write if you were writing your own mock. Of course it's only a little more code if that's the only method that your hand crafted mock needs to implement. In a real world scenario the mock will have several methods that do not need any custom stub implementation at all, it's only the special cases that need to be hand coded and the fact that the hand coding is slightly more complex is made up for the fact that you don't need to hand code everything.</p>

<p>Unfortunately JMock fell at the final fences. </p>

<p>I'm quite comfortable writing unit tests for code that interacts with multiple threads. JMock <a href="http://www.jmock.org/threads.html">doesn't play well</a> with multi-threaded tests. The JMock documentation contains a rather pithy excuse for not dealing with threading; <i>"The root of the problem is trying to use mock objects for integration testing."</i> This is, quite frankly, bullshit. There are plenty of situations where you may need to test an object using multiple-threads and simply saying that "those kinds of tests aren't unit tests" is simply ignoring the real world uses of the tool. Lets take, for example, an object that acts as a <a href="http://www.ccs.neu.edu/home/kenb/csg112/synchronize.html">producer</a> of things. Our unit test will be acting as the consumer and, as is quite common with this patter, the producer may block if there are no things available for the consumer to consume. As soon as you have a test where the object under test may block, for any reason, you need to be able to spin up another thread so that you can continue to control the object during the test. To suggest that this isn't unit testing is somewhat naive.</p>

<p>One of JMock's problems with threading is that when a call occurs on a mock object the expectations are checked and if the call isn't expected the mock throws an exception that is caught by the test case that you're in. The exception fails the test. The problem is that if the mock is being accessed on another thread then the exception simply terminates that thread and the test knows nothing about the failure. I've dealt with this kind of issue before, most notably when testing COM objects in various different apartments. One trick is to provide a way for the user to create threads that can participate in the test and which can handle these exceptions and report them to the thread which is running the test. That approach would deal with my immediate problem where I want to create a second test thread but fails to deal with testing inherently multi-threaded code, such as the <a href="http://www.lenholgate.com/archives/000381.html">threaded version</a> of my <a href="http://www.lenholgate.com/archives/000306.html">timer queue.</a>. To solve the threading issue in all situations involves tracking the thread on which you're created and then only throwing an exception if you're on that thread. If you're not then you need to make a note of the failure and throw the exception the next time you're accessed from the creation thread. Due to Java's object lifetime rules you may need to add a call to some form of 'validation' function to the end of your test to make sure that the mock checks for other thread calls before the test ends. So, it's not an especially hard problem and I expect that if I end up using JMock  a lot, and I can't find someone who already has a solution, I'll solve it and make the code available.</p>

<p>Another problem I came across was mocking a serializable object. The mock isn't serializable even if the object that you're mocking is. I can understand why this might not work but again it's something that's probably reasonably easy to fix. As it was I simply wrote a hand coded mock for this situation.</p>

<p>My 3 days with JMock were a success. The tool is very useful and very powerful and works well. Before the end of day 1 I'd already replaced the hand coded mocks that I put together last week with JMock mocks. By the end of the week the only hand coded mocks in use were ones to get around JMock limitations on serialization and threading. Once the code was complete I did a short presentation on JUnit and JMock to the rest of the team and they're all keen to start using these new tools and some of them were even quite interested in this Test Driven Development thing...</p>]]>
    </content>
</entry>

<entry>
    <title>A change of technologies</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/12/a-change-of-technologies.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.640</id>

    <published>2005-12-03T08:45:36Z</published>
    <updated>2010-12-26T06:50:41Z</updated>

    <summary>I started back with an investment banking client this week. I&apos;m working with the team that brought us &quot;the refactoring project&quot; and another small team, and integrating the trade entry system with some &quot;xll&quot; excel addins and some back end...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Java" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I started back with an investment banking client this week. I'm working with the team that brought us "<a href="http://www.lenholgate.com/archives/000220.html">the refactoring project</a>" and another small team, and integrating the trade entry system with some "xll" excel addins and some back end server software. Pretty standard fare for investment banking work...</p>

<p>I've worked with this client quite a few times over the years and it's interesting to go back and find that they're still using the systems that I helped them to put in place back in 2001. It's nice to see all of the C++ Excel addin code still in regular use and hear that the system has been easy to extend and has remained robust and reliable. The backend stuff is a mix of CORBA servers in Java and C++ that integrate with other banking systems. The suite of software has continued to evolve but it has done so in the spirit of the original design.</p>

The trade entry system is another matter, once again I return to find that the tests don't run and have been neglected and that the system has moved back towards its initial "big ball of mud" design...]]>
        <![CDATA[<p>This time around I'll be helping to get some new people up to speed on the <a href="http://support.microsoft.com/kb/178474/">xll</a> addin framework whilst adding some new functionality to the addins. As before, since I <i>can</i> work on all of the layers, I will do, taking the C++ changes in the addins through the CORBA layer to the C++ unix server. Whilst I'm here I'll also be helping them move some of the unix C++ functionality across to Java. </p>

<p>Yesterday I began some Java work for the first time in quite a while. Whilst getting my environment set up I downloaded and installed JUnit and set off on my first TDD journey with Java. Believe it or not this is the first time I've actually used JUnit to write tests. I've run plenty of JUnit tests in the past when building and installing Java things but, since the last time I did any Java work was back in 2001 with this client, this is the first time that my relatively recent conversion to unit testing and TDD has overlapped with using Java.</p>

<p>The day went well and JUnit was pretty easy to work with. I'm sure I didn't use much of its functionality, but I think the tests were fractionally faster to write than when using my home-grown C++ test framework (reflection means that I don't need to explicitly call the test methods from somewhere as JUnit calls all methods that begin with 'test', this saves me typing a function call per test into my C++ test framework's per test class 'test all' function.).</p>

<p>When I spend some time with the client next week I hope to be able to integrate one of the dynamic mocking systems into the tests. Right now I'm crafting my mocks by hand as I do in C++, I'd like to compare productivity between dynamic mocks and my hand crafted ones.</p>

<p>When the guys on the team saw the JUnit's green bar they were interested in learning more. They'd heard of, but not used it. I expect next week I'll be doing the full TDD evangalism bit in the hope of gaining a few new converts.</p>

<p>It's been almost a year since I last spent any time down on <a href="http://support.microsoft.com/kb/178474/">Cannon Street</a> and it seems that there's a <i>lot</i> of building development work going on in the City of London right now. Loads of the shops that I used to frequent are now boarded up in that way that usually signifies that the whole block in which they reside is about to be 'repurposed'. I just know it's going to take me an age to find a decent coffee shop...</p>]]>
    </content>
</entry>

<entry>
    <title>More Reprints - CORBA, C++ and Java</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/08/more-reprints---corba-c-and-java.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.549</id>

    <published>2005-08-23T09:58:57Z</published>
    <updated>2010-12-24T05:54:03Z</updated>

    <summary>I&apos;ve just finished posting some more reprints from back in 2001 when I was working on CORBA systems with C++ and Java. The articles mostly compare CORBA to COM and show why providing a reference counted server object lifetime management...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="CORBA" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Java" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Reprints" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I've just finished posting some more reprints from back in 2001 when I was working on CORBA systems with C++ and Java.</p>

<p>The articles mostly compare CORBA to COM and show why providing a reference counted server object lifetime management system is harder than it appears.</p>

<p><a href="http://www.lenholgate.com/archives/000343.html">1 - CORBA - Reference Counting</a>. Adding reference counting to CORBA objects isn't as easy as it first seems.</p>

<p><a href="http://www.lenholgate.com/archives/000467.html">2 - CORBA - More Reference Counting</a>. Although we managed to develop a working solution in the first CORBA reference counting article the results were ugly and fragile. In this article we attempt to clean things up a little and, in doing so, get intimate with the Portable Object Adapter and its Servant Managers. </p>

<p><a href="http://www.lenholgate.com/archives/000468.html">3 - CORBA - Reference Counting Issues</a>. At the end of the second article we have developed a self contained reference counting implementation that appears to work. Unfortunately, it's still far from reliable as CORBA doesn't provide the level of support for reference counting that's built into COM. In this article we discuss the problem and the various CORBA methods for controlling server object lifetime. </p>

<p><a href="http://www.lenholgate.com/archives/000469.html">4 - CORBA - Enumeration</a>. CORBA provides sequences as a way of returning collections of items from an method call. The problem with just using unbounded sequences is that the client has no control over how many items it receives as a result of the call. COM gets around this problem using the IEnum style interfaces that allow a client to control how it accesses the items in a collection. </p>

<p><a href="http://www.lenholgate.com/archives/000475.html">5 - CORBA - Iteration</a>. A CORBA style method of enumeration can be seen in the iteration interfaces on the CORBA Naming Service. Given the code we've already written for the enumeration interface we can easily implement an iteration interface as well as (or, more likely, instead of). </p>

<p><a href="http://www.lenholgate.com/archives/000476.html">6 - CORBA - The Evictor Pattern</a>. Since CORBA doesn't really support reliable reference counting implementations we'll compare one of the recommended methods of servant life-time management with our reference counted iteration interface. </p>

<p><a href="http://www.lenholgate.com/archives/000477.html">7 - CORBA - Keep Alive</a>. One way of making a reference counted implementation more robust is to run the keep-alive protocol yourself. We demonstrate this option here. </p>

<p><a href="http://www.lenholgate.com/archives/000478.html">8 - The CORBA Evictor Pattern in Java</a>. In part 6 we solved the problem for C++ servers, here we do the same for Java servers.</p>

Enjoy...]]>
        
    </content>
</entry>

<entry>
    <title>Java caches in the middle tier</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2001/07/java-caches-in-the-middle-tier.html" />
    <id>tag:www.socketframework.com,2001:/blog//12.138</id>

    <published>2001-07-26T19:18:27Z</published>
    <updated>2010-12-28T13:33:43Z</updated>

    <summary>A common way to improve the performance of Java code is to cache objects rather than repeatedly create and destroy them. This is especially true when you&apos;re writing middle tier servers that service client requests and return results objects. Implementing a flexible caching scheme in Java is relatively easy, but there are a few things to watch for.</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Java" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Reprints" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Source Code" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Way back" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        A common way to improve the performance of Java code is to cache objects rather than repeatedly create and destroy them. This is especially true when you&apos;re writing middle tier servers that service client requests and return results objects. Implementing a flexible caching scheme in Java is relatively easy, but there are a few things to watch for.
        <![CDATA[<p><b>The problem</b><br />
Creating objects in the middle tier that live for only the length of a single client request can be inefficient. When those objects are the results from  client requests, and when serveral clients may make requests for the same data, it often makes sense to cache the results. Caching in Java can be implemented relatively easily. After all, the simplest cache is just a map which can store objects based on keys. Sometimes this simple solution is good enough, but usually you want more control over the lifetime of the objects that live in the cache.</p>

<p>This article uses a simple CORBA server to demonstrate increasingly complex cache implementations. This server implements the following interface which allows a client to pass a string and receive a <code>String[][]</code> in return. As we go along we'll improve the caches that it uses and refactor the code as our design evolves.</p>


<pre class="brush: cpp gutter: false">typedef sequence&lt;string&gt; StringSeq;
typedef sequence&lt;StringSeq&gt; StringSeqSeq;

interface CacheTest
{
   StringSeqSeq DoSelect(in string sql);
}
</pre>

<p>In <a href="http://www.lenholgate.com/zips/JCache1.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache1.zip']);">JCache1.zip</a> we develop the client/server framework and use a HashMap as a simple cache. We simulate a time-intensive operation to fetch data based on the user's request and store the results of the queries in the cache for later reuse. The following code from CacheTestImple.java does the work:</p>


<pre class="brush: cpp gutter: false">   public String[][] DoSelect(String sql)
   {
      String[][] results = findInCache(sql);

      if (results == null)
      {
         results = getData(sql);
   
         addToCache(sql, results);
      }

      return results;
   }

   private String[][] getData(String sql)
   { 
      System.out.println("getData(\"" + sql + "\")");

      // replace this with a call to a database if you want...

      pause(5000);

      String[][] results = new String[2][2];

      results[0][0] = "Timestamp";
      results[0][1] = "Sql";
      results[1][0] = new Date().toString();
      results[1][1] = sql;
     
      return results;
   }
</pre>

<p>Obviously this is a simplified example, but the concept is valid. If data being retrieved by a request takes some time to obtain, and multiple clients are likely to request it, then it makes sense to cache it. What's more, due to the way that Java often garbage collects, you may find that a server that caches every result actually has a smaller memory footprint than one that doesn't!</p>

<p><b>Building the example code</b><br />
Install Ant, the build system used by JacORB, and type 'ant' in the src directory. If you don't want to install Ant then it's fairly easy to take the build.xml files and convert them into normal makefiles.</p>

<p><b>Running the sample</b><br />
The scripts directory contains a script to run the server and a script to run the client. The client/server pair use the CORBA naming service to find each other. Run up the JacORB name server (you can use the ns.bat file and the supplied orb.properties and jacorb.properties if you want to). Then run up the server and run the client. By passing different strings on the command line you can make the server return different 'result objects' when it needs to create a new result object there is a delay, when it doesn't because it found the results in the cache the server returns quicker.</p>

<p><b>A performance tweak for the key to the map</b><br />
Although caching the results in a simple HashMap improves the performance of our server there's a small performance tweak that's probably worth making at this stage. Strings aren't the best choice for keys in a HashMap as they calculate their hash value every time the hashCode() member function is called. A simple CacheKey can wrap a String and cache the hash... In <a href="http://www.lenholgate.com/zips/JCache2.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache2.zip']);">JCache2.zip</a> we add a CacheKey class which does just that.</p>

<p><b>Improving on a map</b><br />
A more complicated cache will need to be able to have some control over the entries that it contains. At present our cache will simply grow and grow as new entries are added to it. However, this may be appropriate for some servers and as we've decided that the caching strategy required  by one server may not be the same as that required by another server, or even by another cache within the same server, we'll create an interface that all caches can implement. We will then be able to plug new caches in with ease as long as they conform to our interface.</p>

<p>Our cache interface can look something like this, and we'll move the CacheKey class inside the interface as it's so closely related. Since the second Cache in Cache.CacheKey is a bit redundant, we'll lose it.</p>


<pre class="brush: cpp gutter: false">public interface Cache
{
   public Object find(String keyString);
   public Object find(Key cacheKey);

   public void add(String keyValue, Object obj);
   public void add(Key cacheKey, Object obj);
   
   public void remove(String keyValue);
   public void remove(Key cacheKey);
   
   public String name();
   
   public int entries();
   public int hits();
   public int misses();
   
   public void flush();

   public class Key implements Comparable
   {
      public Key()
...
</pre>

<p>Next we take our original simple HashMap based cache and implement a SimpleCache which operates in the same way as our "forever growing" cache. We then adjust our server implementation class to use our cache interface and we're done. <a href="http://www.lenholgate.com/zips/JCache3.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache3.zip']);">JCache3.zip</a> hasn't changed how our server caches its results, but it has made it far easier for us to change it in the future.</p>

<p><b>A slightly smarter cache</b><br />
If the server is going to run for long periods of time then a cache that never purges any entries is unlikely to work that well, unless the possible data set that the server can return is very small. A more useful cache might purge entries that haven't been used for some time so that once a result has been requested subsequent requests that occur within a certain time of each other will work from the cache. If the object isn't requested for some time, however, it will be purged to make room for other more commonly requested objects.</p>

<p>To achieve this we need to associate a last used timestamp with each cache entry. We then need some way to store the cache entries in descending order of when they were last used - so we can quickly walk through the entries from least recently used to most recently used. We also need a seperate thread to periodically scan the list to see if the least recently use object in the cache has timed out.</p>

<p>In <a href="http://www.lenholgate.com/zips/JCache4.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache4.zip']);">JCache4.zip</a> we implement a second cache that conforms to our Cache interface. TimeLimitedCache contains a HashMap which we use as the cache and a LinkedList which we use to store the cache elements in least recently used order. We add an ObjectRef class to the Cache interface. The ObjectRef links the object that we're caching with a timestamp and its key. This makes it easy for the entries to be removed when they time out. Finally we have a thread to periodically check the cache for objects that have timed out. The whole thing is controlled by two member variables, scanEveryMillis - how often the cleanup thread looks for timeouts - and entryTimeoutMillis - how long an entry can stay in the cache after the last client touched it. Both of these variables are set to very low values so that we can see the cache in action.</p>

<p>We've changed the client too, it now runs automatically. First it populates the server cache with 10 items and then it requests just one item repeatedly to show that a regularly requested item will remain in the cache after items that aren't being used are removed. The result objects that the server returns have been adjusted so that they are larger and the server run script now runs the Java VM with verbose garbage collection so that we can see the effect of the cache on the server's memory usage.</p>

<p><b>Taking out the trash</b><br />
If you watch the latest server carefully you'll notice that it doesn't garbage collect the objects that have been removed from the cache until an arbitrary time after they've been removed. Between the time that the objects are removed from the cache and the time that they are garbage collected the objects are still in memory but completely unusable.</p>

<p>This seems like a bit of a waste. In V1.2 of the Java SDK some classes for working with the garbage collector were added. One of the most useful was the SoftReference class. The SoftReference is a class that can refer to another Object, what makes the SoftReference a little special, however, is that if an object is only reachable through "Soft" references then it is eligible for garbage collection. You can access the object that a SoftReference refers to using the get() method. If the referent has been garbage collected then get() will return null. What's more, a SoftReference can be registered with a ReferenceQueue and at some point after the SoftReference's referent is garbage collected the SoftReference will be placed in the ReferenceQueue.</p>

<p>What this means is that you can use SoftReferences and ReferenceQueues to develop some fairly smart caches. By deriving our ObjectRef from a SoftReference and having the SoftReference hold a "soft" reference to our cache item and the ObjectRef hold an additional normal (hard) reference to the cache item we can release the normal reference when the object times out in the cache but still access the object right up until the moment that the garbage collector decides to collect it.</p>

<p>When the object in the cache times out, the cache calls releaseObject() on the ObjectRef. This causes the only "hard" reference to the object to be removed. The object is now only reachable by the "soft" reference and is therefore eligible to be garbage collected. The ObjectRef is still stored in the cache. If a request for the object comes in and the object hasn't yet been garbage collected then the ObjectRef will be located in the cache. The usage time is updated and get() is called to retrieve the object from the SoftReference. If the object still hasn't been collected then get() returns a reference to it, the hard reference is reattached, the user gets the object from the cache...</p>

<p>If, however, the object has been garbage collected then get() would return null and the cache lookup would have failed. Eventually the ObjectRef will be placed in the ReferenceQueue that it was associated with when it was constructed. By creating another thread we can poll the ReferenceQueue and remove ObjectRefs as they arrive after the object that they referred to has been collected. As we remove the ObjectRef from the queue we ask it to remove itself from the cache and at that point the purged object is no longer reachable via the cache...</p>

<p>It sounds more complicated than it is, go and look at the <a href="http://www.lenholgate.com/zips/JCache5.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache5.zip']);">code</a>...</p>

<p><img src="http://www.lenholgate.com/blog/images/softref1.gif" /></p> 

<p>The ObjectRef holds a normal reference to the cached object so it won't be considered for garbage collection.</p>

<p><img src="http://www.lenholgate.com/blog/images/softref2.gif" /></p> 

<p>The hard reference is removed as the object is purged from the cache. Now the cached object can only be reached via a soft reference. It will now be considered for garbage collection.</p>

<p><img src="http://www.lenholgate.com/blog/images/softref3.gif" /></p> 

<p>The cached object is garbage collected. The ObjectRef is placed on the ReferenceQueue that was associated with the SoftReference that it derives from. When the ObjectRef is removed from the queue it can be removed from the cache.</p>

<p>Now when we run the client in <a href="/zips/JCache5.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache5.zip']);">JCache5.zip</a> you'll see similar behaviour to before, but you'll also see each entry garbage collected. When there's only one entry left it will be allowed to timeout but then accessed before its garbage collected (if my timing code is right :)) and finally allowed to timeout and be garbage collected.</p>

<p><b>A time to live</b><br />
Some results objects may only be valid for a certain length of time. Perhaps you have data fed regularly into your database, say, once every hour. It's OK to cache client requests for up to an hour, but no longer. It doesn't matter if clients are requesting the same data every minute or two, after an hour you need to query the database again. The data has a maximum time to live and we should be able to create a cache which respects that time to live.</p>

<p>This new kind of cache has quite a lot in common with our TimeLimitedCache. It needs to hold an ObjectRef to the object and store a timestamp and it needs to periodically remove expired entries. If we separate this code out from our existing TimeLimitedCache we can produce a common base class for both the newly named EntryUsageTimeoutCache and our new MaxTimeToLivecache. The common base class will keep the TimeLimitedCache name. </p>

<p>EntryUsageTimeoutCache is the TimeLimitedCache from our previous example refactored so that the new TimeLimitedCache contains code that is common to both types of time limited caches. It provides the periodic cleanup thread and an interface that other time limited caches must adhere to for the cleanup thread to be able to work with them.</p>

<p>The ObjectRefs used by the two caches aren't actually as similar as they first seem. One needs a timestamp update whenever the object is accessed and the other needs a timestamp that is set only once, when the object is first added to the cache. To add both timestamps to Cache.ObjectRef seems inappropriate. So we'll move them into classes that derive from Cache.ObjectRef. As it happens, the ObjectRef used by MaxTimeToLiveCache doesnt want the magical object resurrection properties of the SoftReference based Cache.ObjectRef, but the SoftReference stuff is more likely to be used again so we'll leave that as the standard ObjectRef and just cruft up a new simpler one for the MaxTimeToLiveCache.</p>

<p>In <a href="http://www.lenholgate.com/zips/JCache6.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache6.zip']);">JCache6.zip</a> the server has been changed to use the new cache and the client remains the same. Notice how all of the cache entries are purged, even the one that's in regular use.</p>

<p><b>Endless caching strategies</b><br />
At this point we could continue creating new cache strategies until the cows come home... They're easy to use because of the common interface and we can write a cache to suit any server's requirements. The problem is that each time we decide that we need to change how we cache some data we need to modify the program to use the new cache. This is unfortunate and it means that we can't distribute our application with a "pretty good" cache and also ship the definition of the interface that users can use to write their own better caches if they are so inclined. Luckily, thanks to the wonders of reflection doing this is actually quite easy!</p>

<p>In <a href="http://www.lenholgate.com/zips/JCache7.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache7.zip']);">JCache7.zip</a> we add a CacheManager. The CacheManager manages our caches. It allows users of our caches to always work in terms of just the Cache interface. It's a factory for caches which can be configured by merging some properties with the system properties and then requesting a cache by name. The property file stores the mapping between the name that's hard coded in the source code and the cache that actually gets used. The CacheManager is a pluggable factory because it loads the required cache implementations dynamically from their .class files at run-time. It's user extensible because anyone can write a cache that conforms to our Cache interface, put the .class file in their class path and change the property file to refer to it, all without recompiling the application! That's pretty cool! The property file contains entries like this:</p>


<pre class="brush: text gutter: false">com.jetbyte.cache7.CacheManager.MYCACHE.CacheClass=com.jetbyte.cache7.EntryUsageTimeoutCache</pre>


<p>Where "MYCACHE" is the name of the cache passed to the CacheManager's getInstance() method. Usually you'd use the name of the object that contains the cache, such as com.jetbyte7.CacheTestImpl in the example. If the object has a need for more than one cache then it can append something to the end of its class name. If the object wishes to share a cache with another object, it can, as long as the two objects agree to call the cache by the same name. Run the new server and experiment by uncommenting each of the config lines in server.properties and restarting the server.</p>

<p><b>No caches</b><br />
Since we can now easily change the type of cache used, it would be nice to be able to turn off caching completely as conveniently. In <a href="http://www.lenholgate.com/zips/JCache8.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache8.zip']);">JCache8.zip</a> we implement a NullCache. The object conforms to the Cache interface standard but simply doesn't bother to cache any of the objects that are given to it. Now you can change the property file to:</p>


<pre class="brush: text gutter: false">com.jetbyte.cache7.CacheManager.MYCACHE.CacheClass=com.jetbyte.cache7.NullCache</pre>


<p>and caching is turned off! The best thing about this change is that it's possible to allow for the special case of "not caching" without having to have any special logic in the application! Not caching is just another kind of caching.</p>

<p><b>Easy cache configuration</b><br />
Now that our server is using properties that are merged in with the system properties to configure its cache without the need for recompilation we may as well configure the caches themselves via properties. In <a href="http://www.lenholgate.com/zips/JCache9.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache9.zip']);">JCache9.zip</a> we allow the scanEveryMillis and entryTimeoutMillis variables to be configured on a per named cache basis from properties.</p>

<p><b>Download</b><br />
The following source was built using Ant and tested with JacORB 3.21 - the open source Corba Java ORB.</p>

<p><a href="http://www.jacorb.org/">Get JacORB</a></p>

<p><a href="http://www.lenholgate.com/zips/JCache1.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache2.zip']);">JCache1.zip</a> - a simple HashMap cache</p>

<p><a href="http://www.lenholgate.com/zips/JCache2.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache2.zip']);">JCache2.zip</a> - a better key</p>

<p><a href="http://www.lenholgate.com/zips/JCache3.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache3.zip']);">JCache3.zip</a> - use an interface</p>

<p><a href="http://www.lenholgate.com/zips/JCache4.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache4.zip']);">JCache4.zip</a> - purge the least recently used entries</p>

<p><a href="http://www.lenholgate.com/zips/JCache5.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache5.zip']);">JCache5.zip</a> - use SoftReferences</p>

<p><a href="http://www.lenholgate.com/zips/JCache6.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache6.zip']);">JCache6.zip</a> - purge entries after a maximum time</p>

<p><a href="http://www.lenholgate.com/zips/JCache7.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache7.zip']);">JCache7.zip</a> - use a cache factory</p>

<p><a href="http://www.lenholgate.com/zips/JCache8.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache8.zip']);">JCache8.zip</a> - turn off caching with a null cache</p>

<p><a href="http://www.lenholgate.com/zips/JCache9.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JCache9.zip']);">JCache9.zip</a> - configuration from properties</p>]]>
    </content>
</entry>

<entry>
    <title>The CORBA Evictor Pattern in Java</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2001/07/the-corba-evictor-pattern-in-java.html" />
    <id>tag:www.socketframework.com,2001:/blog//12.137</id>

    <published>2001-07-23T00:00:00Z</published>
    <updated>2011-06-23T10:57:24Z</updated>

    <summary>When a CORBA server allows its clients to create and destroy objects one of the recommended ways to handle the object lifetime issues is using the Evictor Pattern. In The Evictor Pattern we solved the problem for C++ servers, here...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="CORBA" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Java" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Reprints" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Source Code" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Way back" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[When a CORBA server allows its clients to create and destroy objects one of the recommended ways to handle the object lifetime issues is using the Evictor Pattern. In <a href="http://www.lenholgate.com/archives/000476.html">The Evictor Pattern</a> we solved the problem for C++ servers, here we do the same for Java servers.]]>
        <![CDATA[<p><b>The problem</b><br />
Due to the way that CORBA deals with object lifetime issues you may find it necessary to have the server control the lifetime of objects created on the server by the client. The kind of objects we're talking about here are the objects that get created for a client when the client calls a method on another object on the server. For example a client may wish to access a collection of objects on a server and to do this may call a method that returns an iterator object. The iterator object has a lifetime that is controlled by the client, the client creates it by requesting it from the server, uses it and eventually destroys it with a call to <code>destroy()</code>. Unfortunately servants on the server can be leaked if the client does not call <code>destroy()</code> and because of this it's best to let the server take control. See the <a href="http://www.google.com/custom?domains=lenholgate.com&amp;q=CORBA+&amp;sitesearch=lenholgate.com&amp;client=pub-6888952347469638&amp;forid=1&amp;ie=ISO-8859-1&amp;oe=ISO-8859-1&amp;safe=active&amp;cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&amp;hl=en">C++ CORBA section</a> for more details of the problems that can occur if you dont let the server manage the lifetime of these objects in some way.</p>

<p>You may be surprised to hear that you have these kind of object lifetime problems with Java. After all, as a Java programmer you're used to objects being garbage collected when they're no longer needed - why dont CORBA servants get cleaned up automatically for you? The problem is that the server needs to hold on to a reference to the servant object until the client doesnt want the object any more. If the client behaves itself then there's no problem, once the client finishes with the object it can tell the server that it no longer requires it and the server can release its last reference and the language run-time can garbage collect the servant object when it wants to. If the client terminates unexpectedly or acts maliciously the the server has no way to manage the references that it is holding on behalf of the clients. One solution to this problem is the evictor pattern.</p>

<p><iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;o=2&amp;p=8&amp;l=as1&amp;asins=0201379279&amp;fc1=000000&amp;=1&amp;lc1=0000ff&amp;bc1=000000&amp;lt1=_blank&amp;IS2=1&amp;f=ifr&amp;bg1=ffffff" width="120" height="280" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<b>The Evictor Pattern</b><br />
For an excellent description of the concept behind the evictor pattern see <a href="http://www.amazon.co.uk/exec/obidos/redirect?path=ASIN/0201379279&amp;link_code=as2&amp;camp=1634&amp;tag=ramcom-21&amp;creative=6738">Advanced CORBA Programming with C++ (APC)</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=ramcom-21&amp;l=as2&amp;o=2&amp;a=0201379279" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />. In a nutshell, the server decides how long your object can live, when the server decides your time is up it simply destroys your object for you and your next call fails... It doesn't matter if you forget to release your objects when you're done, they'll get cleaned up eventually. It doesn't matter if you terminate without letting the server know and, well, it doesn't matter if you have to write heaps of client code to handle exceptional cases of object disappearance...</p>

<p><b>You're out...</b><br />
The simplest evictor works along these lines. The server has finite resources, if those resources are about to be exceeded then the server should garbage collect existing objects that haven't been used for a while to make way for new objects. The easiest implementation is probably something that uses a least recently used algorithm for eviction of "inactive" servants and is implemented in terms of a <code>ServantLocator</code>. See the <a href="http://www.google.com/custom?domains=lenholgate.com&amp;q=CORBA+&amp;sitesearch=lenholgate.com&amp;client=pub-6888952347469638&amp;forid=1&amp;ie=ISO-8859-1&amp;oe=ISO-8859-1&amp;safe=active&amp;cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&amp;hl=en">C++ CORBA section</a> for details of the server that this example is based on and to compare the Java implementation with the equivalent C++ implementation.</p>

<p><b>An evicting ServantLocator</b><br />
A servant locator is called by the POA that it's associated with to dispatch method calls to the appropriate servant. By associating a servant locator with a POA we can place code that we've written directly in the call sequence for every method dispatch call to any servant that uses that POA. We are now responsible for mapping the CORBA Object ID to the servant that is providing the implementation for that object. Since we manage the mapping we also manage the lifetime of the servants.</p>

<p>Every time we create a servant that is to be managed by our servant locator we must notify the servant locator of the new object so that the object can be added to the servant locator's mapping table. When the servant locator adds the object to the mapping table it must also generate an Object ID and from the Object ID we can create an object reference that refers to our servant so that we can hand it out to our clients.</p>

<p>When we wish to destroy a servant that is managed by our servant locator the object simply requests that the servant locator remove it from its mapping table. When the servant locator does this there are no more outstanding references to the servant and the run-time can garbage collect the servant.</p>

<p>The evictor pattern comes into play when the servant locator's Object ID to servant mapping table is full. At this time the servant locator must make space in its mapping table for new servants by removing existing servants. Our servant locator uses a timeout algorithm for deciding which servants to evict.</p>

<p>The actual management of the Object ID to servant mapping is performed by the <code>EvictorQueue</code> class. This class maintains a fixed size array for servants and the index into the array is used as part of the Object ID for that servant. The actual array holds instances of the <code>EvictableServant</code> class which contains the servant reference and timestamps that are used for eviction and object identification purposes. Each time that a servant is accessed its last used timestamp is updated and it is moved to the tail of a list that is maintained in least recently used order. When the array of servants is full the least recently used list is scanned for an object that has timedout, all objects that have timed out are evicted from the array and are available for garbage collection by the run-time. If no servants have timedout then an exception is thrown.</p>

<p><b>Comparing the Java and C++ implementations</b><br />
The Java implementation is more intrusive than the C++ implementation due to the fact that the Java servant implementation must derive from the appropriate POA class and cannot also derive from a mix-in base class that provides the common servant locator code. We could use a Tie implementation but that is likely to be as complex.</p>

<p><b>Download</b><br />
The following source was built using Ant and tested with JacORB 3.21 - the open source Corba Java ORB.<br />
<a href="http://www.jacorb.org/">Get JacORB</a><br />
<a href="http://www.lenholgate.com/zips/JEvict1.zip" onclick="_gaq.push(['_trackEvent', 'Downloads', 'JEvict1.zip']);">JEvict1.zip</a></p>

<p><b>Revision history</b><br />
</p><ul>
<li>23rd July 2001 - Initial revision at <a href="http://www.jetbyte.com">www.jetbyte.com</a>. </li>
<li>23rd August 2005 - reprinted at <a href="http://www.lenholgate.com">www.lenholgate.com</a>.</li>
</ul><p></p>]]>
    </content>
</entry>

</feed>



