Posts Tagged ‘Spring’

Why Java

Monday, August 23rd, 2010

At Cake Solutions, we have been using Java for quite a few years. Recently, I had to formulate why our customers should chose Java over other languages. It was easy to come up with geeky reasons, but purely technical reasons on their own are not enough. It turns out that modern Java EE applications are successful, interesting and exciting because of all the tools we have in the Java world–it boils down to the fact that I believe that a lot of the innovation we see in contemporary computing runs on the JVM and with JDK 1.7, we should see even more exciting developments.
In the meantime, download the Why Java PDF, where I try to explain why you should use JVM-based platform over anything else.

Publish or perish

Wednesday, March 31st, 2010

As the old adage goes, one needs to publish or be forgotten. It’s been quite some time since we gave you Pro Spring 2.5; in that time, Spring 3.0, then 3.0.1 came out and we’re all eagerly awaiting Spring 3.1.
Now, I’ve been fighting off the writing bug for quite some time, but no more. I am starting work on the new Spring “book”, except this time, it will not be a book at all. I will publish the PDFs of the various chapters here on our blog and I will also run the best parts of the text in the Open Source Journal–yes, the printed edition.
So, keep an eye on this space for the first chapter–give me 2 – 3 weeks from now!

October North West Spring User Group Meeting

Monday, October 19th, 2009

Last Tuesday was the North West Spring User Group. I was asked to deliver my jQuery talk that I gave to the South Spring User Group back in September last year.

It’s always good talking about jQuery as it’s relatively straight forward for the audience to read and has a shallow learning curve (if you know CSS already). One of the other pleasures talking about jQuery, and other front end tech, is that you can always show interactive demos to “wow” the audience…

Here are my slides followed by a link to my demo code:

I brought some of my demo code up to date showing examples using the live, event delegation functionality with jQuery. You can view my demo here or download it zipped here.

If you have any queries about the above or want assist you in a jQuery, Javascript and AJAX project contact us.

Spring Integration

Jonas Partner from OpenCredo also gave a presentation on the evening on Spring Integration. His slides are below:

Today’s web applications

Tuesday, September 22nd, 2009

I gave a talk on today’s Java EE and related web applications at yesterday’s London Digital Week event at Skills Matter. I had a full room of geeks keen to find out what I think about web application development. I hope that all attendees learned at least one new thing and that they’ll all find the time to try out Spring, Ruby on Rails or Grails.
To get started, you can download the source code of the talk from http://github.com/janm399/ldw/tree/master; the PDF version of the talk is available at http://github.com/janm399/ldw/raw/master/talk/ldw-pdf.pdf.

I’m looking forward to seeing most of you again at the next Java User Group or Spring User Group event!

That’s what I call performance

Thursday, August 27th, 2009

We are completing work on caching and routing tier we added to a lived-in system. The performance increase is very substantial and I will write a lot more about how we achieved this next week.
For now, I’ll wet your appetite with a screenshot that says it all

Service node statistics

Service node statistics


P.S. Yes, we’re talking about the RDTSC and System.nanoTime()!

Looking for a ${property}

Monday, August 24th, 2009

“Location, Location, Location” may not be enough.

Having property files loaded using ProperyPlaceholderConfigurer bean or the modern <context:property-placeholder> can become a real pain when the placeholders are distributed among the jars.

<context:property-placeholder location=”classpath*:*.properties”/>

Well, only location can be set here, the rest is default. Pretty cool, but it is not doing it… We look back to the old fashion bean

<bean class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”locations” value=”classpath:*.properties”/>
</bean>

The hack comes here: change from the default placeholderPrefix to a specific one, unique among jars used and everything is gonna be alright.

<property name=”placeholderPrefix” value=”£{”/>

Don’t forget now to change the references to the properties (from ${…} to £{…}).

Source 1 Source 2

<bean id=”clientPropertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”searchSystemEnvironment” value=”true”/>
<property name=”systemPropertiesMode” value=”2″/>
<property name=”order” value=”100″/>
<property name=”properties”>
<props>
<prop key=”BPB_SERVICES_HOST”>bpb-services01</prop>
<prop key=”BPB_SERVICES_HOST_PORT”>1099</prop>
</props>
</property>
<property name=”placeholderPrefix” value=”#{”/>
</bean>

Spring User Group speaker icebreaker

Wednesday, August 12th, 2009

The ice has been broken. The first step on the path to future speeches has been taken.

First of all I would like to thank each and every one in the audience for the great support. You can now download the demo source code and the OSGi presentation material.

You can also download Dr David Syer’s Spring Batch presentation.

Videos and photos from the event will be uploaded to our website and to the upcoming Cake blogs, so keep an eye on us.

On future of web programming at Cake

Friday, June 12th, 2009

Ruby eye for the Java guy

Ruby on Rails is a mature and innovative framework designed especially for the web. This allows rapid, stable and iterative web development. At Cake Solutions we use the latest technologies that ensure that websites developed on Rails will be built to specification, on-time and fully tested.

What’s in it for us, Java developers?

What is the fundamental difference between Ruby and Java? The main difference is that Ruby uses duck typing. The fundamental principle of duck typing is that “if it quacks like a duck, looks like a duck, then it is a duck”. In programming, this means that if an object responds to the signals we send it, then we have the correct type. Consider this code:

def foo(f)
   f < < "some text"
end

This signal handler (method) takes an argument f; the object f passed to the method must respond to the << operator. This means that we can call foo with an Array or a File, because both objects respond to the << operator.
"But," you say, "this feels too brittle. I prefer manifestly-typed languages such as Java, where the compiler will check the types."
First of all, manifest typing does not prevent type errors at runtime. If it did, there would be no need for a ClassCastException. The very existence of this exception means that even manifestly-typed languages do not guarantee type safety at runtime. Think about the collections framework in Java 1.4: the collections held just Objects and you had to do dynamic type casting. But how many ClassCastExceptions did you see at runtime and when you did, how difficult was it to fix it?
I put to you that manifestly-typed languages lull you into a false sense of security, where you believe that type errors cannot occur, because the compiler checks them, but I've shown that that is not strictly true.
With this in mind, why resist the typing freedom Ruby gives you? Look back at the foo method. To make the method as flexible as possible in Java, we would have to use some interface (Appendable) as its argument. But that requires a lot of fore-thought. In Ruby, you write code that deals with the typical situation (appending to a file, for example), but the same code can append to an array or a string, even if you never considered this in your design.
Finally, what makes code brittle? It is not the lack of compile-time type checking, it is lack of tests. Whatever language you use, I urge you to think of any code that is not tested as broken. From that follows that you both Ruby and Java can be equally robust, as long as you test your code.

The ecosystem

The libraries and frameworks available for any programming language largely determines its usefulness. Let's ignore the standard library and jump straight into web application development. Consider Spring Framework and Ruby on Rails. Ignoring the "enterprisey" features of the Spring Framework, both frameworks allow you to write web applications. But there is significant difference in the approach to writing applications in RoR and Spring Framework. Ruby on Rails has strong opinions about how you should write web applications. In Ani's famous words, "it's Ruby on Rails way on no way." A more subtle way of putting it is that RoR prefers convention over configuration. RoR's approach is that if it works for 99 % of the cases, it should work for you, too. RoR makes a lot of choices for you. For example, in Spring, to write CRUD code for a domain object, you have to write (in no particular order): the controller, couple of JSPs, web.xml, repository interfaces and their implementation, their Hibernate implementation and Hibernate mappings; ideally you will also have a services tier to isolate your controllers from the database. In other words, a lot of code.
In RoR, you run script/generate DomainObject title:string body:text. This generates the "domain object", database structure, the controllers and views, all automatically, because RoR has strong views about how you should write your code.
In Spring, you can use the Spring MVC annotations or you can use XML configuration; hey, you can even replace the DispatcherServlet with your own creation. But why would you want to? In most cases, you're paid to get the CRUD operation done, not to mess with the framework.

Does that mean you don't like Spring any more?

Absolutely not; we are still Java EE developers at heart. We are keen to use Spring's power for middleware, where we can take advantage of AOP, transaction management, JMS, JMX, Hibernate and all other big guns. For the front end, though, we are keen to use something simpler.
That something must be more flexible, more efficient. And that for us is Ruby on Rails and Grails!

tc Server

Tuesday, May 26th, 2009

“The Tomcat you know, the Enterprise capabilities you need” is how SpringSource advertise tc Server, but how true is that bold statement. Built around the robust Apache Tomcat (6+), tc Server’s power resides in the value added to the Tomcat distribution: its enterprise services. The main service provided is the “ASM” – Application & Server Management system which allows access through a web interface to control most of tc Server’s features – in addition a CLI offers administrators the desired raw power.

The popular application server is neatly wrapped by the enterprise capabilities and offered as an alternative to heavy enterprise application servers making the transition from an incumbent application server as in-obtrusive as possible to new clients. The current and forthcoming version of tc Server includes load balancing, server issue troubleshooting, diagnosis, statistics & you name.

  • Does “tc Server” have clustering capabilities? Yes it does.
  • Does “tc Server” handle distributed operations? Yes it does.
  • Does “tc Server” have reliable and friendly interface? Yes it does.
  • Does “tc Server” have capabilities to diagnose and react to problems based on defined set of scenarios ? Yes it does.

A recent demonstration by SpringSource ran through the main features of the product, showing tc Server to be as powerful as stated, but also showed it to be on the early adopters phase of its evolution. However the product appears to be on the right path and will reach the required maturity in the very near quickly.

In addition to the software services provided, “tc Server” comes with one more important service: the support you get when buying a “tc Server” subscription (annual subscription).

SpringSource offers support, which they promise to be legendary effective and reactive, for tc Server. In terms of pricing the figures don’t look that legendary as we are used to when it comes to SpringSource support, subscriptions being offered for prices between £300 and £500 per year, depending on response time and assistance availability.

There are strategies set in place for migrating from other application servers to “tc Server”, consultancy and support regarding the migration process being provided, so it looks like SpringSource have most support angles covered.

All things considered, “tc Server” appears to be a very attractive option for an application server to be used on future live deployments of clustered applications. Migration of existing environments doesn’t seem to be the smartest move though, since “tc Server” hasn’t reached the desired maturity level in order to substitute existing already paid for expensive alternatives.

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/