spring-attic / spring-integration-dsl-groovy

Groovy DSL for Spring Integration
Apache License 2.0
56 stars 40 forks source link

spring-integration-dsl-groovy is no longer actively maintained by VMware, Inc.

Groovy DSL For Spring Integration

Overview

This project implements a Groovy DSL for Spring Integration. Coming on the heels of the Scala DSL for Spring Integration, I have incorporated some of the same basic ideas and vocabulary however this has been developed independently with a primary focus on providing Groovy and Java developers a simple, flexible, and powerful alternative to XML configuration for Spring Integration applications. Please refer to the DSL User's Guide and other pages on the project wiki for more details.

Features

Implementation

The DSL uses Groovy Builder pattern so the syntax will be familiar to Groovyists. The central class is the IntegrationBuilder which extends FactoryBuilderSupport framework to create a Spring Integration domain model which is translated directly to Spring XML to create a Spring Application Context.

The main benefit of this approach is that the DSL can leverage existing Spring Integration namespace parsers to perform all the necessary validation and bean definition processing. Since this code is tightly coupled to XML parsing, the bean definitions and would otherwise need to be entirely replicated for the DSL. Another advantage is that if the log level is set to DEBUG, you can inspect the generated XML on the console which will give you more insight into how the DSL interprets things. Finally it makes it very easy for the IntegrationBuilder to inject XML builder markup, providing seamless integration with Spring XML.

Example

The following is a simple example in Groovy.

def builder = new IntegrationBuilder()

def flow = builder.messageFlow {
 transform {payload->payload.toUpperCase()}
 filter {payload-> payload =="HELLO"}
 handle {payload->payload}
}

assert flow.sendAndReceive("hello") == "HELLO"
assert flow.sendAndReceive("world") == null

This flow can also be executed from a Java class. The easiest way is to create an external file or classpath resource. The equivalent to the above example looks like :

 messageFlow {
     transform {payload->payload.toUpperCase()}
     filter {payload-> payload =="HELLO"})
     handle {payload->payload})
  }

If we save this to a file named 'messageFlow1.groovy', it may be accessed from a Java class as follows:

IntegrationBuilder builder = new IntegrationBuilder();

MessageFlow flow = (MessageFlow)builder.build(
    new FileInputStream("messageFlow1.groovy"));

flow.sendAndReceive("hello");

In addition to InputStream the API accepts other sources such as a compiled groovy.lang.Script and groovy.lang.GroovyCodeSource

The DSL User's Guide contains a lot more examples to get you started. Also take a look at the spring-integration-dsl-groovy-samples subproject as well as the unit tests included in the various sub projects.

Project Structure

The DSL structure mirrors Spring Integration itself. The core module provides the core components and language framework. Protocol adapters, e.g., http, jms, are maintained as separate subprojects. In addition there is a samples module to help get you started. Each adapter module plugs in to the DSL framework to provide extensions needed to support the associated adapters. At this time, there are only a few adapters implemented for the DSL, but this is expected to grow according to demand, community contributions, etc.

Even if an adapter is not implemented directly in the DSL, you can use Groovy markup to drop in native Spring XML. Please refer to the DSL User's Guide for more information.

Contributing

With the inital core DSL features in place, there remains much to be done to support the constantly growing collection of protocol adapters covered by Spring Integration. Community contributions are certainly welcome! If this interests you, please refer to the DSL Developer's Guide for more information.

Here are some ways for you to get involved in the community: