Skip to content

Instantly share code, notes, and snippets.

@danveloper
Created October 10, 2013 18:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danveloper/6922754 to your computer and use it in GitHub Desktop.
Save danveloper/6922754 to your computer and use it in GitHub Desktop.
An example of using the Spring Integration Groovy DSL with a JMS channel adapter to an embedded ActiveMQ JMS broker
package com.danveloper.springboot
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.integration.dsl.groovy.IntegrationContext
import org.springframework.integration.dsl.groovy.builder.AbstractIntegrationBuilderModuleSupport
import org.springframework.integration.dsl.groovy.builder.IntegrationBuilder
import org.springframework.integration.dsl.groovy.builder.dom.IntegrationDomSupport
import org.springframework.integration.dsl.groovy.builder.dom.XMLNamespaceSupport
import org.springframework.jms.core.JmsTemplate
import org.springframework.jms.core.MessageCreator
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.jms.Session
@Configuration
@Controller
@EnableAutoConfiguration
class Application {
@Autowired
JmsTemplate jmsTemplate
public static void main(String[] args) {
SpringApplication.run(Application, args)
}
@RequestMapping("/")
@ResponseBody String home() {
jmsTemplate.send("log.*",
{ Session session -> session.createTextMessage("Sending Message!") } as MessageCreator)
}
@Bean
IntegrationContext integrationContext(ApplicationContext ctx) {
def builder = new IntegrationBuilder()
builder.integrationContext.moduleSupportInstances = [new AmqIntegrationBuilderModuleSupport()]
builder.integrationContext.parent = ctx
builder.doWithSpringIntegration {
namespaces "int-jms"
springXml {
'amq:broker'(useJmx: false, persistent: false) {
'amq:transportConnectors'() {
'amq:transportConnector'(uri: 'tcp://localhost:61616')
}
}
bean(id: 'amqConnectionFactory', 'class': 'org.apache.activemq.ActiveMQConnectionFactory') {
property(name: 'brokerURL', value: 'vm://localhost')
}
bean(id: 'msgSender', 'class': 'org.springframework.jms.core.JmsTemplate') {
'constructor-arg'(index: '0', ref: 'amqConnectionFactory')
}
bean(id: 'connectionFactory', 'class': 'org.springframework.jms.connection.CachingConnectionFactory') {
property(name: 'targetConnectionFactory', ref: 'amqConnectionFactory')
property(name: 'sessionCacheSize', value: '10')
property(name: 'cacheProducers', value: 'false')
}
bean(id: 'logMessageDestination', 'class': 'org.apache.activemq.command.ActiveMQTopic') {
'constructor-arg'(index: '0', value: 'log.*')
}
bean(id: 'logServiceActivator', 'class':'com.danveloper.springboot.LogServiceActivator')
'int-jms:message-driven-channel-adapter'(id: 'logsIn', destination: 'logMessageDestination', channel: 'logChannel')
}
messageFlow(inputChannel: 'logChannel') {
route('logRouter', { ['log-input'] })
}
handle(inputChannel: 'log-input', ref: 'logServiceActivator', method: 'log')
}
builder.integrationContext
}
class AmqIntegrationBuilderModuleSupport extends AbstractIntegrationBuilderModuleSupport {
@Override
void registerBuilderFactories(IntegrationBuilder builder) {}
@Override
void registerDomBuilders(IntegrationDomSupport integrationDomSupport) {}
@Override
void registerNamespaces(XMLNamespaceSupport namespaceSupport) {
namespaceSupport.addNamespace(
'amq',
'http://activemq.apache.org/schema/core',
'http://activemq.apache.org/schema/core/activemq-core.xsd')
}
}
}
apply plugin: 'groovy'
apply plugin: 'idea'
repositories {
mavenCentral()
maven { url "http://repo.spring.io/milestone" }
mavenLocal()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.1.6'
compile 'org.springframework.boot:spring-boot:0.5.0.M5'
compile 'org.springframework.boot:spring-boot-starter-web:0.5.0.M5'
compile 'org.springframework.boot:spring-boot-starter-integration:0.5.0.M5'
compile 'org.springframework.integration:spring-integration-dsl-groovy-core:1.0.0.M1'
compile 'org.springframework.integration:spring-integration-dsl-groovy-jms:1.0.0.M1'
compile 'org.springframework.integration:spring-integration-jms:2.2.0.RELEASE'
compile 'org.apache.activemq:activemq-core:5.7.0'
compile 'org.apache.xbean:xbean-spring:3.12'
testCompile 'junit:junit:4.10'
}
package com.danveloper.springboot
import org.springframework.stereotype.Service
/**
* User: danielwoods
* Date: 10/10/13
*/
@Service
class LogServiceActivator {
public void log(msg) {
println "Recevied a msg: $msg"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment