Sunday, July 6, 2008

Write once scale everywhere

I think you've seen this slogan pretty often and every grid/clustering framework promise you to scale your application once you write something based on their classes and which would depend on their code.

You know I did not like this solution and thought a lot about such dependences. It's just a way to take money of you. If you decide to change the framework or application server you would probably spend a lot of time to reconfigure the code or even rewrite your code to fit new requirements of another framework and so on.

Let me show you another way. The way that I like the most. The way to scale your Java + Spring application without having any dependences on GridGain framework.

Recently we introduced "executor service" feature that allows you to run your code remotely and which I'm going to use and scale.

Here is a typical example of Java code that runs some Callable on executor service. The only difference here is that we use Spring to obtain executor service (which is to my understanding quite flexible).

Typical executor service code

AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(
"org/gridgain/examples/execservice/spring.xml");

// We register Spring shutdown hook to provide
// automatic beans destruction by Spring.
ctx.registerShutdownHook();

// Get Grid from Spring.
ExecutorService exec = (ExecutorService)ctx.getBean(
"myExecutorService");

Future<String> future = exec.submit(new MyCallable());

...

String res = future.get();

Typical Callable

private static final class MyCallable implements
Callable<String>, Serializable {
public String call() throws Exception {
...
}
}

Note that MyCallable class implements Serializable which is obviously a requirement if you are going to send it to another computer over the network.

That's it. There is no references to any Grid framework or application servers. Java + Spring as I promised.
And obviously it is scalable within single Java VM. Now let's do the trick and make it scalable on the Grid by configuring another executor service in spring.xml. Starting from 2.1.0 (with new feature Grid Spring bean) which should be pushed into production in 2-3 weeks this XML will look as following:

Spring.xml with grid bean and executor service

<bean id="mySpringBean" class="org.gridgain.grid.GridSpringBean"
scope="singleton">
<property name="configuration">
<bean id="grid.cfg"
class="org.gridgain.grid.GridConfigurationAdapter"
scope="singleton"/>
</property>
</bean>

<bean id="myExecutorService" factory-bean="mySpringBean"
factory-method="newGridExecutorService" scope="singleton"/>
And now ladies and gentleman it works on the grid. Simplicity and elegance. That's what we'd like to give our customers and that's what we always think about when write new features. This is customer driven development.

0 comments: