Using Spring Integration in Osgi platform

August 13 2009one Commented

Categorized Under: Java, Osgi, RIA, Software Engineer, Technology

In the post http://blog.esofthead.com/implementing-enterprise-integration-solutions-with-spring-integration/,
I have outlined several challenges that could be solved by using Spring Integration framework. Engroup, the product of eSoftHead (http://esofthead.com/node/25), uses Spring Integration extensively to make loose coupling among its modules. Messages are used to exchange information among modules and each module has own appropriate channels to send messages that other modules could be interested. Here are several examples:

- If any engroup modules need to tag some resource, which resource can be a document or a CRM Lead etc, it does not call tag service (if it do so, tag service must be available and it depends on tag service) but send a message with necessary information to a specific channel name 'add.tag'. If a tag service is available, it will receive message from that channel and save tag as requested.

- When a new user register to engroup system, engroup must do: 1. Create new user document workspace 2. Synchronize user information with its forum and wiki and several tasks later we will add, for example create a account with the same name in chat server, email server. Instead of adding code in register user part and modifying code when there is new added action when register user, we simply send a message to 'add.user' channel, any interested parties would listen this channel and do their tasks.

Engroup runs in Spring Osgi platform that we can control the dependencies among engroup modules and engroup external libraries. With the goal to make engroup modules are loose coupling, Engroup uses publish-subscribe pattern to make two modules have no dependencies in code can talk together with the help from ActiveMQ (messaging server) and Spring Integration framework.

Here are an example that we use all of these tools in engroup: we have two modules engroup document management module and engroup tag resource. Each document could have many tags, when a document is saved into engroup data-store, it will post message to appropriate channel to notify engroup tag module to save its tag. The similar process is applied for any module support tag source in engroup.

  • Set up embedded ActiveMQ server:

For deployment simplicity, engroup uses embedded ActiveMQ instance in its community edition.

        <!--  lets create an embedded ActiveMQ Broker -->
        <amq:broker useJmx="false" persistent="false">
                <amq:transportConnectors>
                        <amq:transportConnector uri="tcp://localhost:0" />
                </amq:transportConnectors>
        </amq:broker>

        <!--  ActiveMQ destinations to use  -->

        <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
        <amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost" />

        <bean id="connectionFactory"
                class="org.springframework.jms.connection.SingleConnectionFactory">
                <property name="targetConnectionFactory">
                        <ref local="jmsFactory" />
                </property>
        </bean>
  • Using Spring JMS to send message:
            <!--Convert java object to Json message format-->
            <bean id="messageConverter"
                    class="com.engroup.integration.jms.converter.JsonMessageConverter" />
    
            <bean id="jmsTemplate"
                    class="org.springframework.jms.core.JmsTemplate">
                    <property name="connectionFactory" ref="connectionFactory" />
                    <property name="messageConverter" ref="messageConverter"></property>
            </bean>
    
            <bean id="messageDispatcher"
                    class="com.engroup.integration.dispatcher.jms.JmsMessageDispatcher">
                    <property name="jmsTemplate" ref="jmsTemplate" />
            </bean>
  • Define message destinations:

In engroup tag module, we define two message destinations: tagRemove and tagAdd

        <!--Import spring service connection factory from engroup esb module-->
        <osgi:reference id="connectionFactory"
                interface="javax.jms.ConnectionFactory" />

        <bean id="tagRemoveDest"
                class="org.apache.activemq.command.ActiveMQQueue">
                <constructor-arg value="tag.remove" />
        </bean>

        <bean id="tagAddDest"
                class="org.apache.activemq.command.ActiveMQQueue">
                <constructor-arg value="tag.add" />
        </bean>
  • Define service activator for channels:

Now, we define service activator to handle tag add request

        <bean id="tagServiceActivator"
                class="com.engroup.module.tag.integration.TagServiceActivator">
                <property name="tagService" ref="tagService" />
        </bean>

        <!-- Establish add tag channel and related things -->

        <jms:inbound-gateway id="jmsTagAdd" request-destination="tagAddDest"
                request-channel="tag.addChannel" reply-timeout="5000"
                request-timeout="5000" />

        <integration:channel id="tag.addChannel" />

        <integration:channel id="tag.addChannelAfterTransformer" />

        <integration:channel id="tag.result" />

        <engroup-esb:json-to-map-transformer input-channel="tag.addChannel"
                output-channel="tag.addChannelAfterTransformer" />

        <!--Method addTag is used to receive message from channel &#39;tag.addChannelAfterTransformer&#39;,
process and return message to channel &#39;tag.result&#39;-->

        <integration:service-activator ref="tagServiceActivator"
                method="addTag" input-channel="tag.addChannelAfterTransformer"
                output-channel="tag.result">
        </integration:service-activator>

Here is a part of code of TagServiceActivator:

public void addTag(Map tagInfo) {
        String contentId = (String) tagInfo.get("contentId");
        String[] tags = (String[]) tagInfo.get("tags");

        tagService.deleteTagRelationshipOfContent(contentId);

        for (String tag : tags) {
            tagService.saveTagRelationship(contentId, tag.trim());
        }
    }

Remember to add dependency Spring integration packages into MANIFEST.MF file, otherwise you will get ClassNotFoundException while
running Osgi.

  • Send message to tag module channels

We go to the code of any modules are interesting in tag resource, in our example it is document module. First, we import messageDispatcher to document service bean:

         <osgi:reference id="connectionFactory"
                interface="javax.jms.ConnectionFactory" />

        <osgi:reference id="messageDispatcher"
                interface="com.engroup.integration.dispatcher.MessageDispatcher" />

        <bean id="internalAccessValidatorFileService"
                class="com.engroup.module.ecm.service.impl.AccessValidatorFileSystemServiceImpl">
                <property name="jcrTemplate" ref="jcrMappingTemplate" />
                <property name="transactionService"
                        ref="contentTransactionService">
                </property>
                <property name="messageDispatcher" ref="messageDispatcher"></property>
        </bean>

When we want to send a message to add tag channel of engroup tag module, we just simply construct a message and messageDispatcher will do the rest:

    private void sendSaveTagRequest(String contentId, String[] tags) {
        Dictionary
 props = new Hashtable();
        props.put("contentId", contentId);
        props.put("tags", tags);
        messageDispatcher.dispatchObject(AvailableDestinationNames.TAG_ADD,
                props);
    }

Osgi can help you control strictly dependencies of a module with external packages if we control well, we can limit dependencies of one engroup module with other module. Spring Integration framework provides solution to integrate data among modules without modifying existing code base. At the beginning, we feel it is complex solution while combining Spring Integration with Osgi platform. However, after number of modules is grown continuously also data exchange among modules are increased, such complex solution become a simple one on both development and deployment. Could you have other solutions?

The following screen-shots are result of displaying tag in document module.

ecm_tag

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • blinkbits
  • BlinkList
  • blogmarks
  • BlogMemes
  • BlogMemes Cn
  • BlogMemes Fr
  • BlogMemes Jp
  • BlogMemes Sp
  • Blogosphere News
  • Blogsvine
  • blogtercimlap
  • Book.mark.hu
  • Bumpzee
  • co.mments
  • connotea
  • De.lirio.us
  • Design Float
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • feedmelinks
  • Fleck
  • Furl
  • GeenRedactie
  • Global Grind
  • Gwar
  • Haohao
  • HealthRanker
  • Hemidemi
  • Identi.ca
  • IndianPad
  • Internetmedia
  • kick.ie
  • Kirtsy
  • laaik.it
  • Leonaut
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Ma.gnolia
  • Meneame
  • MisterWong
  • MisterWong.DE
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • Ping.fm
  • PlugIM
  • Pownce
  • ppnow
  • Print
  • Propeller
  • Ratimarks
  • Rec6
  • Reddit
  • SalesMarks
  • Scoopeo
  • scuttle
  • Segnalo
  • Shadows
  • Simpy
  • Slashdot
  • Smarking
  • Socialogs
  • SphereIt
  • Spurl
  • StumbleUpon
  • Symbaloo
  • Taggly
  • TailRank
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • TwitThis
  • Upnews
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Buzz
  • YahooMyWeb
  • Yigg

Related posts:

  1. Spring Dynamic Modules (for Osgi) – the good complement for Osgi Spring Framework - the de factor standard IoC container...
  2. Osgi – good platform for SaaS model If you are developing the software base on SaaS model...
  3. How to invoke Spring beans in other osgi bundle Spring Dynamic Modules helps integrating Spring beans to Osgi platform...
  4. Implementing enterprise integration solutions with Spring Integration In our last project, we faced 2 challenges of how...
  5. Integration of Jackrabbit OCM and Spring (updated version) In the previous post Jackrabbit OCM and Spring, I have...

One Response to “Using Spring Integration in Osgi platform”

  1. Nice writing style. Looking forward to reading more from you. Actually I am also related from Digitalxsteam that is your one stop online source for complete Back End Application Solutions. We provide all types of e-business solutions with satisfaction. For details, please visit on our website.

Leave a Reply