Thursday, March 18, 2010

Coherence with Spring on OSGi - Oh My!

OSGi has gotten some real traction as an Enterprise services platform. For one of the projects I was involved in a viability study for Coherence with Spring on OSGi recently. Even though not all can be divulged here but we overcame some interesting challenges that are worth to blog about.

Why OSGi? Why Spring? and Why Coherence (Is that a question)?

  • OSGi - It has championed the modularity.
  • Spring - Dependency Injection probably is still its biggest strengths.
  • Coherence - Solves World Peace with simplicity, elegance and Powerful APIs.
Challenges:
  1. OSGi - It needs patience. Lots of it specially if you are just starting with it. You do not need to change the application code to be OSGi compliant. It requires a specific MANIFEST.MF that describes the bundle's attributes and packages it exports and imports with other bundle dependencies. Make sure you have BND handy. I would not recommend you writing the Manifest file your own. Let BND do it for you. Equinox and Knopflerfish are two very popular platforms. I used Equinox.
  2. Spring - Do not work with vanilla Spring APIs. You would need Spring Dynamic Modules that are Spring for OSGi. Also download Springsource Tool Suit. You would need some jars from it's STS to be installed. Also make sure you deploy the 3.0.0 version of the libraries. It will solve a lot of pain you would rather end up going through.
  3. Coherence - Coherence does not have an OSGi bundle off the shelf so you would need to create one. Creating an OSGi bundle for Coherence is fairly easy by using Eclipse IDE. File->New->Project->Plug-in from Existing jar Archives->Select coherence.jar and follow the rest of the instructions. Export the artifact. That's it. Watch out for serialization issues if multiple modules are involved.
Spring DMs:
Install the following modules in the same order (use appropriate start levels):
  1. com.springsource.org.aopalliance
  2. com.springsource.org.apache.commons.logging
  3. org.springframework.aop (3.0.0.RELEASE)
  4. org.springframework.asm ( 3.0.0.RELEASE)
  5. org.springframework.beans (3.0.0.RELEASE)
  6. org.springframework.context (3.0.0.RELEASE)
  7. org.springframework.core (3.0.0.RELEASE)
  8. org.springframework.expression (3.0.0.RELEASE)
  9. org.springframework.osgi.core (1.2.1)
  10. org.springframework.osgi.extender (1.2.1)
  11. org.springframework.osgi.io (1.2.1)
  12. org.springframework.transaction (3.0.0.RELEASE)
  13. com.tangosol.coherence (Or whatever name is given for the Coherence bundle created)
Don't shy away by restarting the Equinox a few times - you may need it.

Lets build a service: Say we want to build a service that puts a String key and a String value in a given data source.

public interface IPut {
public void put (String ds, String key, String value);
}

For an ability to replace Coherence as a Datasource with another lets use a Template that can be injected by Spring later with Application remaining agnostic to the data source provider.

public interface ITemplate {
public void put(String ds, String key, String val);
}

Even though IPut and ITemplate are similar it doesn't have to be. IPut represents the exposed service and ITemplate is for persistence.

Implementations:

public class Putter implements IPut {
private ITemplate template;

public Putter(ITemplate template) {
this.template = template;
}

public void put (String ds, String key, String value) {
if (template != null) {
template.put(ds, key, value);
}
}
}

And,

public class CoherenceTemplate implements ITemplate {

public CoherenceTemplate() {
}

public void put (String cacheName, String key, String value) {
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
ClassLoader newLoader = com.tangosol.net.CacheFactory.class.getClassLoader();
Thread.currentThread().setContextClassLoader(newLoader);
NamedCache nCache = CacheFactory.getCache (cacheName);
nCache.put (key, value);
Thread.currentThread().setContextClassLoader(oldLoader);"

}

}


Make sure you do have appropriate coherence-cache-config.xml deployed.

Configurations:
Spring beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd>

<bean name="template" class="CoherenceTemplate" scope="prototype">
</bean>

<bean name="daoService" class="Putter">
<constructor-arg ref="template"/>
</bean>

</beans>


That's not it. We need another configuration to expose this service for OSGi:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:service id="daoOSGiService" ref="daoService"
interface="IPut"></osgi:service>
</beans>


It's also important to place these two configurations under META-INF/spring directory.

What about the MANIFEST.MF?
Use bundlor to generate the MANIFEST.MF for you. You will have to copy the manifest back to the project and then re-bundle the final artifact jar.

Once you have it all together thats all you need to deploy this application on OSGi.

What about the Client now?
Client would be another OSGi bundle and the process of developing it is similar. As it is still all Spring driven make sure the services are appropriately injected.

First the OSGi configuration:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:reference id="daoService" interface="IPut"/>
</beans>

Then for other beans:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="persister" class="MyClient"
init-method="start" destroy-method="stop">
<property name="putter" ref="daoService"/>
</bean>
</beans>


And the client is like any typical Spring client:

public class MyClient {
IPut putter;

public MyClient () {
}

public IPut getPutter() {
return putter;
}

public void setPutter(IPut putter) {
this.putter = putter;
}

public void start() throws Exception {
putter.put ("MyDataSource", "key", "Value");
}

public void stop() throws Exception {
System.out.println("Goodbye");
}
}


Don't forget to use the BND again to generate the manifest and re-bundling it with the client artifact jar.

Now deploy the client jar and start it.

Enjoy!

Monday, March 15, 2010

Development problems I hate to get into

Here are my pet peeves when it comes to Software development:

  1. Click New Project and select type 'X' in your Eclipse IDE
  2. Deploying unending Apache libraries
  3. Power-point driven Architectural directions without self-contained working prototypes.
  4. Helpless Component dependencies and NoClassDefFoundError
  5. Having to change standard J2SE modules to work in a specific platform.
  6. URLs in response to RFH that you have already been banging your head on.

Saturday, March 13, 2010

Chevy Malibu shares the Arjun Tank story

A little history - India in mid-70's realized a need of an indigenous MBT and embarked upon once seemed a never ending path. Today after three decades of ups and downs the development of Arjun has been completed as it now competes with world's other Main battle tanks. It will not be an exaggeration to say Arjun is one menacing beast but to no avail because its user has already taken a love route to Russian T-90s that India had purchased in mid 90's more so in panic. Today even though Arjun is a better tank its not going anywhere because folks it was built for have already made up their minds.
This story sounds so similar to what Chevy Malibu is today. I have rented so many cars in past many years and I can say for sure Malibu is one that has impressed me the most. But where does it stand against Toyota Camry and Honda Accord? Even with problems of Toyota if I have to buy one I would rather buy a Camry than a Malibu, renting could be another story. Its called a proven platform in the changing world. I will be ready to give Toyota a pass for its problems because I believe they are serious in delivering the same quality that it once was known for. With GM, no.

Friday, February 26, 2010

होली पर एक शेर

चलो होली के इस रुत में आज कुछ रंग हो जाए,
जो आशिक है छुपा बैठा, थोड़ा सा तंग हो जाए।
महक फूलों कि चादर मैं बना कैसे इसे ओढूँ?
हवा में खुशबुयें जो हैं, हमेशा संग हो जाए।

चलो फिर से शेर सुनो

अजीब वाक़या है ये ज़िन्दगी हर मोड़ मुड़ती है।
लगे तो ये कि आखिर मै चलाता हूँ इसे फिर भी,
ये अक्सर राह ही अपने आप मुड़ के यूँ निकलती है।
खुदा जाने कि राहें ये चुना मैंने, या उसने है चुना मुझको?
पर जब भी यूँ लगे कि हाथ में पतवार आयी हो,
झटक के वो सहारा ही मेरे हाथों फिसलती है।

Wednesday, February 17, 2010

A week with Six companies

This has been a week where directly or indirectly I interacted with six companies for the services I use. Here is my rating out of 10 with 10 being highest:

  • Costco - Went to buy a Sleep Science mattress. Costco only carries them with online orders. Ordered one online with tentative shipping day between 2-4 weeks. Seemed a lot. The next day I receive an email that the order has been fulfilled and Mattress has been shipped. Rating - 8
  • Sleep Science - A manufacturer of memory foam mattress. I have used NovaFoam toppers before but this 10" mattress is just awesome. Not as good as Tempurpedic but the cost difference vs quality is just too close. Rating - 9.5
  • UPS - Costco shipped the bed with UPS who gave me shipping time between Morning 8AM to Evening 7PM. Not something you can plan your day around. Because of this vague time slot I had to work from home all day. A shorter slot could have been much better. Mattress arrived around 3PM. Rating - 7
  • Comcast - A company slowly that is getting difficult to pass by these days. I had to transfer my services to this location. They were good to give me a time slot between 4-6PM. The technician arrived at 6:30 - so not good. Looked friendly but he found he needed an amplifier as signal was too low. He was not carrying an amplifier which I would think should be part of their installation package in case required. He plugged some basic cable with a promise to come tomorrow between 12-1PM to install the rest. Rating - 6
  • Apple - A company that boasts itself an epitome of perfection. I have an iPhone so this has become an ongoing thing. I had bought an iPhone compatible data cable because of repeated issues with one given by Apple. This cable worked with my iPhone for close to two months and I carry either this or Apple's when I travel. Last night I suddenly get a message that cable is not compatible and cannot be used. With USB being the only option to charge this phone, my iPhone discharged over the day. I wanted to give Apple a big zero for the premium they charge and pain I am having but giving a benefit of doubt over using a cable not from Apple. Rating - 1.5
  • Sprint - I have a Sprint data card that has been a key to my productivity because the nature of my job where I cannot expect a Cat5 around all the time. In December'09 Sprint offered me a complementary upgrade to their 3g/4g device. Soon I found it was not a complementary upgrade but a Test Bed of it's new technology. I am sitting at the heart of bay area and this device has trouble finding the signal. It connects and disconnects intermittently severely affecting my work. Spent close to 30mins with their technical support who could not fix it and asked me to go to a near by store. I feel cheated by Sprint. With no phone and this flaky internet has put me almost isolated. Rating - 0

Tuesday, February 02, 2010

इक कॉमेडी शेर

पुकारें सुन सुन के वो खुदा शायद थक गया होगा,
ईमेल है नहीं मेरा "अच्छा है" ये फिर भी सोचता होगा।
गर चीखें मेल से आतीं तो कैसे हेल्प करता मैं,
जो टाइम खोलने में लगता बेचारा मर गया होता।
मोबाइल ना बनाया मैं पर उंगलियाँ दी बहुत सी हैं,
रहम एसेमेस से जो जाता, तू क्या मस्जिदों में बिल भरता?