Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Chapter 15 - examples

  • Loading branch information...
commit 6979d426e84d21e33354bd92f76d34d01ccdc62c 1 parent 2744a86
@mbogoevici mbogoevici authored
Showing with 466 additions and 0 deletions.
  1. +50 −0 siia-examples-parent/concurrency/pom.xml
  2. +16 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/AssembledCar.java
  3. +20 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/AssemblyLine.java
  4. +32 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/Counter.java
  5. +18 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/FinishedCar.java
  6. +18 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/PaintShop.java
  7. +17 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/PieceKit.java
  8. +9 −0 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/SupplyInput.java
  9. +32 −0 siia-examples-parent/concurrency/src/main/resources/paint-shop-concurrent.xml
  10. +28 −0 siia-examples-parent/concurrency/src/main/resources/paint-shop.xml
  11. +33 −0 siia-examples-parent/concurrency/src/test/java/com/manning/siia/pipeline/PaintShopConcurrentTests.java
  12. +35 −0 siia-examples-parent/concurrency/src/test/java/com/manning/siia/pipeline/PaintShopDirectTests.java
  13. +19 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/AdvancedPollerTests.java
  14. +19 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/CronPollerTests.java
  15. +19 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/PollerDefaultAndCustomTest.java
  16. +14 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubComparator.java
  17. +16 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubFilter.java
  18. +15 −0 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubService.java
  19. +13 −0 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/AdvancedPollerTests-context.xml
  20. +12 −0 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/CronPollerTests-context.xml
  21. +30 −0 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/PollerDefaultAndCustomTest-context.xml
  22. +1 −0  siia-examples-parent/pom.xml
View
50 siia-examples-parent/concurrency/pom.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>siia-parent</artifactId>
+ <groupId>com.manning.siia</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>concurrency</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.integration</groupId>
+ <artifactId>spring-integration-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.integration</groupId>
+ <artifactId>spring-integration-file</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aop</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-tx</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ </dependency>
+ </dependencies>
+</project>
View
16 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/AssembledCar.java
@@ -0,0 +1,16 @@
+package com.manning.siia.pipeline;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class AssembledCar {
+ private long serialNumber;
+
+ public AssembledCar(long serialNumber) {
+ this.serialNumber = serialNumber;
+ }
+
+ public long getSerialNumber() {
+ return serialNumber;
+ }
+}
View
20 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/AssemblyLine.java
@@ -0,0 +1,20 @@
+package com.manning.siia.pipeline;
+
+import org.springframework.integration.annotation.ServiceActivator;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class AssemblyLine {
+
+ @ServiceActivator
+ public AssembledCar assemble(PieceKit pieceKit) {
+ try {
+ Thread.sleep(1000l);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ return new AssembledCar(pieceKit.getSerialNumber());
+ }
+
+}
View
32 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/Counter.java
@@ -0,0 +1,32 @@
+package com.manning.siia.pipeline;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.springframework.integration.annotation.ServiceActivator;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class Counter {
+
+ private int count = 0;
+
+ private CountDownLatch latch = new CountDownLatch(1);
+
+ public synchronized void resetLatch(int latchSize) {
+ latch = new CountDownLatch(latchSize);
+ }
+
+ @ServiceActivator
+ public void count(FinishedCar assembledCar) {
+ count++;
+ latch.countDown();
+ System.out.println("Assembled car " + assembledCar.getSerialNumber());
+ }
+
+ public int getCount() throws InterruptedException {
+ latch.await(30, TimeUnit.SECONDS);
+ return count;
+ }
+}
View
18 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/FinishedCar.java
@@ -0,0 +1,18 @@
+package com.manning.siia.pipeline;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class FinishedCar {
+
+ private long serialNumber;
+
+
+ public FinishedCar(long serialNumber) {
+ this.serialNumber = serialNumber;
+ }
+
+ public long getSerialNumber() {
+ return serialNumber;
+ }
+}
View
18 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/PaintShop.java
@@ -0,0 +1,18 @@
+package com.manning.siia.pipeline;
+
+import org.springframework.integration.annotation.ServiceActivator;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class PaintShop {
+ @ServiceActivator
+ public FinishedCar assemble(AssembledCar assembledCar) {
+ try {
+ Thread.sleep(1000l);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ return new FinishedCar(assembledCar.getSerialNumber());
+ }
+}
View
17 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/PieceKit.java
@@ -0,0 +1,17 @@
+package com.manning.siia.pipeline;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class PieceKit {
+
+ private long serialNumber;
+
+ public PieceKit(long serialNumber) {
+ this.serialNumber = serialNumber;
+ }
+
+ public long getSerialNumber() {
+ return serialNumber;
+ }
+}
View
9 siia-examples-parent/concurrency/src/main/java/com/manning/siia/pipeline/SupplyInput.java
@@ -0,0 +1,9 @@
+package com.manning.siia.pipeline;
+
+/**
+ * @author Marius Bogoevici
+ */
+public interface SupplyInput {
+
+ void buildCar(PieceKit pieceKit);
+}
View
32 siia-examples-parent/concurrency/src/main/resources/paint-shop-concurrent.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://www.springframework.org/schema/integration"
+ 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">
+
+ <poller default="true" fixed-rate="500"/>
+
+ <channel id="input"/>
+ <channel id="paint">
+ <queue capacity="10"/>
+ </channel>
+ <channel id="output"/>
+
+ <gateway id="supplyInput"
+ service-interface="com.manning.siia.pipeline.SupplyInput"
+ default-request-channel="input"/>
+
+ <service-activator id="assemblyLine" input-channel="input"
+ output-channel="paint">
+ <beans:bean
+ class="com.manning.siia.pipeline.AssemblyLine"/>
+ </service-activator>
+ <service-activator id="paintShop" input-channel="paint"
+ output-channel="output">
+ <beans:bean class="com.manning.siia.pipeline.PaintShop"/>
+ </service-activator>
+ <service-activator id="exitCounter" input-channel="output"
+ ref="counter"/>
+ <beans:bean id="counter"
+ class="com.manning.siia.pipeline.Counter"/>
+</beans:beans>
View
28 siia-examples-parent/concurrency/src/main/resources/paint-shop.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://www.springframework.org/schema/integration"
+ 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">
+
+ <channel id="input"/>
+ <channel id="paint"/>
+ <channel id="output"/>
+
+ <gateway id="supplyInput"
+ service-interface="com.manning.siia.pipeline.SupplyInput"
+ default-request-channel="input"/>
+
+ <service-activator id="assemblyLine" input-channel="input"
+ output-channel="paint">
+ <beans:bean
+ class="com.manning.siia.pipeline.AssemblyLine"/>
+ </service-activator>
+ <service-activator id="paintShop" input-channel="paint"
+ output-channel="output">
+ <beans:bean class="com.manning.siia.pipeline.PaintShop"/>
+ </service-activator>
+ <service-activator id="exitCounter" input-channel="output"
+ ref="counter"/>
+ <beans:bean id="counter"
+ class="com.manning.siia.pipeline.Counter"/>
+</beans:beans>
View
33 siia-examples-parent/concurrency/src/test/java/com/manning/siia/pipeline/PaintShopConcurrentTests.java
@@ -0,0 +1,33 @@
+package com.manning.siia.pipeline;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author Marius Bogoevici
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration("classpath:paint-shop-concurrent.xml")
+public class PaintShopConcurrentTests {
+
+ @Autowired SupplyInput supplyInput;
+
+ @Autowired Counter counter;
+
+ @Test
+ public void testBuild() throws Exception {
+
+ counter.resetLatch(10);
+
+ for (int i=0; i < 10; i++ ) {
+ PieceKit pieceKit = new PieceKit(i);
+ supplyInput.buildCar(pieceKit);
+ }
+
+ Assert.assertEquals(10, counter.getCount());
+ }
+}
View
35 siia-examples-parent/concurrency/src/test/java/com/manning/siia/pipeline/PaintShopDirectTests.java
@@ -0,0 +1,35 @@
+package com.manning.siia.pipeline;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.integration.MessageChannel;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author Marius Bogoevici
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration("classpath:paint-shop.xml")
+public class PaintShopDirectTests {
+
+ @Autowired SupplyInput supplyInput;
+
+ @Autowired Counter counter;
+
+ @Test
+ public void testBuild() throws Exception {
+
+ counter.resetLatch(10);
+
+ for (int i=0; i < 10; i++ ) {
+ PieceKit pieceKit = new PieceKit(i);
+ supplyInput.buildCar(pieceKit);
+ }
+
+ Assert.assertEquals(10, counter.getCount());
+ }
+}
View
19 siia-examples-parent/concurrency/src/test/java/siia/concurrency/AdvancedPollerTests.java
@@ -0,0 +1,19 @@
+package siia.concurrency;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author Marius Bogoevici
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+public class AdvancedPollerTests {
+
+ @Test
+ public void testContextLoads() throws Exception {
+
+ }
+}
View
19 siia-examples-parent/concurrency/src/test/java/siia/concurrency/CronPollerTests.java
@@ -0,0 +1,19 @@
+package siia.concurrency;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author Marius Bogoevici
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+public class CronPollerTests {
+
+ @Test
+ public void testContextLoads() throws Exception {
+
+ }
+}
View
19 siia-examples-parent/concurrency/src/test/java/siia/concurrency/PollerDefaultAndCustomTest.java
@@ -0,0 +1,19 @@
+package siia.concurrency;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author Marius Bogoevici
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+public class PollerDefaultAndCustomTest {
+
+ @Test
+ public void testContextLoads() throws Exception {
+
+ }
+}
View
14 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubComparator.java
@@ -0,0 +1,14 @@
+package siia.concurrency;
+
+import java.util.Comparator;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class StubComparator implements Comparator<Object> {
+
+ @Override
+ public int compare(Object o, Object o1) {
+ return 0;
+ }
+}
View
16 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubFilter.java
@@ -0,0 +1,16 @@
+package siia.concurrency;
+
+import java.util.List;
+
+import org.springframework.integration.file.filters.FileListFilter;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class StubFilter implements FileListFilter {
+
+ @Override
+ public List filterFiles(Object[] files) {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
View
15 siia-examples-parent/concurrency/src/test/java/siia/concurrency/StubService.java
@@ -0,0 +1,15 @@
+package siia.concurrency;
+
+import org.springframework.integration.annotation.ServiceActivator;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class StubService<I,O> {
+
+ @ServiceActivator
+ public O handle(I input) {
+ return null;
+ }
+
+}
View
13 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/AdvancedPollerTests-context.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://www.springframework.org/schema/integration"
+ 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"
+ >
+
+ <poller id="defaultPoller" fixed-rate="10000" default="true"
+ max-messages-per-poll="1" receive-timeout="2000"/>
+</beans:beans>
View
12 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/CronPollerTests-context.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://www.springframework.org/schema/integration"
+ xsi:schemaLocation=
+ "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
+ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
+ xmlns:file="http://www.springframework.org/schema/integration/file">
+
+ <poller id="defaultPoller" default="true" cron="0 0 0 * * *" />
+</beans:beans>
View
30 siia-examples-parent/concurrency/src/test/resources/siia/concurrency/PollerDefaultAndCustomTest-context.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://www.springframework.org/schema/integration"
+ xsi:schemaLocation=
+ "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
+ http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
+ xmlns:file="http://www.springframework.org/schema/integration/file">
+
+ <poller id="defaultPoller" fixed-delay="500" default="true"/>
+ <channel id="input">
+ <queue capacity="10"/>
+ </channel>
+
+ <service-activator id="activator"
+ input-channel="input" ref="service"/>
+
+ <file:inbound-channel-adapter id="fileAdapter"
+ directory="${java.io.tmpdir}"
+ filter="filter"
+ comparator="comparator"
+ auto-startup="false">
+ <poller fixed-rate="10000"/>
+ </file:inbound-channel-adapter>
+
+ <beans:bean id="service" class="siia.concurrency.StubService"/>
+ <beans:bean id="comparator" class="siia.concurrency.StubComparator"/>
+ <beans:bean id="filter" class="siia.concurrency.StubFilter"/>
+</beans:beans>
View
1  siia-examples-parent/pom.xml
@@ -227,6 +227,7 @@
<module>trip-diary</module>
<module>webapp</module>
<module>messages-channels</module>
+ <module>concurrency</module>
</modules>
<build>
<plugins>
Please sign in to comment.
Something went wrong with that request. Please try again.