• Home
  • RSS Feed
  • Log in

Guaranteed Delivery in Spring Integration
Posted by Wilfred Springer just before lunchtime: November 27th, 2009

This is not a rant against ESB. I am not saying that ESBs never have a purpose, nor suggest that it's all just a scam. If - after having read this post - you got the impression that I suspect a conspiracy behind ESB, then I want to tell you up front that this is certainly not what I intended to say.


The only claim I am actually making is that - if you are interested in implementing integration patterns - you can certainly achieve that with a whole lotta less. And as always, less clearly is more. You do not always need a monumental solution to solve an integration problem. Spring Integration and Camel are wonderful alternative solutions, and way less intrusive.

That doesn't mean that it's all just roses and sunshine. There are certainly things left to desire. One of the things that I find sort of missing in Spring Integration is an easy way to have guaranteed message delivery, so that's what this post is about.

Channels

One of Spring Integration's major abstractions is a channel. Think of it as a queue that is not necessarily backed by - well, a queue. It's just the place where you hand-of or receive a message, without the sender and receiver necessarily being aware of each other's existence. When the receiver reveives the message, then that message might be pulled from a buffer kept internally inside the channel (like in a QueueChannel), but it might as well be delivered in a synchronous manner, by the same thread that sent it to the channel (like in a DirectChannel). From a receiver's point of view, it's all the same.

Spring Integration Channel Abstractions

Spring Integration Channel Abstractions

Now, if you're dealing with a channel that is backed by a an actual in-memory BlockingQueue, then you do have some degree of reliability. You know the message will be delivered at least once, at most once, and that order is preserved, but only if your system is never shut down. If your system is shut down, either because you stop it yourself, or because of unexpected error conditions, all of the messages that are inside those channels can essentially be considered lost: whenever the system reboots, all of those queues will be empty.

Message loss in regular channels, versus KahaChannel

Message loss in regular channels, versus KahaChannel

Idempotency to the rescue?

You could argue that you could just replay all of your messages, and - as long as the receivers or intermediates are all idempotent receivers - everything will just work out fine, but then I tend to disagree. First of all, you do not want to replay your entire history of messages at system startup. So you probably need some mechanism to prevent messages that already traversed the entire chain of message handlers from being sent again. If idempotent receiver is the only trick up your sleeve, you end up doing a hell of a lot of work yourself, and it is not going to make your life any easier.

Apart from that, if you do to replay a fair selection of messages that entered your system in the past, then you still need the ability to reliably store the messages at the outer rim of your system. So even if you abstained from wanting reliable delivery guarantees inside your system, you still will not be able to avoid a reliable hand-off at the edge of your system. (That is, unless you convince your callers (business partners, external clients) to do all of this bookkeeping for you, but I bet you will not be able to pull that off.)

JMS to the rescue?

Now,  I said Spring Integration does not have support for guaranteed delivery, which actually was a lie. You can have reliable delivery, if you route your messages through JMS, and use a persistent queue. So, it can be done. However, why would you want the overhead of the JMS API and everything that sits behind it, if you could also implement the PollableChannel in a reliable way directly?

Kaha DB to the rescue!

So, that's what I did. I present to you, the KahaChannel. Kaha is ActiveMQ's data store backing persistent message queues. So essentially, all I did is rip out Kaha underneath ActiveMQ and implemented the PollableChannel interface on top of that. The result is a native PollableChannel that directly stores messages into ActiveMQ's highly optimized data store. And Kaha is file-based, so it does not require any other external datastore.

(Obviously, the PollableChannel interface could also be easily implemented on top of some other journalling system, such as Howl. However, Kaha seems to be way more active than any of the other journalling systems I am aware of.)

So now what?

Using the KahaChannel is fairly simple. It is just a drop-in replacement for any other PollableChannel. Unfortunately, it does not nicely blend in with the Spring Integration namespaces (yet), so you do need to construct an instance of a KahaChannel using vanilla Spring configuration.

So, if this used to be the configuration of your channel before:

    <si:channel id="inboxl">
        <si:queue capacity="1101"/>
    </si:channel>

Then the only thing to do to make your system a little bit more reliable is drop in this as a replacement:

    <bean id="inbox" class="nl.flotsam.spring.integration.kaha.KahaChannelFactory">
        <property name="directory" value="..."/>
    </bean>

The example above uses the KahaChannelFactory for constructing an instance. That's not a requirement. You can also construct it directly, by creating an instance of KahaChannel and passing in the parameters as constructor arguments, but then the name of the container constructed underneath will not automatically correspond to the id of the bean.

Performance

Replacing an in-memory buffer-based channel with a disk-based with reliable delivery guarantees obviously has its price. However, the performance penalty is not necessarily huge. It all depends on the size and complexity of your messages. If your messages cary hardly any data, then the penalty is fairly small: pumping 10.000 messages through an in-memory channel took 3.2 seconds on my system. Running the same messages through a KahaChannel took only 4.3 seconds. You do the math.

Encoding Messages

In order to make the KahaChannel really a reliable channel that will survive a crash, the messages obviously need to be stored. Unfortunately, Spring Integration does not define an abstraction for encoding and decoding messages yet.

The Kaha abstractions allow you to encode to DataOutput and decode from DataInput, so anything eventually needs to be mapped to something supporting these abstractions. Rather than trying to implement the ultimate way of encoding and decoding a message itself (if something like that would exist at all), the KahaChannel basically takes the stance that it is up to you to provide a sensible codec. And for that purpose, it defines its own message codec interface:

public interface MessageCodec<T> {
    void encode(Message<T> value, DataOutput out) throws IOException;
    Message<T> decode(DataInput in) throws IOException;
}

It does however also ship its own SimpleMessageCodec, which implements a ridiculously simple strategy for persisting your messages, only supporting basic types as int, boolean and String as types of the payload and headers.

Conclusions

Spring Integration itself does not provide a persistent message channel, guaranteeing that your messages will eventually arrive at the receiver, even in case of system restarts. However, if you leave that out, you end up with a toolbox that pretty much provides most of what you would expect from a fullblown ESB solution, at least in terms of its support for implementing integration patterns. A JMS Message Broker could be used to add durable message persistence to the mix.

However, despite the fact that Spring Integration has no support for persistent message channels built in, it does allow you to add your own. Implementing a persistent channel on top of those abstractions turns out to be fairly easy, if you rely on a data store such as Kaha. And having a lightweight native persistent message channel seems to be a more natural fit in Spring Integration's lightweight approach. In a sense, it is similar to the NMR in JBI implementations: you know your messages are safe once they are inside: it may be backed by a JMS Message Broker, but to the ESB developer, it's just a native component of the product.

  • Share/Bookmark

Tags: esb, integration, messaging, SOA, spring integration
Filed under Java, SOA | 7 Comments »



7 Responses to “Guaranteed Delivery in Spring Integration”



    jos Says:
    Posted at: November 27, 2009 at 11:57 am

    JBI NMR is not systematically backed by a persistent message store : NMR is used to manage communication Exchanges between endpoints and support patterns (in-out, in-only, …) and sync or async communication, the exchange end state tells the initiator if it was successful or not.
    NMR can be persistent (slow) or SEDA based (fast but not persistent). In this case safe endpoints must deal with persistency and retry (such as with JMS endpoint for instance) to manage exchange failures.
    Regards



    Mark Fisher Says:
    Posted at: November 27, 2009 at 8:20 pm

    Nice post! We should be able to support a custom “queue” strategy within Spring Integration such that namespace support would be available with something like a “ref” attribute or inner-bean defined within a queue element. On one hand, with the introduction of MessageStore in 2.0, we will have a persistence option (via a DataSource-backed implementation), but that does not provide queue characteristics inherently. On the other hand, we could simply delegate to any java.util.BlockingQueue reference, but that defines more methods than we really need.

    As in your example, all that is really needed is something to support “addLast” and “removeFirst”, and *timeouts* for each if supported by the underlying implementation. These could be mapped directly to our channel’s send and receive calls or we could provide something like a BlockingQueueAdapter that does implement BlockingQueue but then delegates to such a strategy interface. In any case, my goal would be to not only support this with a single strategy hook but also to provide one or more persistent options out of the box within the 2.0 timeframe.

    Regards,
    Mark



    Erik Rozendaal Says:
    Posted at: November 30, 2009 at 10:32 am

    So how much faster is the KahaChannel compared to the JMS one?



    Wilfred Springer Says:
    Posted at: November 30, 2009 at 10:39 am

    I haven’t tried yet. :-)



    Age Mooy Says:
    Posted at: November 30, 2009 at 10:57 am

    Why would you not use a standardized API implemented by the same backend you’re talking to now ? Not invented here ?



    Wilfred springer Says:
    Posted at: November 30, 2009 at 9:43 pm

    Basically cutting out abstractions and making things a little bit more understandable. Nobody likes the JMS APIs, but we still accept it in between everything we do. If I can do with less, then I’d prefer to do so.

    Another argument is performance. It turns out that we loose a freaking amount of cycles nowadays with piles of pointless abstractions.

    And then of course, I didn’t invent ActiveMQ. ;-) But to my defense I will say that I didn’t invent Kaha and I didn’t invent Spring Integration either. In fact, I didn’t invent anything at all, so this could never have been the not invented here syndrome manifesting itself.



    Mark Fisher Says:
    Posted at: December 2, 2009 at 3:32 pm

    Wilfred,

    I would be very interested to see the results of some crude performance tests between your Kaha-only channel, and Spring Integration’s new JMS-backed channel using ActiveMQ. That would be a great way to measure the “freaking amounts of cycles” ;)

    Whatever performance difference exists could be considered in the trade-off, but on the other side, there are additional benefits of the “full” ActiveMQ solution (queue management/monitoring via JMX, the ability to *share* a channel within a load-balancing/failover strategy, transactions with redelivery, etc).

    Regards,
    Mark



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (279)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Flex (17)
  • JPA (17)
    • JPA implementation patterns (13)
  • Eclipse (15)
  • Quality Assurance (14)
  • Middleware (19)
  • Frameworks (13)

Tag Cloud

    Closures esb Java qcon Grails Performance Poppendieck Lean Testing Scala JavaOne SOA fitnesse Semantic Web Groovy IntelliJ Introduction to Agile Architecture Agile Functional Programming Seam Xebia Scrum product owner Maven Agile Awareness Workshop Ajax Spring XML Hibernate
medicin depression buy phentermine without a perscription aricept generic hair loss help how do you prevent bone loss urinary tract infection symptoms viagra sex domination cialis viagra cures for throat infection buy sumycin acne care new medication for cancer treatment help for sleeping problems on-line pharmacies cure snoring medications to help clot blood what is aspirin buy zestoretic bronchitis vs pneumonia back pain muscle acne face medication muscle women pain behind knee fat blocker man health arthritis natural cure woman health women insomnia cheap phentermine online cats and irritable bowel syndrome buy cialis generic online nutritional diet for osteoporosis abnormal blood clots treatments for hair loss what is zyprexa dental whitening products impotence herbs drugs for diabetes allergy prevention buy canada levitra Mentax adhd in children hair loss in woman medicines for blood clot online imitrex viagra buy free dog products clindamycin drug how to stop hair loss chloramphenicol discount drug viagra what valium does permanent hair loss heart failure medicine avapro 150mg ordering viagra online food allergies order viagra online online viagra prescription carisoprodol mg improve your skin discount erectile dysfunction medication buy xanax online buy order viagra scabies teatments information allegra vitamine b1 diazepam breast cancer support free stop smoking cipro side effects ultram cheapest treatment attention deficit disorder discount vitamins supplements how to get viagra online synthroid buy cheapest cialis zyrtec online how to clear acne preventive osteoporosis immune stimulants what is hoodia On Line Viagra getting over the pain diflucan dosage health asthma online stores hair loss products blood clot drugs colon parasites hair loss products discount medicine pravastatin buy griseofulvin tablets order indomethacin dog health products how to take a beta-blocker diazapan is valium treating cold sores chronic pain drug what is osteoporosis stress drug tooth whitening lowering cholesterol naturally legality of buying cialis online order levitra treatment for insomnia cheapest cialis index depakote overdose alprazolam condom sales treatment of yeast infection xanax sales taking viagra after cialis how to control pain new birth control chest pain health prozac prescription blood clots viagra in mexico chlamydia pill cancer drugs cold flu drugs how do i order viagra online super viagra acyclovir medicine benadryl dosage erythromycin pregnancy buy contoured condom chronic muscle pain pet health dogs treatment attention deficit disorder dental teeth whitening asthma medicine free prescription drugs herpes drug diabetes treatment buy tooth whitening gel cheap fast valium generic levitra buy cheapest viagra online lopressor drug pharmacy drug prices ultram dosing treatments for bipolar disorder neurontin withdrawal parasite medication chlamydia tips for increasing breast size ways to enhance breast what is valium used for metformin tablet order birth control hair loss for men how does xanax work treatment hepatitis c rythmol cheap acai antioxidants nexium generic blood pressure pills levitra online no prescription Levitra Online medications on line motion sickness drugs bactrim online order roxithromycin nicotine where can i order viagra immune supplements buy erexin v bph prostate allopurinol xanax for depression drug new smoking stop cheap impotence drug generic cialis delivery new treatment for depression antibiotics for cat viagra china alternative medicine cholesterol viagra dose anxiety disorder treatment severe muscle pain treatment of cancer calcium carbonate penis enlargement without pill valium maximum dosage reasons for high blood pressure energy product breast enlargement info cheap effexor building your body wrinkle cream aricept dosage alpha blocker increasing female sex drive valium depression new pain meds no rx xanax drug trileptal mg imitrex avapro 150mg medicine drugs contraception female claritin pill medication for acne med orders buy viagra internet levitra effect treatment for blood clots order sominex buy creatine buy precose cheap viagra overnight lopressor drug body building info health drugs general health and medical what is diazepam eye infections in dogs online prescription pills diclofenac tablet new medication anxiety buy citalopram medication male enhancement enhancement fat blocker medicine for throat infection order cardizem about soma health remedies for dogs generic xanax cheap zyrtec for depression medicine viagra sex domination buy acne skin care product hypnosis help study cure vaginal yeast infection weight loss supplement program muscle pain in leg how to increase erection buy viagra what is cla augmentin doses gaining muscle mass health med online heart rate treatments lopressor drug dog ear canal phentermine without prescription viagra order online weight loss glipizide diabetes astelin generic fat blocker buy gel tooth whitening cheap wellbutrin online weight loss program buy antiox anti-biotics acne skin treatmen tramadole vpxl pill drugs affecting levitra immune system support augmentin hypothyroidism medication buy erexin v uy prescription medication without a prescription buy discount order osteo arthritis online buy pilocarpine cheapest place to buy phentermine parasite treatment impotence help body fat loss viagra herb alternative constipation supplements treatment dementia adhd and medications muscle spasm relief viagra online cheap relieve upper back pain stop hair loss discount viagra online menstrual cycle problems antifungal shampoo side effects ativan gabapentin medication where can i buy viagra diazepam buy soma online clonidine dosage viagra gel top hair loss fast antibiotics cure chlamydia skin fungal infections drug zofran give up smoking alternative medicine cholesterol sleeping help best online viagra scams prednisone 10mg viagra sex domination lotensin easy weight loss pain meds without prescription over the counter drugs new high blood pressure medic generic compazine cetirizine drug order phentermine best fat blockers woman enhancement supplement drug zofran buy precose new drug treatment for cancer how to increase fertility viagra in australia benadryl dosing buy alcoholism medications order l arginine buy diazepam generic for ativan ativan prescription drugs weight loss treatment for chest pain woman health where can i buy phentermine online skin fungal infection give up smoking viagra on line hoodia information how does osteoporosis occur buy viagra online buy alcoholism medications depakote overdose klonopin pill tetracycline capsules what is high blood pressure bladder control for dogs generic for lipitor glucophage online pharmacy gabapentin dosage treating yeast infections dog health info cymbalta anxiety cheap tramadol without prescription hydrea drugs used for cancer cure for high blood pressure alcohol and valium relief from constipation liver infection treatment cialis soft zantac medication help sleep problems all natural antibiotics order medication without prescription sleep problems free hypnotherapy gaining muscle mass cheap viagra order online natural help for pain how to buy viagra drug price celebrex information otc diuretic levitra 10 mg buy medicine online pets products relief foot pain cialis without prescription med care cheapest generic cialis rapid hair loss pain medications generic side effects meds without prescriptions cat anxiety buy simplicef natural cure arthritis effects of high blood pressure lowest price generic viagra how to get birth control new breast cancer drug buy topamax blood pressure meds when are beta blockers prescribed how to get pain meds order fosamax online viagra name order viagra viagra cialis cat's eye health how to relieve lower back pain treating ear infections diazapan is valium online pain doctors high blood pressure in elderly medication to stop smoking wellbutrin dosages diabetes blood sugar levels weight loss diet pill side effects of prescribed pain pills drug list high blood pressure buy cialis online in usa ultram cost how to help osteoporosis how to use clomid discount brand viagra wellbutrin cymbalta buy pills without a prescription buy pain medicine online tab tramadol depression symptoms treatment how levitra work hypertension medications beta blockers prevent premature ejaculation xanax interactions with other medicines purchase medicine on line does alli work xenical mexico prescriptions buy sumycin uy prescription medication without a prescription ambien cost methocarbamol effects cheap beta blockers cats bladder reduce cholesterol naturally metformin tablet scabies medicine breast enhancer pills body building over 50 order viagra cheap zestril medication how to buy prescription medications online pharma kamagra drugs depression ear infection symptoms big muscle controlling blood pressure pain meds and pregnancy buy diazepam without prescription skin allergies antibiotic zoloft buy weight loss nutrition program Buy Cialis breast increase meds without prescriptions blood clots medical edema treatment for flu best hangover remedy diabetes drugs