├── .gitignore
├── src
├── test
│ └── java
│ │ └── jc
│ │ └── DemoApplicationTests.java
└── main
│ ├── java
│ ├── xml
│ │ └── DemoApplication.java
│ └── jc
│ │ └── DemoApplication.java
│ └── resources
│ └── xml
│ └── outbound-kafka-integration.xml
├── pom.xml
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | pom.xml.tag
3 | pom.xml.releaseBackup
4 | pom.xml.versionsBackup
5 | pom.xml.next
6 | release.properties
7 | dependency-reduced-pom.xml
8 | buildNumber.properties
9 |
--------------------------------------------------------------------------------
/src/test/java/jc/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package jc;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.test.context.web.WebAppConfiguration;
6 | import org.springframework.boot.test.SpringApplicationConfiguration;
7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8 |
9 | @RunWith(SpringJUnit4ClassRunner.class)
10 | @SpringApplicationConfiguration(classes = DemoApplication.class)
11 | @WebAppConfiguration
12 | public class DemoApplicationTests {
13 |
14 | @Test
15 | public void contextLoads() {
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/xml/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package xml;
2 |
3 | import org.apache.commons.logging.Log;
4 | import org.apache.commons.logging.LogFactory;
5 | import org.springframework.beans.factory.annotation.Qualifier;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.DependsOn;
11 | import org.springframework.context.annotation.ImportResource;
12 | import org.springframework.integration.config.EnableIntegration;
13 | import org.springframework.messaging.MessageChannel;
14 | import org.springframework.messaging.support.GenericMessage;
15 |
16 | @SpringBootApplication
17 | @EnableIntegration
18 | @ImportResource("/xml/outbound-kafka-integration.xml")
19 | public class DemoApplication {
20 |
21 | private Log log = LogFactory.getLog(getClass());
22 |
23 | @Bean
24 | @DependsOn("kafkaOutboundChannelAdapter")
25 | CommandLineRunner kickOff(@Qualifier("inputToKafka") MessageChannel in) {
26 | return args -> {
27 | for (int i = 0; i < 1000; i++) {
28 | in.send(new GenericMessage<>("#" + i));
29 | log.info("sending message #" + i);
30 | }
31 | };
32 | }
33 |
34 |
35 | public static void main(String args[]) {
36 | SpringApplication.run(DemoApplication.class, args);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/resources/xml/outbound-kafka-integration.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
13 |
14 |
15 |
16 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.test
7 | demo
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 |
12 | org.springframework.boot
13 | spring-boot-starter-parent
14 | 1.2.3.RELEASE
15 |
16 |
17 |
18 | UTF-8
19 | 1.8
20 |
21 |
22 |
23 |
24 | org.apache.kafka
25 | kafka_2.10
26 | 0.8.1.1
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-integration
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter
35 |
36 |
37 | org.springframework.integration
38 | spring-integration-kafka
39 | 1.1.1.RELEASE
40 |
41 |
42 | org.springframework.integration
43 | spring-integration-java-dsl
44 | 1.1.0.M1
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-starter-test
49 | test
50 |
51 |
52 |
53 |
54 |
55 | libs-milestone-local
56 | http://repo.spring.io/simple/libs-milestone-local/
57 |
58 |
59 |
60 |
61 |
62 | org.springframework.boot
63 | spring-boot-maven-plugin
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/src/main/java/jc/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package jc;
2 |
3 | import org.apache.commons.logging.Log;
4 | import org.apache.commons.logging.LogFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.beans.factory.annotation.Qualifier;
7 | import org.springframework.beans.factory.annotation.Value;
8 | import org.springframework.boot.CommandLineRunner;
9 | import org.springframework.boot.SpringApplication;
10 | import org.springframework.boot.autoconfigure.SpringBootApplication;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 | import org.springframework.context.annotation.DependsOn;
14 | import org.springframework.integration.IntegrationMessageHeaderAccessor;
15 | import org.springframework.integration.config.EnableIntegration;
16 | import org.springframework.integration.dsl.IntegrationFlow;
17 | import org.springframework.integration.dsl.IntegrationFlows;
18 | import org.springframework.integration.dsl.SourcePollingChannelAdapterSpec;
19 | import org.springframework.integration.dsl.kafka.Kafka;
20 | import org.springframework.integration.dsl.kafka.KafkaHighLevelConsumerMessageSourceSpec;
21 | import org.springframework.integration.dsl.kafka.KafkaProducerMessageHandlerSpec;
22 | import org.springframework.integration.dsl.support.Consumer;
23 | import org.springframework.integration.kafka.support.ZookeeperConnect;
24 | import org.springframework.messaging.MessageChannel;
25 | import org.springframework.messaging.support.GenericMessage;
26 | import org.springframework.stereotype.Component;
27 |
28 | import java.util.List;
29 | import java.util.Map;
30 |
31 | /**
32 | * Demonstrates using the Spring Integration Apache Kafka Java Configuration DSL.
33 | * Thanks to Spring Integration ninja Artem Bilan
34 | * for getting the Java Configuration DSL working so quickly!
35 | *
36 | * @author Josh Long
37 | */
38 | @EnableIntegration
39 | @SpringBootApplication
40 | public class DemoApplication {
41 |
42 | public static final String TEST_TOPIC_ID = "event-stream";
43 |
44 | /**
45 | * common values used in both the consumer and producer configuration classes.
46 | * This is a poor-man's {@link org.springframework.boot.context.properties.ConfigurationProperties}!
47 | */
48 | @Component
49 | public static class KafkaConfig {
50 |
51 | @Value("${kafka.topic:" + TEST_TOPIC_ID + "}")
52 | private String topic;
53 |
54 | @Value("${kafka.address:localhost:9092}")
55 | private String brokerAddress;
56 |
57 | @Value("${zookeeper.address:localhost:2181}")
58 | private String zookeeperAddress;
59 |
60 | KafkaConfig() {
61 | }
62 |
63 | public KafkaConfig(String t, String b, String zk) {
64 | this.topic = t;
65 | this.brokerAddress = b;
66 | this.zookeeperAddress = zk;
67 | }
68 |
69 | public String getTopic() {
70 | return topic;
71 | }
72 |
73 | public String getBrokerAddress() {
74 | return brokerAddress;
75 | }
76 |
77 | public String getZookeeperAddress() {
78 | return zookeeperAddress;
79 | }
80 | }
81 |
82 | @Configuration
83 | public static class ProducerConfiguration {
84 |
85 | @Autowired
86 | private KafkaConfig kafkaConfig;
87 |
88 | private static final String OUTBOUND_ID = "outbound";
89 |
90 | private Log log = LogFactory.getLog(getClass());
91 |
92 | @Bean
93 | @DependsOn(OUTBOUND_ID)
94 | CommandLineRunner kickOff(@Qualifier(OUTBOUND_ID + ".input") MessageChannel in) {
95 | return args -> {
96 | for (int i = 0; i < 1000; i++) {
97 | in.send(new GenericMessage<>("#" + i));
98 | log.info("sending message #" + i);
99 | }
100 | };
101 | }
102 |
103 |
104 | @Bean(name = OUTBOUND_ID)
105 | IntegrationFlow producer() {
106 |
107 | log.info("starting producer flow..");
108 |
109 | return flowDefinition -> {
110 | Consumer producerMetadataSpecConsumer =
111 | (KafkaProducerMessageHandlerSpec.ProducerMetadataSpec metadata) ->
112 | metadata.async(true)
113 | .batchNumMessages(10)
114 | .valueClassType(String.class)
115 | .valueEncoder(String::getBytes);
116 |
117 | KafkaProducerMessageHandlerSpec messageHandlerSpec =
118 | Kafka.outboundChannelAdapter(props -> props.put("queue.buffering.max.ms", "15000"))
119 | .messageKey(m -> m.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER))
120 | .addProducer(this.kafkaConfig.getTopic(), this.kafkaConfig.getBrokerAddress(), producerMetadataSpecConsumer);
121 | flowDefinition
122 | .handle(messageHandlerSpec);
123 | };
124 | }
125 | }
126 |
127 | @Configuration
128 | public static class ConsumerConfiguration {
129 |
130 | @Autowired
131 | private KafkaConfig kafkaConfig;
132 |
133 | private Log log = LogFactory.getLog(getClass());
134 |
135 | @Bean
136 | IntegrationFlow consumer() {
137 |
138 | log.info("starting consumer..");
139 |
140 | KafkaHighLevelConsumerMessageSourceSpec messageSourceSpec = Kafka.inboundChannelAdapter(
141 | new ZookeeperConnect(this.kafkaConfig.getZookeeperAddress()))
142 | .consumerProperties(props ->
143 | props.put("auto.offset.reset", "smallest")
144 | .put("auto.commit.interval.ms", "100"))
145 | .addConsumer("myGroup", metadata -> metadata.consumerTimeout(100)
146 | .topicStreamMap(m -> m.put(this.kafkaConfig.getTopic(), 1))
147 | .maxMessages(10)
148 | .valueDecoder(String::new));
149 |
150 | Consumer endpointConfigurer = e -> e.poller(p -> p.fixedDelay(100));
151 |
152 | return IntegrationFlows
153 | .from(messageSourceSpec, endpointConfigurer)
154 | .