Posts Tagged ‘open source central’

Introducing AsyncWorker

Tuesday, June 23rd, 2009

I have another project for the Open Source Central: AsyncWorker. I am tired of writing similar asynchronous call infrastructure from scratch in every project. This is why I am going to write the AsyncWorker. At the highest level, there will be an interface (class?) AsyncWorker defined as

interface AsyncWorker {
    <T> T invoke(T t);
    <T> T invoke(T t, int maxWait);
    <T> InvocationIdentity execute(T t, AsyncCallback callback);
}

The implementation of the AsyncWorker will be flexible enough to use simple heap, JMS or database queues to store the invocations and the API is very easy to start using in your existing projects. For example, if you wanted to turn a synchronous call into asynchronous one with as little coding as possible, you’d write

@Controller
class ExistingController {
    private SomeService someService;
    private AsyncWorker asyncWorker;

    @RequestMapping(...)
    public void index() {
        // this.someService.doWork(1, 2, "hello");     #A
        this.asyncWorker.invoke(this.someService).     #B
            doWork(1, 2, "hello");
    }
}

That should be it! Instead of synchronous call on line #A, you will use the AsyncWorker on line #B. The AsyncWorker will queue the invocation of the doWork method of SomeService and will return immediately. Because of the asynchronous nature of the call, the value returned from the method call will be default for the return type (0 for int, long, 0.0 for double, float, null for object reference, etc.).
The alternatives, as you can guess, are using the invoke(T, int) method, which will also invoke any method of T asynchronously, but will wait up to the specified timespan before returning. If the invoked method completes within the specified timespan, the returned value will be valid, otherwise it will be the default value.
Finally, you can use the execute method to schedule the asynchronous call — this gives you the highest degree of control and you will get an InvocationIdentity object back. You will be able to use the returned InvocationIdentity to monitor the progress of the call.

Call for comments

If you think this is a good idea, please comment and add any features you’d find useful. We will be releasing this project under the Open Source Central under Apache Licence.

Hibernate and primary key unique constaint exception

Thursday, April 23rd, 2009

Its one of those things – you have the same problem every now and then, but not often enough to remember what the solution was.
I was implementing some hibernate code, but the tests for it failed due to primary key constraint exception (basically hibernate was trying to save already save object using same primary key). I can clearly remember that i have seen this exception before, but the cause and solution were lost somewhere between all those NonUniqueObjectExceptions and jsp exceptions that i had problems with few weeks ago :) .
So i had to dig into in once more, and promise to myself that i will blog it after i diagnose the problem, so i don’t forget about it ever again (and save someone else the trouble as well). So here it is:

The domain model and hibernate mapping were really basic – Descriptor object has reference to List of Note objects:


public class Descriptor{
    private Long id;
    private List<Note> notes = new ArrayList<Note>;

    public void addNote(Note note){
       note.setDescriptor(note);
       this.notes.add(note);
   }
   //getters and setters omitted for clarity
}
public class Note{
    private Long id;
    private String text;
    private Descriptor descriptor;
    //getters and setters omitted for clarity
}
<class name="Descriptor" table="t_descriptor">
        <id name="id" type="long" unsaved-value="null">
            <generator class="sequence">
                <param name="sequence">s_descriptor_id</param>
            </generator>
        </id>

        <list name="notes" cascade="all"
            <key column="descriptor"/>
            <index column="id"/>
            <one-to-many class="Note"/>
        </list>
</class>
<class name="Note" table="t_note">
        <id name="id" type="long" unsaved-value="null">
            <generator class="sequence">
                <param name="sequence">s_note</param>
            </generator>
        </id>
        <property name="text" column="text" not-null="true"/>
        <many-to-one name="descriptor" column="descriptor" not-null="true"
                     class="Descriptor"/>
    </class>

Looks simple, but when i run the test for the code above, i got dreaded primary key unique constraint exception.
After a bit of though, i was able to kick myself for not noticing the problem:
The notes property of Descriptor class is mapped with cascade=”all” meaning all save, updates, deletes with apply for the child objects as well. However, the inverse is set to false (inverse property is missing, defaults to inverse=”false”) – making both sides of the bi-directional relationship responsible of taking care of the relationship. SO Hibernate generates two insert statements, one because of cascade=”all”, and one as part of inverse=”false” rule.
The solutions is to set inverse=”true” on notes property mapping – this will make just one side of bi-directional relationship responsible for relationship, and the Hibernate will issue just one insert statement for the Note object.
Here is the correct piece of mapping:

        <list name="notes" cascade="all" inverse="true">
            <key column="descriptor"/>
            <index column="id"/>
            <one-to-many class="Note"/>
        </list>

Huh! Cascade and inverse properties when mapping collections in hibernate simplify the development, and improve performance if used correctly, but beware of the pitfalls of unsuspected exception.
To read more go to the Hibernate website or read this blog: http://www.codeweblog.com/hibernate-in-the-inverse-and-cascade/

The first Spring User Group in Manchester

Tuesday, April 21st, 2009

The first Spring User Group (SUG) event was held in Manchester on the 8th April at the Core Technology Centre in Manchester (a superb venue for anyone looking to hold an event in the North West). 46 people attended, which was fantastic for the first event and the feedback was all very positive. Attendees travelled from as far a field as Glasgow, Southport, the Midlands as well as Manchester and surrounding areas.

We were treated to practical demonstrations by Rob Harrop from Spring Source on Building REST applications with Spring Framework 3.0 and Jan Machacek from Cake Solutions on Building an application using the Spring Framework 3.0. The video of both talks can be seen at, http://vimeo.com/channels/opensourcecentral. The code for Rob’s talk can be found at http://github.com/robharrop/restlist/tree/master or for checkout users can simply do: git://github.com/robharrop/restlist.git. Jan’s code can be found at http://www.cakesolutions.net/teamblogs/2009/04/09/sug-source-code/.

The topics most requested for talks in future events were OSGI, Groovy and Spring security, we are actively arranging talks on these subjects. The consensus was that the SUG North West be run bi-monthly so the provisional date for the next meeting is the 3rd June 2009 at the same venue. If you want to be kept in the loop for future events or announcements please join the LinkedIn North West Spring User Group, e-mail me at guy@springusergroup.org.uk or visit the SUG website at http://www.springusergroup.org.uk/.