Permalink
Browse files

add a Channels examples

  • Loading branch information...
1 parent 4da3a71 commit 56502b8f5ec9c29d949f767d42aa058e362b4177 @felipeg48 committed Feb 19, 2013
@@ -0,0 +1,25 @@
+apply plugin: 'groovy'
+
+ group = 'spring-examples'
+version = '0.1.0-SNAPSHOT'
+
+task "create-dirs" << {
+ sourceSets*.groovy.srcDirs*.each { it.mkdirs() }
+ sourceSets*.resources.srcDirs*.each { it.mkdirs() }
+}
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+
+ groovy 'org.codehaus.groovy:groovy-all:2.0.6'
+ compile 'org.springframework:spring-context:3.2.0.RELEASE'
+ runtime 'log4j:log4j:1.2.17'
+
+ testCompile 'junit:junit:4.8.2'
+ testCompile 'org.springframework:spring-test:3.2.0.RELEASE'
+ testCompile 'org.springframework.integration:spring-integration-core:2.2.1.RELEASE'
+ testCompile 'org.springframework.integration:spring-integration-stream:2.2.1.RELEASE'
+}
@@ -0,0 +1,17 @@
+/**
+ *
+ */
+package com.itprosmx.spring.integration
+
+import groovy.transform.ToString
+
+/**
+ * @author felipeg
+ *
+ */
+@ToString(includeFields=true)
+class SimpleModel {
+ String name
+ String phone
+ Date birthday
+}
@@ -0,0 +1,20 @@
+/**
+ *
+ */
+package com.itprosmx.spring.integration
+
+import org.springframework.integration.Message
+
+/**
+ * @author felipeg
+ *
+ */
+class SimpleModelComparator implements Comparator<Message<SimpleModel>> {
+
+ @Override
+ int compare(Message<SimpleModel> model, Message<SimpleModel> otherModel) {
+ //Compare the models based on the Date. Seniors must be first
+ return model.payload.birthday.compareTo(otherModel.payload.birthday)
+ }
+
+}
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:int="http://www.springframework.org/schema/integration"
+ xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
+ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
+ http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.2.xsd">
+
+
+ <!-- p2pChannelTest -->
+ <int:channel id="messageChannel">
+ <int:queue capacity="10" />
+ </int:channel>
+
+ <!-- pubsubChannelTest -->
+ <int:publish-subscribe-channel id="pubsubChannel"/>
+
+ <!-- priorityChannelTest -->
+ <int:channel id="priorityChannel">
+ <int:priority-queue capacity="10" />
+ </int:channel>
+
+ <!-- priorityWithComparatorChannelTest -->
+ <bean id="simpleModelComparator" class="com.itprosmx.spring.integration.SimpleModelComparator" />
+ <int:channel id="priorityWithComparatorChannel">
+ <int:priority-queue capacity="10" comparator="simpleModelComparator"/>
+ </int:channel>
+
+ <!-- rendezvousChannelTest -->
+ <int:channel id="rendezvousChannel">
+ <int:rendezvous-queue/>
+ </int:channel>
+</beans>
@@ -0,0 +1,159 @@
+/**
+ *
+ */
+package com.itprosmx.spring.integration
+
+import static org.junit.Assert.*
+
+import org.junit.Ignore
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.integration.Message
+import org.springframework.integration.MessageChannel
+import org.springframework.integration.MessageHeaders
+import org.springframework.integration.channel.PublishSubscribeChannel
+import org.springframework.integration.channel.QueueChannel
+import org.springframework.integration.core.MessageHandler
+import org.springframework.integration.message.GenericMessage
+import org.springframework.integration.support.MessageBuilder
+import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
+
+/**
+ * @author felipeg
+ *
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration("/META-INF/spring/channels-context.xml")
+class ChannelTest {
+
+ @Autowired
+ MessageChannel messageChannel
+
+ @Autowired
+ MessageChannel pubsubChannel
+
+ @Autowired
+ MessageChannel priorityChannel
+
+ @Autowired
+ MessageChannel priorityWithComparatorChannel
+
+ @Test
+ @Ignore
+ void p2pChannelTest() {
+ //Producer
+ //Step 1. Build the Message
+ Message<String> message = new GenericMessage<String>("Simple Message")
+ assertNotNull message
+
+ //Step 2. Send the Message
+ messageChannel.send message
+
+ //Consumer
+ //Step 3. Received the Message
+ Message msgReceived = ((QueueChannel) messageChannel).receive(10000)
+ assertNotNull msgReceived
+ println "P2P Message (Received): ${msgReceived.payload}"
+ }
+
+ @Test
+ @Ignore
+ void pubsubChannelTest(){
+ //Consumer
+ //Step 1. Subscribe to the Channel
+ ((PublishSubscribeChannel)pubsubChannel).
+ subscribe([
+ handleMessage: { msg ->
+ println "PubSub Message (Received): $msg"
+ }
+ ] as MessageHandler)
+
+
+ //Producer
+ //Step 2. Build the Message
+ Message<String> message = new GenericMessage<String>("Simple Message")
+ assertNotNull message
+
+ //Step 3. Send the Message
+ pubsubChannel.send message
+ }
+
+ @Test
+ @Ignore
+ void priorityChannelTest(){
+
+ //Step 0. Some variables
+ String payloadPriorityLow = "Low Priority"
+ String payloadPriorityHigh = "High Priority"
+
+ //Producer
+ //Step 1. Build the Message and send
+ Message<String> message = MessageBuilder.withPayload(payloadPriorityLow).setHeader(MessageHeaders.PRIORITY, 1).build();
+ assertNotNull message
+ println "Sending Low Priority Message, with value 1"
+ priorityChannel.send message
+
+ //Step 2. Send another message with higher priority
+ message = MessageBuilder.withPayload(payloadPriorityHigh).setHeader(MessageHeaders.PRIORITY, 10).build();
+ assertNotNull message
+ println "Sending High Priority Message, with value 10"
+ priorityChannel.send message
+
+ //Consumer
+ //Step 3. Received the Messages
+ Message msgReceived = ((QueueChannel) priorityChannel).receive(10000)
+ assertNotNull msgReceived
+ println "Message (Received): ${msgReceived.payload}"
+ assertEquals payloadPriorityHigh,msgReceived.payload
+
+ msgReceived = ((QueueChannel) priorityChannel).receive(10000)
+ assertNotNull msgReceived
+ println "Message (Received): ${msgReceived.payload}"
+ assertEquals payloadPriorityLow,msgReceived.payload
+
+ }
+
+ @Test
+ @Ignore
+ void priorityWithComparatorChannelTest(){
+ //Step 0. Model
+ def payloadPriorityLow = new SimpleModel(name:"Doe",phone:"1-800-VMWARE",birthday:Date.parse("yyyy-mm-dd","1990-08-22"))
+ def payloadPriorityHigh = new SimpleModel(name:"John",phone:"1-800-SPRING",birthday:Date.parse("yyyy-mm-dd","1980-08-22"))
+
+ //Producer
+ //Step 1. Build the Message and send
+ Message<SimpleModel> message = new GenericMessage<SimpleModel>(payloadPriorityLow)
+ assertNotNull message
+ println "Sending Low Priority Message -> $payloadPriorityLow"
+ priorityWithComparatorChannel.send message
+
+ //Step 2. Send another message with higher priority
+ message = new GenericMessage<SimpleModel>(payloadPriorityHigh)
+ assertNotNull message
+ println "Sending High Priority Message -> $payloadPriorityHigh"
+ priorityWithComparatorChannel.send message
+
+
+ println "Seniors must go first"
+
+ //Consumer
+ //Step 3. Received the Messages
+ Message msgReceived = ((QueueChannel) priorityWithComparatorChannel).receive(10000)
+ assertNotNull msgReceived
+ println "Message (Received): ${msgReceived.payload}"
+ assertEquals payloadPriorityHigh.name,msgReceived.payload.name
+
+ msgReceived = ((QueueChannel) priorityWithComparatorChannel).receive(10000)
+ assertNotNull msgReceived
+ println "Message (Received): ${msgReceived.payload}"
+ assertEquals payloadPriorityLow.name,msgReceived.payload.name
+ }
+
+ @Test
+ @Ignore
+ void rendezvousChannelTest(){
+
+ }
+}
@@ -27,7 +27,7 @@ class SimpleTest {
@Test
void simpleTest() {
- Message<String> message = new GenericMessage<String>("Simple Message");
+ Message<String> message = new GenericMessage<String>("Simple Message")
assertNotNull message
messageChannel.send message

0 comments on commit 56502b8

Please sign in to comment.