Spring Integration 的 Groovy DSL

工程 | David Turanski | 2012 年 11 月 6 日 | ...

Spring Integration 使用 Spring 编程模型实现了 企业集成模式,从而在基于 Spring 的应用程序中实现消息传递。Spring Integration 还通过支持 JMS、HTTP、AMQP、TCP、FTP(s)、SMTP 等的声明式适配器提供与外部系统的集成。目前,消息流的配置主要通过 Spring XML 完成,Spring Integration 支持多个命名空间以使其尽可能简洁。今年早些时候,SpringSource 发布了 Spring Integration 的 Scala DSL。现在,我们很高兴地宣布 Groovy DSL 的第一个里程碑版本 (1.0.0.M1)。

这两个 DSL 都有一个共同的目标——为 Spring Integration 提供强大而灵活的 XML 配置替代方案。这两种语言在语义上也相似,因为 Groovy DSL 借鉴了 Scala DSL 引入的概念。此外,两者本质上都是 Spring Integration 的一个门面。然而,相似之处仅此而已。许多差异可归因于 Scala 和 Groovy 之间的语言差异,最显著的是静态与动态类型。Groovy DSL 主要面向 Groovy 程序员,他们对基于该 DSL 的 构建器模式 的分层语法感到自在。这对于 Java 开发人员也很有吸引力,他们可以利用 DSL 提供的丰富功能,并且会发现该语法非常易于理解。

Hello World

让我们从一个 Groovy 示例开始

def builder = new IntegrationBuilder()

def flow = builder.messageFlow {
   transform {"hello, $it"}
   handle {println it}
}

flow.send('world')

这会创建一个 Spring 应用程序上下文,构建一个 Spring Integration 消息流,该消息流由一个转换器 (transform) 和一个服务激活器 (handle) 组成,并使用直接通道连接这些端点,以便它们按顺序执行。转换器将消息负载(在这种情况下是“world”)附加到字符串“hello, ”,服务激活器将结果打印到标准输出。瞧!这里我们看到了构建器模式的一个简单实例。对于不熟悉 Groovy 的人来说,这是有效的 Groovy 语法。messageFlowtransformhandle 都是 DSL 定义的方法。{} 是 Groovy 的闭包语法。由于括号和分号在 Groovy 中是可选的,这等同于


def flow = builder.messageFlow({
   transform({"hello, $it"});
   handle({println(it)});
});

此外,我应该提到,Groovy 闭包默认期望一个名为 'it' 的参数。闭包参数可以命名并可选地进行类型化。例如


   transform {String payload -> "hello,  $payload"}

还有一件事。Groovy 允许您在双引号字符串中嵌入变量表达式。这并不是一个 Groovy 教程,但足以说明 Groovy 的所有语法糖使得代码非常甜美、简洁和可读。顺便说一句,这对应的 XML 是


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:si="http://www.springframework.org/schema/integration" 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 http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
   
   <si:transformer id="t1" input-channel="flow1.inputChannel" output-channel="sa1.inputChannel" expression="'Hello,' + payload"/>
   <si:service-activator id="sa1" input-channel="sa.inputChannel" expression = "T(java.lang.System).out.println(payload)"/>
</beans>

然后我们必须编写大约十行代码来初始化 Spring 应用程序上下文并发送消息。

要在 Java 中运行 DSL 示例,有几种选择。一种方法是加载并运行外部 DSL 脚本

HelloWorld.groovy


 messageFlow {
    transform {"hello, $it"}
    handle {println it}
 }
 

Main.java


public class Main {
    public static void main(String[] args) {
          IntegrationBuilder builder = new IntegrationBuilder();
          MessageFlow flow = (MessageFlow) builder.build(new File("HelloWorld.groovy"));
          flow.send("world");
    }
}

除了上面所示的 File 实例外,build() 方法还接受 InputStream、GroovyCodeSource、Spring Resource,甚至 groovy.lang.Script。因此,如果您使用 Groovy 编译器编译您的项目,您可以执行类似这样的操作,其中 HelloWorld 是 groovy.lang.Script 的一个实例。


public class Main {
    public static void main(String[] args) {
          IntegrationBuilder builder = new IntegrationBuilder();
          MessageFlow flow = (MessageFlow) builder.build(new HelloWorld()));
          flow.send("world");
    }
}

下一步

希望这个简短的介绍说明了 DSL 的易用性。如果您想更深入地了解,spring-integration-dsl-groovy Github 仓库 包含一份 DSL 用户指南,其中包含大量示例,更详细地描述了 DSL 的功能。

获取 Spring 新闻通讯

通过 Spring 新闻通讯保持联系

订阅

领先一步

VMware 提供培训和认证,助您加速进步。

了解更多

获得支持

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支持和二进制文件,只需一份简单的订阅。

了解更多

即将举行的活动

查看 Spring 社区所有即将举行的活动。

查看所有