├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── org
│ │ └── apache
│ │ └── camel
│ │ └── component
│ │ └── kafka
│ │ ├── KafkaComponent.java
│ │ ├── KafkaComponentUtil.java
│ │ ├── KafkaConfiguration.java
│ │ ├── KafkaConstants.java
│ │ ├── KafkaConsumer.java
│ │ ├── KafkaConsumerTask.java
│ │ ├── KafkaEndpoint.java
│ │ └── KafkaProducer.java
└── resources
│ └── META-INF
│ ├── LICENSE.txt
│ ├── NOTICE.txt
│ └── services
│ └── org
│ └── apache
│ └── camel
│ └── component
│ └── kafka
└── test
├── java
└── org
│ └── apache
│ └── camel
│ └── component
│ └── kafka
│ ├── KafkaConsumerTest.java
│ ├── KafkaEndpointTest.java
│ ├── KafkaProducerTest.java
│ ├── KafkaTestSupport.java
│ ├── itests
│ ├── AsyncInOnlyTest.java
│ ├── AsyncInOutTest.java
│ ├── AsyncTransferEnxchangeInOnlyTest.java
│ ├── AsyncTransferExchangeInOutTest.java
│ ├── KafkaConsumerIT.java
│ ├── SimplePartitionerUseInAsyncInOutTest.java
│ ├── SimplePartitionerUseInAsyncTransferExchangeInOutTest.java
│ ├── SimplePartitionerUseInSyncInOutTest.java
│ ├── SimplePartitionerUseInSyncTransferExchangeInOutTest.java
│ ├── SpringSupportTest.java
│ ├── SyncInOnlyTest.java
│ ├── SyncInOutTest.java
│ ├── SyncTransferExchangeInOnlyTest.java
│ ├── SyncTransferExchangeInOutTest.java
│ └── TransferExchangeSpringSupportTest.java
│ ├── load
│ └── SimpleLoadTest.java
│ └── partitioner
│ └── SimplePartitioner.java
└── resources
├── META-INF
└── spring
│ └── basic-spring-tests.xml
└── log4j.properties
/README.md:
--------------------------------------------------------------------------------
1 | # Camel-Kafka
2 |
3 | ## Overview
4 |
5 | [Apache Kafka](http://kafka.apache.org/), is a distributed messaging system that due to its architecture, performance and scalability characteristics, has proven to be revolutionary in today’s messaging technologies and has been used with success in several domains and projects - most notably among them the LinkedIn’s real-time activity data pipeline. **Camel-Kafka** is an [Apache Camel](http://camel.apache.org/) component that allows you to work with [Apache Kafka](http://kafka.apache.org/) message oriented middleware.
6 |
7 |
8 | ## Documentation :
9 |
10 | Documentation for the camel-kafka component can be found in the [wiki pages](https://github.com/ipolyzos/camel-kafka/wiki).
11 |
12 |
13 | ## License:
14 |
15 | Copyright 2013 Ioannis Polyzos
16 |
17 | Licensed under the Apache License, Version 2.0 (the "License");
18 | you may not use this file except in compliance with the License.
19 | You may obtain a copy of the License at
20 |
21 | http://www.apache.org/licenses/LICENSE-2.0
22 |
23 | Unless required by applicable law or agreed to in writing, software
24 | distributed under the License is distributed on an "AS IS" BASIS,
25 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 | See the License for the specific language governing permissions and
27 | limitations under the License.
28 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
16 |
20 |
21 | 4.0.0
22 |
23 |
24 | org.apache.camel
25 | components
26 | 2.13.1
27 |
28 |
29 | camel-kafka
30 | bundle
31 | Camel :: Kafka
32 |
33 |
34 | Kafka Component for Apache Camel.
35 |
36 |
37 |
38 |
39 | org.slf4j,
40 | org.apache.commons.lang,
41 | org.apache.camel*,
42 | scala,
43 | kafka.message,
44 | kafka.consumer,
45 | kafka.producer,
46 | kafka.javaapi.consumer,
47 | kafka.javaapi.producer,
48 | com.google.common.base;version="[11,14)",
49 | org.apache.hadoop.conf;version="[1,2)"
50 |
51 |
52 | org.apache.camel.component.kafka.*
53 |
54 | org.apache.camel.component.kafka.*
55 | org.apache.camel.spi.ComponentResolver;component=kafka
56 |
57 |
58 | 2.4_6
59 | 1.5.1
60 | 2.0.2
61 | 0.8.1.1_1
62 | 0.3
63 |
64 |
65 |
66 |
67 |
68 | ${project.basedir}/src/test/resources
69 |
70 | **/*
71 |
72 |
73 |
74 |
75 |
76 | maven-surefire-plugin
77 |
78 |
79 |
80 | WARN
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 | org.slf4j
93 | slf4j-simple
94 | test
95 |
96 |
97 |
98 |
99 | org.apache.camel
100 | camel-core
101 |
102 |
103 |
104 |
105 | junit
106 | junit
107 | test
108 |
109 |
110 | org.mockito
111 | mockito-all
112 | ${mockito-version}
113 | test
114 |
115 |
116 | org.powermock
117 | powermock-core
118 | ${powermock-version}
119 | test
120 |
121 |
122 | org.powermock
123 | powermock-module-junit4
124 | ${powermock-version}
125 | test
126 |
127 |
128 | org.powermock
129 | powermock-api-mockito
130 | ${powermock-version}
131 | test
132 |
133 |
134 | org.apache.camel
135 | camel-test
136 | test
137 |
138 |
139 | org.apache.camel
140 | camel-test-spring
141 | test
142 |
143 |
144 |
145 |
146 | org.apache.servicemix.bundles
147 | org.apache.servicemix.bundles.kafka_2.10
148 | ${kafka_2.10-version}
149 |
150 |
151 |
152 |
153 | com.101tec
154 | zkclient
155 | ${zkclient-version}
156 |
157 |
158 |
159 |
160 | com.yammer.metrics
161 | metrics-core
162 | ${yammer-metrics-version}
163 |
164 |
165 | com.yammer.metrics
166 | metrics-annotation
167 | ${yammer-metrics-version}
168 |
169 |
170 |
171 |
172 | org.scala-lang
173 | scala-library
174 | ${scala-version}
175 | provided
176 |
177 |
178 |
179 |
180 | org.apache.servicemix.bundles
181 | org.apache.servicemix.bundles.commons-lang
182 | ${commons-lang-version}
183 |
184 |
185 | com.google.guava
186 | guava
187 | ${google-guava-version}
188 |
189 |
190 |
191 |
192 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaComponent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import java.util.Map;
21 |
22 | import org.apache.camel.Endpoint;
23 | import org.apache.camel.impl.DefaultComponent;
24 | import org.apache.camel.util.ObjectHelper;
25 |
26 | /**
27 | * Camel-Kafka {@link DefaultComponent}.
28 | */
29 | public class KafkaComponent extends DefaultComponent {
30 |
31 | /**
32 | * Camel-Kafka configuration
33 | */
34 | private KafkaConfiguration configuration;
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | @Override
40 | protected Endpoint createEndpoint(final String uri,
41 | final String remaining,
42 | final Map parameters) throws Exception {
43 |
44 | configuration= new KafkaConfiguration();
45 | setProperties(configuration, parameters);
46 |
47 | if(ObjectHelper.isEmpty(remaining)){
48 |
49 | throw new IllegalArgumentException("The topic name is missing.");
50 | }
51 |
52 | configuration.setTopicName(remaining);
53 |
54 | return new KafkaEndpoint(uri, this, configuration);
55 | }
56 |
57 | /**
58 | * Get Configuration
59 | *
60 | * @return Configuration
61 | */
62 | public KafkaConfiguration getConfiguration() {
63 |
64 | return configuration;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaComponentUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import static com.google.common.base.Strings.isNullOrEmpty;
21 |
22 | import kafka.consumer.ConsumerConfig;
23 |
24 | import kafka.message.MessageAndMetadata;
25 | import org.apache.camel.Exchange;
26 | import org.apache.camel.Message;
27 | import org.apache.camel.impl.DefaultExchange;
28 | import org.apache.camel.impl.DefaultExchangeHolder;
29 | import org.apache.camel.impl.DefaultMessage;
30 | import org.apache.commons.lang.SerializationUtils;
31 |
32 | import java.util.concurrent.ExecutorService;
33 |
34 | /**
35 | * Camel-Kafka Component Util
36 | */
37 | public final class KafkaComponentUtil {
38 |
39 | /**
40 | * Private Constructor
41 | */
42 | private KafkaComponentUtil() {
43 |
44 | //prevent instantiation
45 | }
46 |
47 | /**
48 | * Util method to validate configuration.
49 | *
50 | * @param configuration
51 | * Camel Kafka Consumer Configuration
52 | */
53 | protected static void checkConsumerConfiguration(final KafkaConfiguration configuration) {
54 |
55 | if (isNullOrEmpty(configuration.getGroupId())) {
56 |
57 | throw new IllegalArgumentException("\"group.id\" is null or empty!");
58 | } else if (isNullOrEmpty(configuration.getZookeeperConnect())) {
59 |
60 | throw new IllegalArgumentException("\"zookeeper.connect\" is null or empty!");
61 | }
62 | }
63 |
64 | /**
65 | * Utility method to validate configuration.
66 | *
67 | * @param configuration
68 | * Camel Kafka Configuration
69 | */
70 | protected static void checkProducerConfiguration(final KafkaConfiguration configuration) {
71 |
72 | if (isNullOrEmpty(configuration.getMetadataBrokerList())) {
73 |
74 | throw new IllegalArgumentException("\"metadata.broker.list\" is null or empty!");
75 | } else if (isNullOrEmpty(configuration.getSerializerClass())) {
76 |
77 | throw new IllegalArgumentException("\"serializer.class\" is null or empty!");
78 | }
79 | }
80 |
81 | /**
82 | * Util method to create a Consumer Config
83 | *
84 | * @param configuration Kafka Configuration
85 | * @return Consumer Config
86 | */
87 | protected static ConsumerConfig createConsumerConfig(final KafkaConfiguration configuration) {
88 |
89 | return new ConsumerConfig(configuration.getProperties());
90 | }
91 |
92 | /**
93 | * Utility method to serialize the whole exchange.
94 | *
95 | * @param exchange Exchange
96 | * @return byte array
97 | */
98 | public static byte[] serializeExchange(final Exchange exchange) {
99 |
100 | return SerializationUtils.serialize(DefaultExchangeHolder.marshal(exchange));
101 | }
102 |
103 | /**
104 | * Utility method to serialize the whole exchange.
105 | *
106 | * @param exchange
107 | * @return
108 | */
109 | public static byte[] serializeBody(final Exchange exchange) {
110 |
111 | return SerializationUtils.serialize(exchange.getIn().getBody(byte[].class));
112 | }
113 |
114 | /**
115 | * Utility method to serialize the whole exchange.
116 | *
117 | * @param body
118 | * @return
119 | */
120 | public static Object deserializeBody(final byte[] body) {
121 |
122 | return SerializationUtils.deserialize(body);
123 | }
124 |
125 | /**
126 | * Get get executor service from camel context
127 | *
128 | * @param endpoint
129 | * @param configuration
130 | * @return
131 | */
132 | protected static ExecutorService getExecutorService(final KafkaEndpoint endpoint,
133 | final KafkaConfiguration configuration) {
134 |
135 | return endpoint.getCamelContext()
136 | .getExecutorServiceManager()
137 | .newFixedThreadPool(endpoint, "KafkaTopic[" + configuration.getTopicName() + "]", configuration.getConcurrentConsumers());
138 | }
139 |
140 | /**
141 | * Utility method to create exchange from incoming data
142 | *
143 | * @param incomingData
144 | * @param configuration
145 | */
146 | protected static Exchange constructExchange(final KafkaEndpoint endpoint,
147 | final MessageAndMetadata incomingData,
148 | final KafkaConfiguration configuration) {
149 |
150 | Exchange exchange = new DefaultExchange(endpoint.getCamelContext(), endpoint.getExchangePattern());
151 |
152 | if (configuration.isTransferExchange()) { // transfer exchange?
153 |
154 | DefaultExchangeHolder exchangeHolder = (DefaultExchangeHolder) SerializationUtils.deserialize(incomingData.message());
155 | DefaultExchangeHolder.unmarshal(exchange, exchangeHolder);
156 | }else{
157 |
158 | final Message message = new DefaultMessage();
159 | message.setBody(deserializeBody(incomingData.message()));
160 | exchange.setIn(message);
161 | }
162 |
163 | fillExchangeInMessageWithMetadata(exchange, incomingData);
164 |
165 | return exchange;
166 |
167 | }
168 |
169 | /**
170 | * Utility method to fill in message with metadata
171 | *
172 | * @param exchange
173 | * @param incomingData
174 | */
175 | private static void fillExchangeInMessageWithMetadata(final Exchange exchange,
176 | final MessageAndMetadata incomingData) {
177 | final Message message = exchange.getIn();
178 |
179 | message.setHeader(KafkaConstants.PARTITION.value, incomingData.partition());
180 | message.setHeader(KafkaConstants.TOPIC.value, incomingData.topic());
181 | message.setHeader(KafkaConstants.OFFSET.value, incomingData.offset());
182 | message.setHeader(KafkaConstants.KEY.value, incomingData.key());
183 | }
184 | }
185 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaConfiguration.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import org.slf4j.Logger;
21 | import org.slf4j.LoggerFactory;
22 |
23 | import static com.google.common.base.Strings.isNullOrEmpty;
24 | import static org.apache.camel.component.kafka.KafkaConstants.*;
25 |
26 | import java.util.Properties;
27 | /**
28 | * Camel Kafka Configuration.
29 | *
30 | * NOTE: It contains configuration for both consumers and
31 | * producers. Default configuration apply upon construction and
32 | * empty or null values ignored after this.
33 | */
34 | public class KafkaConfiguration {
35 |
36 | /**
37 | * Logger
38 | */
39 | private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConfiguration.class);
40 |
41 | /**
42 | * Topic Name
43 | */
44 | private String topicName;
45 |
46 | /**
47 | * Transfer Exchange
48 | */
49 | private boolean transferExchange = false;
50 |
51 | /**
52 | * Concurrent consumers (i.e streams in this context)
53 | */
54 | private int concurrentConsumers = 10;
55 |
56 | /**
57 | * Partition Key
58 | */
59 | private String partitionKey = "DEFAULT_PARTITION";
60 |
61 | /**
62 | * Kafka Configuration
63 | */
64 | private Properties props;
65 |
66 | /**
67 | * Default constructor
68 | *
69 | * NOTE: constructor also initialize configuration with
70 | * default values as given by kafka documentation
71 | */
72 | public KafkaConfiguration() {
73 |
74 | // initialize properties
75 | props = new Properties();
76 |
77 | /* NOTE: set defaults values according to
78 | * kafka documentation
79 | */
80 | setGroupId(DEFAULT_GROUP.value);
81 | setTopicName(DEFAULT_TOPIC.value);
82 | setSocketTimeoutMs(DEFAULT_SOCKET_TIMEOUT_MS.value);
83 | setSocketReceiveBufferBytes(DEFAULT_SOCKET_RECEIVE_BUFFER_BYTES.value);
84 | setFetchMessageMaxBytes(DEFAULT_FETCH_MESSAGE_MAX_BYTES.value);
85 | setAutoCommitEnable(DEFAULT_AUTO_COMMIT_ENABLE.value);
86 | setAutoCommitIntervalMs(DEFAULT_AUTO_COMMIT_INTERVAL_MS.value);
87 | setQueuedMaxMessageChunks(DEFAULT_QUEUED_MAX_MESSAGE_CHUNKS.value);
88 | setRebalanceMaxRetries(DEFAULT_REBALANCE_MAX_RETRIES.value);
89 | setFetchMinBytes(DEFAULT_FETCH_MIN_BYTES.value);
90 | setFetchWaitMaxMs(DEFAULT_FETCH_WAIT_MAX_MS.value);
91 | setRebalanceBackoffMs(DEFAULT_REBALANCE_BACKOFF_MS.value);
92 | setRefreshLeaderBackoffMs(DEFAULT_REFRESH_LEADER_BACKOFF_MS.value);
93 | setAutoOffsetReset(DEFAULT_AUTO_OFFSET_RESET.value);
94 | setConsumerTimeoutMs(DEFAULT_CONSUMER_TIMEOUT_MS.value);
95 | setZookeeperSessionTimeoutMs(DEFAULT_ZOOKEEPER_SESSION_TIMEOUT_MS.value);
96 | setZookeeperConnectionTimeoutMs(DEFAULT_CONNECTION_TIMEOUT_MS.value);
97 | setZookeeperSyncTimeMs(DEFAULT_ZOOKEEPER_SYNC_TIME_MS.value);
98 | setRequestRequiredAcks(DEFAULT_REQUEST_REQUIRED_ACKS.value);
99 | setRequestTimeoutMs(DEFAULT_REQUEST_TIMEOUT_MS.value);
100 | setProducerType(DEFAULT_PRODUCER_TYPE.value);
101 | setSerializerClass(DEFAULT_SERIALIZER_CLASS.value);
102 | setPartitionerClass(DEFAULT_PARTITIONER_CLASS.value);
103 | setCompressionCodec(DEFAULT_COMPRESSION_CODEC.value);
104 | setCompressedTopics(null);
105 | setMessageSendMaxRetries(DEFAULT_MESSAGE_SEND_MAX_RETRIES.value);
106 | setRetryBackoffMs(DEFAULT_RETRY_BACKOFF_MS.value);
107 | setTopicMetadataRefreshIntervalMs(DEFAULT_TOPIC_METADATA_REFRESH_INTERVAL_MS.value);
108 | setQueueBufferingMaxMs(DEFAULT_QUEUE_BUFFERING_MAX_MS.value);
109 | setQueueBufferingMaxMessages(DEFAULT_QUEUE_BUFFERING_MAX_MESSAGES.value);
110 | setQueueEnqueueTimeoutMs(DEFAULT_QUEUE_ENQUEUE_TIMEOUT_MS.value);
111 | setBatchNumMessages(DEFAULT_BATCH_NUM_MESSAGES.value);
112 | setSendBufferBytes(DEFAULT_SEND_BUFFER_BYTES.value);
113 | setClientId(DEFAULT_CLIENT_ID.value);
114 | setKeySerializerClass(DEFAULT_KEY_SERIALIZER_CLASS.value);
115 | }
116 |
117 | /**
118 | * Get Kafka Consumers/Producers Configuration
119 | *
120 | * @return kafkaConfiguration
121 | */
122 | public Properties getKafkaConfiguration() {
123 |
124 | return props;
125 | }
126 |
127 | /**
128 | * Set Kafka Consumers/Producers Configuration.
129 | *
130 | * @param kafkaConfiguration
131 | */
132 | public void setKafkaConfiguration(Properties kafkaConfiguration) {
133 |
134 | this.props = kafkaConfiguration;
135 | }
136 |
137 | /**
138 | * Get topic name
139 | *
140 | * @return topicName
141 | */
142 | public String getTopicName() {
143 |
144 | return topicName;
145 | }
146 |
147 | /**
148 | * Set topic name
149 | *
150 | * @param topicName
151 | */
152 | public void setTopicName(String topicName) {
153 |
154 | this.topicName = topicName;
155 | }
156 |
157 | /**
158 | * Get transfer exchange
159 | *
160 | * @return
161 | */
162 | public boolean isTransferExchange() {
163 |
164 | return transferExchange;
165 | }
166 |
167 | /**
168 | * Set transfer exchange
169 | *
170 | * @param transferExchange
171 | */
172 | public void setTransferExchange(boolean transferExchange) {
173 |
174 | this.transferExchange = transferExchange;
175 | }
176 |
177 | /**
178 | * Get concurrent consumers
179 | *
180 | * @return
181 | */
182 | public int getConcurrentConsumers() {
183 |
184 | return concurrentConsumers;
185 | }
186 |
187 | /**
188 | * Get concurrent consumers
189 | *
190 | * @param concurrentConsumers
191 | */
192 | public void setConcurrentConsumers(int concurrentConsumers) {
193 |
194 | this.concurrentConsumers = concurrentConsumers;
195 | }
196 |
197 |
198 | /**
199 | * Get Partition key
200 | *
201 | * @return
202 | */
203 | public String getPartitionKey() {
204 | return partitionKey;
205 | }
206 |
207 | /**
208 | * Set partition key
209 | *
210 | * @param partitionKey
211 | */
212 | public void setPartitionKey(final String partitionKey) {
213 | this.partitionKey = partitionKey;
214 | }
215 |
216 | /**
217 | * Get Group ID.
218 | *
219 | * @return groupId
220 | */
221 | public String getGroupId() {
222 |
223 | return props.getProperty("group.id");
224 | }
225 |
226 | /**
227 | * Set Group ID.
228 | *
229 | * @param groupId
230 | */
231 | public void setGroupId(final String groupId) {
232 |
233 | if (!isNullOrEmpty(groupId)) {
234 |
235 | props.put("group.id", groupId);
236 | }
237 | }
238 |
239 | /**
240 | * Get Zookeeper Connection String.
241 | *
242 | * @return zookeeperConnect
243 | */
244 | public String getZookeeperConnect() {
245 |
246 | return props.getProperty("zookeeper.connect");
247 | }
248 |
249 | /**
250 | * Set Zookeeper Connection String.
251 | *
252 | * @param zookeeperConnect
253 | */
254 | public void setZookeeperConnect(final String zookeeperConnect) {
255 |
256 | if (!isNullOrEmpty(zookeeperConnect)) {
257 |
258 | props.put("zookeeper.connect", zookeeperConnect);
259 | }
260 | }
261 |
262 | /**
263 | * Get Zookeeper Connection String.
264 | *
265 | * NOTE: Alias of getZookeeperConnect().
266 | *
267 | * @return zookeeperConnect
268 | */
269 | public String getZkConnect() {
270 |
271 | return getZookeeperConnect();
272 | }
273 |
274 | /**
275 | * Set Zookeeper Connection String.
276 | *
277 | * NOTE: Alias of setZookeeperConnect().
278 | *
279 | * @param zkConnect
280 | */
281 | public void setZkConnect(final String zkConnect) {
282 |
283 | this.setZookeeperConnect(zkConnect);
284 | }
285 |
286 | /**
287 | * Get Consumer Id
288 | *
289 | * NOTE: the default value is : null.
290 | *
291 | * @return consumerId
292 | */
293 | public String getConsumerId() {
294 |
295 | return props.getProperty("consumer.id");
296 | }
297 |
298 | /**
299 | * Set Consumer ID
300 | *
301 | * NOTE: Generated automatically if not set.
302 | *
303 | * @param consumerId
304 | */
305 | public void setConsumerId(final String consumerId) {
306 |
307 | if (!isNullOrEmpty(consumerId)){
308 |
309 | props.setProperty("consumer.id", consumerId);
310 | }
311 | }
312 |
313 | /**
314 | * Get Socket Timeout for network requests.
315 | *
316 | * NOTE: the default value is : 30 * 1000
317 | *
318 | * @return socketTimeoutMs
319 | */
320 | public String getSocketTimeoutMs() {
321 |
322 | return props.getProperty("socket.timeout.ms");
323 | }
324 |
325 | /**
326 | * Set Socket Timeout for network requests
327 | *
328 | * NOTE: The actual timeout set will be max.fetch.wait + socket.timeout.ms.
329 | *
330 | * @param socketTimeoutMs
331 | */
332 | public void setSocketTimeoutMs(final String socketTimeoutMs) {
333 |
334 | if (!isNullOrEmpty(socketTimeoutMs)){
335 |
336 | props.setProperty("socket.timeout.ms", socketTimeoutMs);
337 | }
338 | }
339 |
340 | /**
341 | * Get the socket receive buffer for network requests.
342 | *
343 | * NOTE: the default value is : 64 * 1024
344 | *
345 | * @return socketReceiveBufferBytes
346 | */
347 | public String getSocketReceiveBufferBytes() {
348 |
349 | return props.getProperty("socket.timeout.ms");
350 | }
351 |
352 | /**
353 | * Set the socket receive buffer for network requests
354 | *
355 | * @param socketReceiveBufferBytes
356 | */
357 | public void setSocketReceiveBufferBytes(final String socketReceiveBufferBytes) {
358 |
359 | if (!isNullOrEmpty(socketReceiveBufferBytes)){
360 |
361 | props.setProperty("socket.receive.buffer.bytes", socketReceiveBufferBytes);
362 | }
363 | }
364 |
365 | /**
366 | * Get the number of byes of messages to attempt to fetch for each topic-partition in each fetch request.
367 | *
368 | * NOTE: the default value is : 1024 * 1024
369 | *
370 | * @return fetchMessageMaxBytes
371 | */
372 | public String getFetchMessageMaxBytes() {
373 |
374 | return props.getProperty("fetch.message.max.bytes");
375 | }
376 |
377 | /**
378 | * The number of byes of messages to attempt to fetch for each topic-partition in each fetch request
379 | *
380 | * @param fetchMessageMaxBytes
381 | */
382 | public void setFetchMessageMaxBytes(final String fetchMessageMaxBytes) {
383 |
384 | if (!isNullOrEmpty(fetchMessageMaxBytes)){
385 |
386 | props.setProperty("fetch.message.max.bytes",fetchMessageMaxBytes);
387 | }
388 | }
389 |
390 | /**
391 | * Get auto commit status.
392 | *
393 | * NOTE: the default value is :true
394 | *
395 | * @return autoCommitEnabled
396 | */
397 | public String getAutoCommitEnable() {
398 |
399 | return props.getProperty("auto.commit.enable");
400 | }
401 |
402 | /**
403 | * Enable/disable auto commit.
404 | *
405 | * NOTE: If true, periodically commit to zookeeper the offset of messages already fetched by the consumer
406 | * (the default value is true).
407 | *
408 | * @param autoCommitEnable
409 | */
410 | public void setAutoCommitEnable(final String autoCommitEnable) {
411 |
412 | if (!isNullOrEmpty(autoCommitEnable)){
413 |
414 | props.setProperty("auto.commit.enable",autoCommitEnable);
415 | }
416 | }
417 |
418 | /**
419 | * Get the frequency in ms that the consumer offsets are committed to zookeeper.
420 | *
421 | * NOTE: the default value is :60 * 1000
422 | *
423 | * @return autoCommitEnable
424 | */
425 | public String getAutoCommitIntervalMs() {
426 |
427 | return props.getProperty("auto.commit.interval.ms");
428 | }
429 |
430 | /**
431 | * Set the frequency in ms that the consumer offsets are committed to zookeeper.
432 | *
433 | * @param autoCommitIntervalMs
434 | */
435 | public void setAutoCommitIntervalMs(final String autoCommitIntervalMs) {
436 |
437 | if (!isNullOrEmpty(autoCommitIntervalMs)){
438 |
439 | props.setProperty("auto.commit.interval.ms",autoCommitIntervalMs);
440 | }
441 | }
442 |
443 | /**
444 | * Get the max number of message chunks buffered for consumption.
445 | *
446 | * NOTE: the default value is :10.
447 | *
448 | * @return queuedMaxMessageChunks
449 | */
450 | public String getQueuedMaxMessageChunks() {
451 |
452 | return props.getProperty("queued.max.message.chunks");
453 | }
454 |
455 | /**
456 | * Set the max number of message chunks buffered for consumption.
457 | *
458 | * @param queuedMaxMessageChunks
459 | */
460 | public void setQueuedMaxMessageChunks(final String queuedMaxMessageChunks) {
461 |
462 | if (!isNullOrEmpty(queuedMaxMessageChunks)){
463 |
464 | props.setProperty("queued.max.message.chunks",queuedMaxMessageChunks);
465 | }
466 | }
467 |
468 | /**
469 | * Get the maximum number of attempts to re-balance before giving up.
470 | *
471 | * NOTE: the default value is :4.
472 | *
473 | * @return rebalanceMaxRetries
474 | */
475 | public String getRebalanceMaxRetries() {
476 |
477 | return props.getProperty("rebalance.max.retries");
478 | }
479 |
480 | /**
481 | * Set the maximum number of attempts to re-balance before giving up.
482 | *
483 | * @param rebalanceMaxRetries
484 | */
485 | public void setRebalanceMaxRetries(final String rebalanceMaxRetries) {
486 |
487 | if (!isNullOrEmpty(rebalanceMaxRetries)){
488 |
489 | props.setProperty("rebalance.max.retries",rebalanceMaxRetries);
490 | }
491 | }
492 |
493 | /**
494 | * Set the minimum amount of data the server should return for a fetch request.
495 | *
496 | * NOTE: the default value is :1.
497 | *
498 | * @return fetchMinBytes
499 | */
500 | public String getFetchMinBytes() {
501 |
502 | return props.getProperty("fetch.min.bytes");
503 | }
504 |
505 | /**
506 | * Set the minimum amount of data the server should return for a fetch request.
507 | *
508 | * @param fetchMinBytes
509 | */
510 | public void setFetchMinBytes(final String fetchMinBytes) {
511 |
512 | if (!isNullOrEmpty(fetchMinBytes)){
513 |
514 | props.setProperty("fetch.min.bytes",fetchMinBytes);
515 | }
516 | }
517 |
518 | /**
519 | * Get the maximum amount of time the server will block before answering the fetch request.
520 | *
521 | * NOTE:If there isn't sufficient data to immediately satisfy fetch.min.bytes.
522 | * The default value is :100.
523 | *
524 | * @return fetchWaitMaxMs
525 | */
526 | public String getFetchWaitMaxMs() {
527 |
528 | return props.getProperty("fetch.wait.max.ms");
529 | }
530 |
531 | /**
532 | * Set the maximum amount of time the server will block before answering the fetch request.
533 | *
534 | * @param fetchWaitMaxMs
535 | */
536 | public void setFetchWaitMaxMs(final String fetchWaitMaxMs) {
537 |
538 | if (!isNullOrEmpty(fetchWaitMaxMs)){
539 |
540 | props.setProperty("fetch.wait.max.ms",fetchWaitMaxMs);
541 | }
542 | }
543 |
544 | /**
545 | * Get backoff time between retries during rebalance.
546 | *
547 | * NOTE: the default value is :2000.
548 | *
549 | * @return rebalanceBackoffMs
550 | */
551 | public String getRebalanceBackoffMs() {
552 |
553 | return props.getProperty("rebalance.backoff.ms");
554 | }
555 |
556 | /**
557 | * Set backoff time between retries during rebalance.
558 | *
559 | * @param rebalanceBackoffMs
560 | */
561 | public void setRebalanceBackoffMs(final String rebalanceBackoffMs) {
562 |
563 | if (!isNullOrEmpty(rebalanceBackoffMs)){
564 |
565 | props.setProperty("rebalance.backoff.ms",rebalanceBackoffMs);
566 | }
567 | }
568 |
569 | /**
570 | * Get backoff time to wait before trying to determine the leader of a partition that has just lost its leader.
571 | *
572 | * NOTE: the default value is :200.
573 | *
574 | * @return
575 | */
576 | public String getRefreshLeaderBackoffMs() {
577 |
578 | return props.getProperty("refresh.leader.backoff.ms");
579 | }
580 |
581 | /**
582 | * Set backoff time to wait before trying to determine the leader of a partition that has just lost its leader.
583 | *
584 | * @param refreshLeaderBackoffMs
585 | */
586 | public void setRefreshLeaderBackoffMs(final String refreshLeaderBackoffMs) {
587 |
588 | if (!isNullOrEmpty(refreshLeaderBackoffMs)){
589 |
590 | props.setProperty("refresh.leader.backoff.ms",refreshLeaderBackoffMs);
591 | }
592 | }
593 |
594 | /**
595 | * Configure the auto offset reset.
596 | *
597 | * NOTE: the default value is :largest.
598 | *
599 | * @return
600 | */
601 | public String getAutoOffsetReset() {
602 |
603 | return props.getProperty("auto.offset.reset");
604 | }
605 |
606 | /**
607 | * Get the auto offset reset configuration.
608 | *
609 | * @param autoOffsetReset
610 | */
611 | public void setAutoOffsetReset(final String autoOffsetReset) {
612 |
613 | if (!isNullOrEmpty(autoOffsetReset)){
614 |
615 | props.setProperty("auto.offset.reset",autoOffsetReset);
616 | }
617 | }
618 |
619 | /**
620 | * Set the consumer timeout in ms.
621 | *
622 | * NOTE: Throw a timeout exception to the consumer if no message is available for consumption after the specified interval. The default value is :-1.
623 | *
624 | * @return
625 | */
626 | public String getConsumerTimeoutMs() {
627 |
628 | return props.getProperty("consumer.timeout.ms");
629 | }
630 |
631 | /**
632 | * Set the consumer timeout in ms.
633 | *
634 | * @param consumerTimeoutMs
635 | */
636 | public void setConsumerTimeoutMs(final String consumerTimeoutMs) {
637 |
638 | if (!isNullOrEmpty(consumerTimeoutMs)){
639 |
640 | props.setProperty("consumer.timeout.ms",consumerTimeoutMs);
641 | }
642 | }
643 |
644 | /**
645 | * Get client id
646 | *
647 | * Note:The client id is a user-specified string sent in each request to help trace calls. The default value is :group id value.
648 | *
649 | * @return clientId
650 | */
651 | public String getClientId() {
652 |
653 | return props.getProperty("client.id");
654 | }
655 |
656 | /**
657 | * Set client id
658 | *
659 | * @param clientId
660 | */
661 | public void setClientId(final String clientId) {
662 |
663 | if (!isNullOrEmpty(clientId)){
664 |
665 | props.setProperty("client.id",clientId);
666 | }
667 | }
668 |
669 | /**
670 | * Get Zookeeper session timeout.
671 | *
672 | * NOTE:If the consumer fails to heartbeat to zookeeper for this period of time it is considered dead and a rebalance will occur.
673 | *
674 | * @return zookeeperSessionTimeoutMs
675 | */
676 | public String getZookeeperSessionTimeoutMs() {
677 |
678 | return props.getProperty("zookeeper.session.timeout.ms");
679 | }
680 |
681 | /**
682 | * Set Zookeeper session timeout.
683 | *
684 | * @param zookeeperSessionTimeoutMs
685 | */
686 | public void setZookeeperSessionTimeoutMs(final String zookeeperSessionTimeoutMs) {
687 |
688 | if (!isNullOrEmpty(zookeeperSessionTimeoutMs)){
689 |
690 | props.setProperty("zookeeper.session.timeout.ms",zookeeperSessionTimeoutMs);
691 | }
692 | }
693 |
694 | /**
695 | * Get the max time that the client waits while establishing a connection to zookeeper.
696 | *
697 | * NOTE: the default value is :6000.
698 | *
699 | * @return zookeeperConnectionTimeoutMs
700 | */
701 | public String getZookeeperConnectionTimeoutMs() {
702 |
703 | return props.getProperty("zookeeper.connection.timeout.ms");
704 | }
705 |
706 | /**
707 | * Set the max time that the client waits while establishing a connection to zookeeper.
708 | *
709 | * @param zookeeperConnectionTimeoutMs
710 | */
711 | public void setZookeeperConnectionTimeoutMs(final String zookeeperConnectionTimeoutMs) {
712 |
713 | if (!isNullOrEmpty(zookeeperConnectionTimeoutMs)){
714 |
715 | props.setProperty("zookeeper.connection.timeout.ms",zookeeperConnectionTimeoutMs);
716 | }
717 | }
718 |
719 | /**
720 | * Get how far a ZK follower can be behind a ZK leader
721 | *
722 | * NOTE: the default value is :2000.
723 | *
724 | * @return zookeeperSyncTimeMs
725 | */
726 | public String getZookeeperSyncTimeMs() {
727 |
728 | return props.getProperty("zookeeper.sync.time.ms");
729 | }
730 |
731 | /**
732 | * Set how far a ZK follower can be behind a ZK leader.
733 | *
734 | * @param zookeeperSyncTimeMs
735 | */
736 | public void setZookeeperSyncTimeMs(final String zookeeperSyncTimeMs) {
737 |
738 | if (!isNullOrEmpty(zookeeperSyncTimeMs)){
739 |
740 | props.setProperty("zookeeper.sync.time.ms",zookeeperSyncTimeMs);
741 | }
742 | }
743 |
744 | /**
745 | * Get metadata broker list.
746 | *
747 | * @return
748 | */
749 | public String getMetadataBrokerList() {
750 |
751 | return props.getProperty("metadata.broker.list");
752 | }
753 |
754 | /**
755 | * Set metadata broker list.
756 | *
757 | * @param metadataBrokerList
758 | */
759 | public void setMetadataBrokerList(final String metadataBrokerList) {
760 |
761 | if (!isNullOrEmpty(metadataBrokerList)){
762 |
763 | props.setProperty("metadata.broker.list",metadataBrokerList);
764 | }
765 | }
766 |
767 | /**
768 | * Get Request Required Acks
769 | *
770 | * NOTE: the default value is :2000.
771 | *
772 | * @return requestRequiredAcks
773 | */
774 | public String getRequestRequiredAcks() {
775 |
776 | return props.getProperty("request.required.acks");
777 | }
778 |
779 | /**
780 | * Set Request Required Acks
781 | *
782 | * @param requestRequiredAcks
783 | */
784 | public void setRequestRequiredAcks(final String requestRequiredAcks) {
785 |
786 | if (!isNullOrEmpty(requestRequiredAcks)){
787 |
788 | props.setProperty("request.required.acks",requestRequiredAcks);
789 | }
790 | }
791 |
792 | /**
793 | * Get the amount of time the broker will wait trying to meet the request.
794 | *
795 | * NOTE: the default value is :10000.
796 | *
797 | * @return requestTimeoutMs
798 | */
799 | public String getRequestTimeoutMs() {
800 |
801 | return props.getProperty("request.timeout.ms");
802 | }
803 |
804 | /**
805 | * Set the amount of time the broker will wait trying to meet the request.
806 | *
807 | * @param requestTimeoutMs
808 | */
809 | public void setRequestTimeoutMs(final String requestTimeoutMs) {
810 |
811 | if (!isNullOrEmpty(requestTimeoutMs)){
812 |
813 | props.setProperty("request.timeout.ms",requestTimeoutMs);
814 | }
815 | }
816 |
817 | /**
818 | * Get the producer type (sync/async)
819 | *
820 | * NOTE: the default value is :sync.
821 | *
822 | * @return producerType
823 | */
824 | public String getProducerType() {
825 |
826 | return props.getProperty("producer.type");
827 | }
828 |
829 | /**
830 | * Set the producer type (sync/async)
831 | *
832 | * @param producerType
833 | */
834 | public void setProducerType(final String producerType) {
835 |
836 | if (!isNullOrEmpty(producerType)){
837 |
838 | props.setProperty("producer.type",producerType);
839 | }
840 | }
841 |
842 | /**
843 | * Get the serializer class.
844 | *
845 | * NOTE: the default value is :kafka.serializer.DefaultEncoder. The serializer class for messages.
846 | * The default encoder takes a byte[] and returns the same byte[].
847 | *
848 | * @return serializerClass
849 | */
850 | public String getSerializerClass() {
851 |
852 | return props.getProperty("serializer.class");
853 | }
854 |
855 | /**
856 | * Set the serializer class.
857 | *
858 | * NOTE: currently tested and supports only the default serializer
859 | * due to it send and receive the whole exchange.
860 | *
861 | *
862 | * @param serializerClass
863 | */
864 | public void setSerializerClass(final String serializerClass) {
865 |
866 | /* TODO: need to work around this toward support only body.
867 | *
868 | * if (!isNullOrEmpty(serializerClass)){
869 | *
870 | * props.setProperty("serializer.class",serializerClass);
871 | * }
872 | */
873 |
874 | props.setProperty("serializer.class",KafkaConstants.DEFAULT_SERIALIZER_CLASS.value);
875 | }
876 |
877 |
878 | /**
879 | * Get the key serializer class.
880 | *
881 | * NOTE: The serializer class for keys (defaults to the same as for messages if nothing is given).
882 | *
883 | * @return
884 | */
885 | public String getKeySerializerClass() {
886 |
887 | return props.getProperty("key.serializer.class");
888 | }
889 |
890 | /**
891 | * Set the key serializer class.
892 | *
893 | * @param keySerializerClass
894 | */
895 | public void setKeySerializerClass(final String keySerializerClass) {
896 |
897 | if (!isNullOrEmpty(keySerializerClass)){
898 |
899 | props.setProperty("key.serializer.class",keySerializerClass);
900 | }
901 | }
902 |
903 | /**
904 | * Get the partitioner class.
905 | *
906 | * NOTE: the default value is :kafka.producer.DefaultPartitioner
907 | * @return partitionerClass
908 | */
909 | public String getPartitionerClass() {
910 |
911 | return props.getProperty("partitioner.class");
912 | }
913 |
914 | /**
915 | * Set the partitioner class.
916 | *
917 | * @param partitionerClass
918 | */
919 | public void setPartitionerClass(final String partitionerClass) {
920 |
921 | if (!isNullOrEmpty(partitionerClass)){
922 |
923 | props.setProperty("partitioner.class",partitionerClass);
924 | }
925 | }
926 |
927 | /**
928 | * Get the compresion codec.
929 | *
930 | * NOTE: the default value is :none
931 | *
932 | * @return compressionCodec
933 | */
934 | public String getCompressionCodec() {
935 |
936 | return props.getProperty("compression.codec");
937 | }
938 |
939 | /**
940 | * Set the compresion codec.
941 | *
942 | * @param compressionCodec
943 | */
944 | public void setCompressionCodec(final String compressionCodec) {
945 |
946 | if (!isNullOrEmpty(compressionCodec)){
947 |
948 | props.setProperty("compression.codec",compressionCodec);
949 | }
950 | }
951 |
952 | /**
953 | * Get compressed topics
954 | *
955 | * NOTE: the default value is : null
956 | *
957 | * @return compressedTopics
958 | */
959 | public String getCompressedTopics() {
960 |
961 | return props.getProperty("compressed.topics");
962 | }
963 |
964 | /**
965 | * Set compressed topics
966 | *
967 | * @param compressedTopics
968 | */
969 | public void setCompressedTopics(final String compressedTopics) {
970 |
971 | if (!isNullOrEmpty(compressedTopics)){
972 |
973 | props.setProperty("compressed.topics",compressedTopics);
974 | }
975 | }
976 |
977 | /**
978 | * Get the number of retries when failures occur when sending.
979 | *
980 | * NOTE: the default value is : 3
981 | *
982 | * @return messageSendMaxRetries
983 | */
984 | public String getMessageSendMaxRetries() {
985 |
986 | return props.getProperty("message.send.max.retries");
987 | }
988 |
989 | /**
990 | * Set the number of retries when failures occur when sending
991 | *
992 | * @param messageSendMaxRetries
993 | */
994 | public void setMessageSendMaxRetries(final String messageSendMaxRetries) {
995 |
996 | if (!isNullOrEmpty(messageSendMaxRetries)){
997 |
998 | props.setProperty("message.send.max.retries",messageSendMaxRetries);
999 | }
1000 | }
1001 |
1002 | /**
1003 | * Get the retry backoff time.
1004 | *
1005 | * NOTE: the default value is : 100
1006 | *
1007 | * @return retryBackoffMs
1008 | */
1009 | public String getRetryBackoffMs() {
1010 |
1011 | return props.getProperty("retry.backoff.ms");
1012 | }
1013 |
1014 | /**
1015 | * Set the retry backoff time.
1016 | *
1017 | * @param retryBackoffMs
1018 | */
1019 | public void setRetryBackoffMs(final String retryBackoffMs) {
1020 |
1021 | if (!isNullOrEmpty(retryBackoffMs)){
1022 |
1023 | props.setProperty("retry.backoff.ms",retryBackoffMs);
1024 | }
1025 | }
1026 |
1027 | /**
1028 | * Get the Topic metadata refresh interval in ms.
1029 | *
1030 | * NOTE: the default value is : 600000
1031 | *
1032 | * @return topicMetadataRefreshIntervalMs
1033 | */
1034 | public String getTopicMetadataRefreshIntervalMs() {
1035 |
1036 | return props.getProperty("topic.metadata.refresh.interval.ms");
1037 | }
1038 |
1039 | /**
1040 | * Set the Topic metadata refresh interval in ms.
1041 | *
1042 | * @param topicMetadataRefreshIntervalMs
1043 | */
1044 | public void setTopicMetadataRefreshIntervalMs(String topicMetadataRefreshIntervalMs) {
1045 |
1046 | if (!isNullOrEmpty(topicMetadataRefreshIntervalMs)){
1047 |
1048 | props.setProperty("topic.metadata.refresh.interval.ms",topicMetadataRefreshIntervalMs);
1049 | }
1050 | }
1051 |
1052 | /**
1053 | * Get the queue buffering max time.
1054 | *
1055 | * NOTE: the default value is : 5000
1056 | *
1057 | * @return queueBufferingMaxMs
1058 | */
1059 | public String getQueueBufferingMaxMs() {
1060 |
1061 | return props.getProperty("queue.buffering.max.ms");
1062 | }
1063 |
1064 | /**
1065 | * Set the queue buffering max time.
1066 | *
1067 | * @param queueBufferingMaxMs
1068 | */
1069 | public void setQueueBufferingMaxMs(String queueBufferingMaxMs) {
1070 |
1071 | if (!isNullOrEmpty(queueBufferingMaxMs)){
1072 |
1073 | props.setProperty("queue.buffering.max.ms",queueBufferingMaxMs);
1074 | }
1075 | }
1076 |
1077 | /**
1078 | * Get the queue buffering max messages.
1079 | *
1080 | * NOTE: the default value is : 10000
1081 | *
1082 | * @return queueBufferingMaxMessages
1083 | */
1084 | public String getQueueBufferingMaxMessages() {
1085 |
1086 | return props.getProperty("queue.buffering.max.messages");
1087 | }
1088 |
1089 | /**
1090 | * Set the queue buffering max messages.
1091 | *
1092 | * @param queueBufferingMaxMessages
1093 | */
1094 | public void setQueueBufferingMaxMessages(final String queueBufferingMaxMessages) {
1095 |
1096 | if (!isNullOrEmpty(queueBufferingMaxMessages)){
1097 |
1098 | props.setProperty("queue.buffering.max.messages",queueBufferingMaxMessages);
1099 | }
1100 | }
1101 |
1102 | /**
1103 | * Get queue enqueue timeout.
1104 | *
1105 | * NOTE: the default value is : -1
1106 | *
1107 | * @return
1108 | */
1109 | public String getQueueEnqueueTimeoutMs() {
1110 |
1111 | return props.getProperty("queue.enqueue.timeout.ms");
1112 | }
1113 |
1114 | /**
1115 | * Set queue enqueue timeout.
1116 | *
1117 | * @param queueEnqueueTimeoutMs
1118 | */
1119 | public void setQueueEnqueueTimeoutMs(String queueEnqueueTimeoutMs) {
1120 |
1121 | if (!isNullOrEmpty(queueEnqueueTimeoutMs)){
1122 |
1123 | props.setProperty("queue.enqueue.timeout.ms",queueEnqueueTimeoutMs);
1124 | }
1125 | }
1126 |
1127 | /**
1128 | * Get the number of messages to send in one batch when using async mode.
1129 | *
1130 | * NOTE: the default value is : 200
1131 | *
1132 | * @return batchNumMessages
1133 | */
1134 | public String getBatchNumMessages() {
1135 |
1136 | return props.getProperty("batch.num.messages");
1137 | }
1138 |
1139 | /**
1140 | * Set the number of messages to send in one batch when using async mode.
1141 | *
1142 | * @param batchNumMessages
1143 | */
1144 | public void setBatchNumMessages(String batchNumMessages) {
1145 |
1146 | if (!isNullOrEmpty(batchNumMessages)){
1147 |
1148 | props.setProperty("batch.num.messages",batchNumMessages);
1149 | }
1150 | }
1151 |
1152 | /**
1153 | * Get the Socket write buffer size.
1154 | *
1155 | * NOTE: the default value is : 100 * 1024
1156 | *
1157 | * @return
1158 | */
1159 | public String getSendBufferBytes() {
1160 |
1161 | return props.getProperty("send.buffer.bytes");
1162 | }
1163 |
1164 | /**
1165 | * Set the Socket write buffer size.
1166 | *
1167 | * @param sendBufferBytes
1168 | */
1169 | public void setSendBufferBytes(String sendBufferBytes) {
1170 |
1171 | if (!isNullOrEmpty(sendBufferBytes)){
1172 | props.setProperty("send.buffer.bytes",sendBufferBytes);
1173 | }
1174 | }
1175 |
1176 | /**
1177 | * Get configuration properties
1178 | *
1179 | * @return
1180 | */
1181 | public Properties getProperties() {
1182 |
1183 | return props;
1184 | }
1185 | }
1186 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaConstants.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | /**
21 | * Camel-Kafka Constants
22 | */
23 | public enum KafkaConstants {
24 |
25 | TOPIC_NAME("KafkaTopicName"),
26 |
27 | /*
28 | Default values from are based on kafka documentation.
29 |
30 | TODO: append description for each property
31 | */
32 |
33 | DEFAULT_CLIENT_ID("kafka.DEFAULT_CLIENT_ID"),
34 | DEFAULT_GROUP("kafka.DEFAULT_GROUP"),
35 | DEFAULT_TOPIC("kafka.DEFAULT_TOPIC"),
36 | DEFAULT_SERIALIZER_CLASS("kafka.serializer.DefaultEncoder"),
37 | DEFAULT_PARTITIONER_CLASS("kafka.producer.DefaultPartitioner"),
38 | DEFAULT_PRODUCER_TYPE("sync"),
39 | DEFAULT_BATCH_NUM_MESSAGES("200"),
40 | DEFAULT_QUEUE_ENQUEUE_TIMEOUT_MS("-1"),
41 | DEFAULT_QUEUE_BUFFERING_MAX_MESSAGES("10000"),
42 | DEFAULT_QUEUE_BUFFERING_MAX_MS("5000"),
43 | DEFAULT_SOCKET_TIMEOUT_MS("30000"),
44 | DEFAULT_SOCKET_RECEIVE_BUFFER_BYTES("65536"),
45 | DEFAULT_FETCH_MESSAGE_MAX_BYTES("1048576"),
46 | DEFAULT_AUTO_COMMIT_ENABLE("true"),
47 | DEFAULT_AUTO_COMMIT_INTERVAL_MS("60000"),
48 | DEFAULT_QUEUED_MAX_MESSAGE_CHUNKS("10"),
49 | DEFAULT_REBALANCE_MAX_RETRIES("4"),
50 | DEFAULT_FETCH_MIN_BYTES("1"),
51 | DEFAULT_FETCH_WAIT_MAX_MS("100"),
52 | DEFAULT_REBALANCE_BACKOFF_MS("2000"),
53 | DEFAULT_REFRESH_LEADER_BACKOFF_MS("200"),
54 | DEFAULT_AUTO_OFFSET_RESET("largest"),
55 | DEFAULT_CONSUMER_TIMEOUT_MS("-1"),
56 | DEFAULT_ZOOKEEPER_SESSION_TIMEOUT_MS("6000"),
57 | DEFAULT_CONNECTION_TIMEOUT_MS("60000"),
58 | DEFAULT_ZOOKEEPER_SYNC_TIME_MS("2000"),
59 | DEFAULT_REQUEST_REQUIRED_ACKS("0"),
60 | DEFAULT_REQUEST_TIMEOUT_MS("10000"),
61 | DEFAULT_COMPRESSION_CODEC("none"),
62 | DEFAULT_MESSAGE_SEND_MAX_RETRIES("3"),
63 | DEFAULT_RETRY_BACKOFF_MS("100"),
64 | DEFAULT_TOPIC_METADATA_REFRESH_INTERVAL_MS("600000"),
65 | DEFAULT_SEND_BUFFER_BYTES("102400"),
66 | // assume that by default key will be of type string
67 | DEFAULT_KEY_SERIALIZER_CLASS("kafka.serializer.StringEncoder"),
68 |
69 | PARTITION_KEY("kafka.PARTITION_KEY"),
70 | PARTITION("kafka.EXCHANGE_NAME"),
71 | OFFSET("kafka.OFFSET"),
72 | KEY("kafka.CONTENT_TYPE"),
73 | TOPIC("kafka.TOPIC");
74 |
75 | /** enum value*/
76 | public final String value;
77 |
78 | /**
79 | * Enum Constructor
80 | *
81 | * @param str
82 | */
83 | private KafkaConstants(String str) {
84 | value = str;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
3 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
4 | * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 | * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 | * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
7 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
8 | * governing permissions and limitations under the License.
9 | */
10 |
11 | package org.apache.camel.component.kafka;
12 |
13 | import static org.apache.camel.component.kafka.KafkaComponentUtil.*;
14 |
15 | import java.util.HashMap;
16 | import java.util.List;
17 | import java.util.Map;
18 | import java.util.concurrent.ExecutorService;
19 |
20 | import kafka.consumer.Consumer;
21 | import kafka.consumer.KafkaStream;
22 | import kafka.javaapi.consumer.ConsumerConnector;
23 |
24 | import org.apache.camel.AsyncProcessor;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.impl.DefaultConsumer;
27 | import org.apache.camel.util.AsyncProcessorConverterHelper;
28 | import org.slf4j.Logger;
29 | import org.slf4j.LoggerFactory;
30 |
31 | /**
32 | * Camel-Kafka {@link DefaultConsumer}.
33 | */
34 | public class KafkaConsumer extends DefaultConsumer {
35 |
36 | /**
37 | * Logger
38 | */
39 | private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConsumer.class);
40 |
41 | /**
42 | * Camel-Kafka consumer processor
43 | */
44 | private final AsyncProcessor processor;
45 |
46 | /**
47 | * Kafka Consumer
48 | */
49 | private ConsumerConnector consumer;
50 |
51 | /**
52 | * Context executor service
53 | */
54 | private ExecutorService executor;
55 |
56 | /**
57 | * Camel-Kafka Configuration
58 | */
59 | private KafkaConfiguration configuration;
60 |
61 | /**
62 | * Camel-Kafka Endpoint reference
63 | */
64 | private KafkaEndpoint endpoint;
65 |
66 | /**
67 | * Default constructor.
68 | *
69 | * @param endpoint
70 | * @param processor
71 | * @param configuration
72 | */
73 | public KafkaConsumer(final KafkaEndpoint endpoint,
74 | final Processor processor,
75 | final KafkaConfiguration configuration) {
76 |
77 | super(endpoint, processor);
78 | this.endpoint = endpoint;
79 | this.configuration = configuration;
80 | this.processor = AsyncProcessorConverterHelper.convert(processor);
81 |
82 | // validate configuration
83 | checkConsumerConfiguration(configuration);
84 |
85 | }
86 |
87 | @Override
88 | protected void doStart() throws Exception {
89 |
90 | super.doStart();
91 |
92 | if (LOGGER.isDebugEnabled()) {
93 |
94 | LOGGER.info("Starting Kafka Consumer");
95 | }
96 |
97 | this.executor = getExecutorService(endpoint, configuration);
98 |
99 | // Kafka Consumer initialisation
100 | consumer = Consumer.createJavaConsumerConnector(createConsumerConfig(configuration));
101 |
102 | final Map topicCountMap = new HashMap();
103 | topicCountMap.put(configuration.getTopicName(), configuration.getConcurrentConsumers());
104 |
105 | final Map>> consumerMap = consumer.createMessageStreams(topicCountMap);
106 |
107 | // create message streams
108 | /*
109 | Kafka Streams
110 | */
111 | List> streams = consumerMap.get(configuration.getTopicName());
112 |
113 | // create different thread to process streams
114 | for (final KafkaStream stream : streams) {
115 |
116 | executor.submit(new KafkaConsumerTask(stream, endpoint, this, processor, configuration));
117 | }
118 | }
119 |
120 | @Override
121 | protected void doStop() throws Exception {
122 |
123 | super.doStop();
124 | if (LOGGER.isDebugEnabled()) {
125 |
126 | LOGGER.info("Stopping Kafka Consumer");
127 | }
128 |
129 | if (consumer != null) {
130 |
131 | consumer.shutdown();
132 | }
133 |
134 | if (executor != null) {
135 |
136 | if (endpoint != null && getEndpoint().getCamelContext() != null) {
137 |
138 | endpoint.getCamelContext().getExecutorServiceManager().shutdownNow(executor);
139 | } else {
140 |
141 | executor.shutdownNow();
142 | }
143 | }
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaConsumerTask.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import kafka.consumer.ConsumerIterator;
21 | import kafka.consumer.KafkaStream;
22 |
23 | import kafka.message.MessageAndMetadata;
24 | import org.apache.camel.Exchange;
25 | import org.apache.camel.Processor;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import static org.apache.camel.component.kafka.KafkaComponentUtil.constructExchange;
30 |
31 | /**
32 | * Kafka Consumer Thread
33 | */
34 | @SuppressWarnings("unchecked")
35 | public class KafkaConsumerTask implements Runnable{
36 |
37 | /**
38 | * Logger
39 | */
40 | private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConsumerTask.class);
41 |
42 | /**
43 | * Camel Kafka endpoint
44 | */
45 | private final KafkaEndpoint endpoint;
46 |
47 | /**
48 | * Camel Processor
49 | */
50 | private final Processor processor;
51 |
52 | /**
53 | * Camel Kafka Configuration
54 | */
55 | private final KafkaConfiguration configuration;
56 |
57 | /**
58 | * Camel Kafka Consumer
59 | */
60 | private final KafkaConsumer consumer;
61 |
62 | /**
63 | * Consumer Iterator
64 | */
65 | private final ConsumerIterator consumerIterator;
66 |
67 | /**
68 | * Default constructor.
69 | *
70 | * NOTE: when used as consumer thread
71 | */
72 | public KafkaConsumerTask(final KafkaStream stream,
73 | final KafkaEndpoint endpoint,
74 | final KafkaConsumer consumer,
75 | final Processor processor,
76 | final KafkaConfiguration configuration) {
77 |
78 | this.consumerIterator = stream.iterator();
79 | this.endpoint = endpoint;
80 | this.consumer = consumer;
81 | this.processor = processor;
82 | this.configuration = configuration;
83 | }
84 |
85 | /**
86 | * {@inheritDoc}
87 | */
88 | @Override
89 | public void run() {
90 |
91 | if (LOGGER.isDebugEnabled()) {
92 |
93 | LOGGER.debug("Camel Kafka Consumer started.");
94 | }
95 |
96 | while (consumerIterator.hasNext()) {
97 |
98 | MessageAndMetadata incomingData = consumerIterator.next();
99 |
100 | if (incomingData != null) {
101 |
102 | if (LOGGER.isDebugEnabled()) {
103 |
104 | LOGGER.debug("Kafka Consumer Message received : " + incomingData);
105 | }
106 |
107 | final Exchange exchange = constructExchange(endpoint, incomingData, configuration);
108 |
109 | try {
110 |
111 | this.processor.process(exchange);
112 | } catch (Exception e) {
113 |
114 | LOGGER.error("Error processing the message:", e);
115 | } finally {
116 |
117 | if (exchange.getException() != null) {
118 |
119 | consumer.getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
120 | }
121 | }
122 | }
123 | }
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaEndpoint.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import org.apache.camel.Component;
21 | import org.apache.camel.Consumer;
22 | import org.apache.camel.Processor;
23 | import org.apache.camel.Producer;
24 | import org.apache.camel.impl.DefaultEndpoint;
25 |
26 | /**
27 | * Camel-Kafka {@link DefaultEndpoint}.
28 | */
29 | public class KafkaEndpoint extends DefaultEndpoint {
30 |
31 | /**
32 | * Camel Kafka Configuration
33 | */
34 | private KafkaConfiguration configuration;
35 |
36 | /**
37 | * Kafka endpoint Constructor
38 | *
39 | * @param endpointUri
40 | * @param component
41 | * @param configuration
42 | */
43 | public KafkaEndpoint(final String endpointUri,
44 | final Component component,
45 | final KafkaConfiguration configuration) {
46 |
47 | super(endpointUri, component);
48 | this.configuration = configuration;
49 | }
50 |
51 | /**
52 | * {@inheritDoc}
53 | */
54 | @Override
55 | public Producer createProducer() throws Exception {
56 |
57 | return new KafkaProducer(this, this.configuration);
58 | }
59 |
60 | /**
61 | * {@inheritDoc}
62 | */
63 | @Override
64 | public Consumer createConsumer(Processor processor) throws Exception {
65 |
66 | return new KafkaConsumer(this, processor, this.configuration);
67 | }
68 |
69 | /**
70 | * {@inheritDoc}
71 | */
72 | @Override
73 | public boolean isSingleton() {
74 |
75 | return true;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/camel/component/kafka/KafkaProducer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import static org.apache.camel.component.kafka.KafkaComponentUtil.checkProducerConfiguration;
21 | import static org.apache.camel.component.kafka.KafkaComponentUtil.serializeBody;
22 | import static org.apache.camel.component.kafka.KafkaComponentUtil.serializeExchange;
23 |
24 | import kafka.javaapi.producer.Producer;
25 | import kafka.producer.KeyedMessage;
26 | import kafka.producer.ProducerConfig;
27 |
28 | import org.apache.camel.AsyncCallback;
29 | import org.apache.camel.AsyncProcessor;
30 | import org.apache.camel.Endpoint;
31 | import org.apache.camel.Exchange;
32 | import org.apache.camel.impl.DefaultProducer;
33 | import org.slf4j.Logger;
34 | import org.slf4j.LoggerFactory;
35 |
36 | /**
37 | * Camel-Kafka {@link DefaultProducer}
38 | */
39 | public class KafkaProducer extends DefaultProducer implements AsyncProcessor {
40 |
41 | /**
42 | * Logger
43 | */
44 | private static final Logger LOGGER = LoggerFactory.getLogger(KafkaProducer.class);
45 |
46 | /**
47 | * Kafka Producer
48 | */
49 | private final kafka.javaapi.producer.Producer producer;
50 |
51 | /**
52 | * Camel-Kafka Configuration
53 | */
54 | private KafkaConfiguration configuration;
55 |
56 | /**
57 | * Default constructor to be used.
58 | *
59 | * @param endpoint
60 | */
61 | public KafkaProducer(final Endpoint endpoint,
62 | final KafkaConfiguration configuration) {
63 | super(endpoint);
64 |
65 | checkProducerConfiguration(configuration);
66 | this.configuration = configuration;
67 |
68 | /* Create Kafka Producer */
69 | final ProducerConfig config = new ProducerConfig(configuration.getProperties());
70 | producer = new Producer(config);
71 | }
72 |
73 | @Override
74 | public final void process(Exchange exchange) throws Exception {
75 |
76 | checkAndSend(exchange);
77 | }
78 |
79 | @Override
80 | public final boolean process(final Exchange exchange,
81 | final AsyncCallback callback) {
82 |
83 | checkAndSend(exchange);
84 | callback.done(true);
85 | return true;
86 | }
87 |
88 | /**
89 | * Utility method to prepare and send data
90 | *
91 | *
92 | * TODO:
93 | * modify to be able to make use of Encoders other than Default
94 | *
95 | * @param exchange
96 | */
97 | private void checkAndSend(final Exchange exchange) {
98 |
99 | String partitionKey;
100 | if (exchange.getIn().getHeaders().containsKey(KafkaConstants.PARTITION_KEY.value)) {
101 |
102 | partitionKey = exchange.getIn().getHeader(KafkaConstants.PARTITION_KEY.value, String.class);
103 | } else {
104 | LOGGER.info("No partition key set, using the default");
105 |
106 | partitionKey = configuration.getPartitionKey();
107 | }
108 |
109 | String topicName;
110 | if (exchange.getIn().getHeaders().containsKey(KafkaConstants.TOPIC_NAME.value)) {
111 |
112 | topicName = exchange.getIn().getHeader(KafkaConstants.TOPIC_NAME.value, String.class);
113 | } else {
114 |
115 | topicName = configuration.getTopicName();
116 | }
117 |
118 | final KeyedMessage message;
119 |
120 | if (configuration.isTransferExchange()){ //should transfer exchange?
121 |
122 | message = new KeyedMessage(topicName, partitionKey, serializeExchange(exchange));
123 | } else{
124 |
125 | message = new KeyedMessage(topicName, partitionKey, serializeBody(exchange));
126 | }
127 |
128 | producer.send(message);
129 |
130 | if (LOGGER.isDebugEnabled()) {
131 |
132 | LOGGER.debug("Kafka Producer send : " + exchange);
133 | }
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
204 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/NOTICE.txt:
--------------------------------------------------------------------------------
1 | =========================================================================
2 | == NOTICE file corresponding to the section 4 d of ==
3 | == the Apache License, Version 2.0, ==
4 | == in this case for the Apache Camel distribution. ==
5 | =========================================================================
6 |
7 | This product includes software developed by
8 | The Apache Software Foundation (http://www.apache.org/).
9 |
10 | Please read the different LICENSE files present in the licenses directory of
11 | this distribution.
12 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/services/org/apache/camel/component/kafka:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | class=org.apache.camel.component.kafka.KafkaComponent
19 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/KafkaConsumerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
3 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
4 | * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 | * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 | * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
7 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
8 | * governing permissions and limitations under the License.
9 | */
10 |
11 | package org.apache.camel.component.kafka;
12 |
13 | import static org.mockito.Mockito.*;
14 |
15 | import org.apache.camel.Exchange;
16 | import org.apache.camel.ExchangePattern;
17 | import org.apache.camel.Message;
18 | import org.apache.camel.Processor;
19 | import org.junit.Before;
20 | import org.junit.Ignore;
21 | import org.junit.Test;
22 |
23 | /**
24 | * Camel-Kafka Producer Tests
25 | */
26 | public class KafkaConsumerTest extends KafkaTestSupport {
27 |
28 | /**
29 | * Mock Camel-Kafka Endpoint
30 | */
31 | private KafkaEndpoint mockKafkaEndpoint;
32 |
33 | /**
34 | * Mock Camel-Kafka Configuration
35 | */
36 | private KafkaConfiguration mockKafkaConfiguration;
37 |
38 | /**
39 | * Mock Processor
40 | */
41 | private Processor mockKafkaProcessor;
42 |
43 | @Before
44 | public void setUp() {
45 |
46 | // setup mocks
47 |
48 | Exchange mockCamelExchange = mock(Exchange.class);
49 | mockKafkaEndpoint = mock(KafkaEndpoint.class);
50 | mockKafkaConfiguration = spy(new KafkaConfiguration());
51 | /*
52 | * Mock Camel Message
53 | */
54 | Message mockCamelMessage = mock(Message.class);
55 | mockKafkaProcessor = mock(Processor.class);
56 |
57 | // setup default conditions
58 | when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage);
59 | when(mockCamelExchange.getPattern()).thenReturn(ExchangePattern.InOnly);
60 | }
61 |
62 | public void creatingAnInstanceShouldNotThrowExceptionIfConfigurationIsMissingGroupId() throws Exception {
63 |
64 | mockKafkaConfiguration.setZookeeperConnect("samplehost");
65 |
66 | new KafkaConsumer(mockKafkaEndpoint, mockKafkaProcessor, mockKafkaConfiguration);
67 |
68 | verify(mockKafkaConfiguration, atMost(1)).getGroupId();
69 | verify(mockKafkaConfiguration, atMost(1)).getZkConnect();
70 | verify(mockKafkaConfiguration, atMost(1)).getZookeeperConnect();
71 | }
72 |
73 | public void creatingAnInstanceShouldNotThrowExceptionIfConfigurationIsMissingZKConnect() throws Exception {
74 |
75 | mockKafkaConfiguration.setGroupId(KafkaConstants.DEFAULT_GROUP.value);
76 |
77 | new KafkaConsumer(mockKafkaEndpoint, mockKafkaProcessor, mockKafkaConfiguration);
78 |
79 | verify(mockKafkaConfiguration, atMost(1)).getGroupId();
80 | verify(mockKafkaConfiguration, atMost(1)).getZkConnect();
81 | verify(mockKafkaConfiguration, atMost(1)).getZookeeperConnect();
82 | }
83 |
84 | public void creatingAnInstanceShouldNotFailIfFakeHostIsProvidedInMandatoryConfiguration() throws Exception {
85 |
86 | mockKafkaConfiguration.setZookeeperConnect("sampleHost");
87 | mockKafkaConfiguration.setGroupId(KafkaConstants.DEFAULT_GROUP.value);
88 |
89 | new KafkaConsumer(mockKafkaEndpoint, mockKafkaProcessor, mockKafkaConfiguration);
90 |
91 | verify(mockKafkaConfiguration, atMost(1)).getGroupId();
92 | verify(mockKafkaConfiguration, atMost(1)).getZkConnect();
93 | verify(mockKafkaConfiguration, atMost(1)).getZookeeperConnect();
94 | }
95 |
96 | @Test
97 | //@Ignore("This test requires an instance of zookeeper running localy")
98 | public void creatingAnInstanceShouldSuccessedIfMandatoryConfigurationIsProvided() throws Exception {
99 |
100 | mockKafkaConfiguration.setZookeeperConnect("localhost:2181");
101 | mockKafkaConfiguration.setGroupId(KafkaConstants.DEFAULT_GROUP.value);
102 |
103 | new KafkaConsumer(mockKafkaEndpoint, mockKafkaProcessor, mockKafkaConfiguration);
104 |
105 | verify(mockKafkaConfiguration, atMost(1)).getGroupId();
106 | verify(mockKafkaConfiguration, atMost(1)).getZkConnect();
107 | verify(mockKafkaConfiguration, atMost(1)).getZookeeperConnect();
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/KafkaEndpointTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.camel.component.kafka;
18 |
19 | import java.net.URISyntaxException;
20 |
21 | import kafka.message.Message;
22 | import kafka.message.MessageAndMetadata;
23 |
24 | import kafka.serializer.DefaultDecoder;
25 | import org.apache.camel.Endpoint;
26 | import org.apache.camel.Exchange;
27 | import org.junit.Test;
28 |
29 | import static org.junit.Assert.assertEquals;
30 | import static org.junit.Assert.assertTrue;
31 | import static org.mockito.Mockito.mock;
32 |
33 | public class KafkaEndpointTest {
34 |
35 | @Test
36 | public void assertSingleton() throws URISyntaxException {
37 |
38 | KafkaEndpoint endpoint = new KafkaEndpoint("kafka:test", mock(KafkaComponent.class), mock(KafkaConfiguration.class));
39 | assertTrue(endpoint.isSingleton());
40 | }
41 |
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/KafkaProducerTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
3 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
4 | * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 | * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 | * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
7 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
8 | * governing permissions and limitations under the License.
9 | */
10 |
11 | package org.apache.camel.component.kafka;
12 |
13 | import static org.mockito.Mockito.atMost;
14 | import static org.mockito.Mockito.verify;
15 | import static org.powermock.api.mockito.PowerMockito.*;
16 |
17 | import org.apache.camel.Exchange;
18 | import org.apache.camel.ExchangePattern;
19 | import org.apache.camel.Message;
20 | import org.junit.Before;
21 | import org.junit.Test;
22 |
23 | /**
24 | * Camel-Kafka Producer Tests
25 | */
26 | public class KafkaProducerTest extends KafkaTestSupport {
27 |
28 | /**
29 | * Mock Camel-Kafka Endpoint
30 | */
31 | private KafkaEndpoint mockKafkaEndpoint;
32 |
33 | /**
34 | * Mock Camel-Kafka Configuration
35 | */
36 | private KafkaConfiguration mockKafkaConfiguration;
37 |
38 | @Before
39 | public void setUp() {
40 |
41 | // setup mocks
42 |
43 | /*
44 | * Mock CamelExchange
45 | */
46 | Exchange mockCamelExchange = mock(Exchange.class);
47 | mockKafkaEndpoint = mock(KafkaEndpoint.class);
48 | mockKafkaConfiguration = spy(new KafkaConfiguration());
49 | /*
50 | * Mock Camel Message
51 | */
52 | Message mockCamelMessage = mock(Message.class);
53 |
54 | // setup default conditions
55 | when(mockCamelExchange.getIn()).thenReturn(mockCamelMessage);
56 | when(mockCamelExchange.getPattern()).thenReturn(ExchangePattern.InOnly);
57 | }
58 |
59 | @Test
60 | public void creatingAnInstanceShouldSuccessedIfConfigurationIsNotMissing() throws Exception {
61 |
62 | mockKafkaConfiguration.setMetadataBrokerList("sampleBroker:11");
63 |
64 | new KafkaProducer(mockKafkaEndpoint, mockKafkaConfiguration);
65 |
66 | verify(mockKafkaConfiguration, atMost(1)).getMetadataBrokerList();
67 | verify(mockKafkaConfiguration, atMost(1)).getSerializerClass();
68 | verify(mockKafkaConfiguration, atMost(1)).getPartitionerClass();
69 | verify(mockKafkaConfiguration, atMost(1)).getRequestRequiredAcks();
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/KafkaTestSupport.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka;
19 |
20 | import org.apache.camel.test.junit4.CamelTestSupport;
21 |
22 | /**
23 | * Camel-Kafka Test Support
24 | */
25 | public class KafkaTestSupport extends CamelTestSupport {
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/AsyncInOnlyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic InOnly Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class AsyncInOnlyTest extends KafkaTestSupport {
38 |
39 | @EndpointInject(uri = "mock:result")
40 | private MockEndpoint mock;
41 |
42 | final long uid = new Random().nextLong();
43 |
44 | @Test
45 | public void asyncInOnlyTest() throws Exception {
46 |
47 | final String TEST_PAYLOAD = "Test Payload InOnly!";
48 |
49 | mock.expectedMessageCount(1);
50 | mock.expectedBodiesReceived(TEST_PAYLOAD);
51 |
52 | template.send("direct:kaiotep", ExchangePattern.InOnly, new Processor() {
53 | public void process(Exchange exchange) throws Exception {
54 |
55 | exchange.getIn().setBody(TEST_PAYLOAD);
56 | }
57 | });
58 |
59 | assertMockEndpointsSatisfied();
60 | mock.reset();
61 | }
62 |
63 | @Override
64 | protected RouteBuilder createRouteBuilder() throws Exception {
65 |
66 | return new RouteBuilder() {
67 | @Override
68 | public void configure() throws Exception {
69 |
70 | from("direct:kaiotep").to("kafka:kaiot?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&producerType=async&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
71 | from("kafka:kaiot?zkConnect=localhost:2181&groupId="+ uid +KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
72 | }
73 | };
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/AsyncInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic InOut Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class AsyncInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void asyncInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut!";
49 |
50 | mock.expectedMessageCount(1);
51 | mock.expectedBodiesReceived(TEST_PAYLOAD);
52 |
53 | template.send("direct:kaiouttep", ExchangePattern.InOut, new Processor() {
54 | public void process(Exchange exchange) throws Exception {
55 |
56 | exchange.getIn().setBody(TEST_PAYLOAD);
57 | }
58 | });
59 |
60 | assertMockEndpointsSatisfied();
61 | mock.reset();
62 | }
63 |
64 | @Override
65 | protected RouteBuilder createRouteBuilder() throws Exception {
66 |
67 | return new RouteBuilder() {
68 | @Override
69 | public void configure() throws Exception {
70 |
71 | from("direct:kaiouttep").to("kafka:kaioutt?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&producerType=async&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
72 | from("kafka:kaioutt?zkConnect=localhost:2181&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
73 | }
74 | };
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/AsyncTransferEnxchangeInOnlyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOnly Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class AsyncTransferEnxchangeInOnlyTest extends KafkaTestSupport {
38 |
39 | @EndpointInject(uri = "mock:result")
40 | private MockEndpoint mock;
41 |
42 | final long uid = new Random().nextLong();
43 |
44 | @Test
45 | public void asyncTransferEnxchangeInOnlyTest() throws Exception {
46 |
47 | final String TEST_PAYLOAD = "Test Payload InOnly!";
48 | final String TEST_HEADER = "Test.header";
49 | final String TEST_HEADER_VALUE = "test.header.value";
50 |
51 | mock.expectedMessageCount(1);
52 | mock.expectedBodiesReceived(TEST_PAYLOAD);
53 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
54 |
55 | template.send("direct:ateioep", ExchangePattern.InOnly, new Processor() {
56 | public void process(Exchange exchange) throws Exception {
57 |
58 | exchange.getIn().setBody(TEST_PAYLOAD);
59 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
60 | }
61 | });
62 |
63 | assertMockEndpointsSatisfied();
64 | mock.reset();
65 | }
66 |
67 | @Override
68 | protected RouteBuilder createRouteBuilder() throws Exception {
69 |
70 | return new RouteBuilder() {
71 | @Override
72 | public void configure() throws Exception {
73 |
74 | from("direct:ateioep").to("kafka:ateio?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&transferExchange=true&producerType=async&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
75 | from("kafka:ateio?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid +KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
76 | }
77 | };
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/AsyncTransferExchangeInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class AsyncTransferExchangeInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void asyncTransferExchangeInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut!";
49 | final String TEST_HEADER = "Test.header";
50 | final String TEST_HEADER_VALUE = "test.header.value";
51 |
52 | mock.expectedMessageCount(1);
53 | mock.expectedBodiesReceived(TEST_PAYLOAD);
54 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
55 |
56 | template.send("direct:ateioutep", ExchangePattern.InOut, new Processor() {
57 | public void process(Exchange exchange) throws Exception {
58 |
59 | exchange.getIn().setBody(TEST_PAYLOAD);
60 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
61 | }
62 | });
63 |
64 | assertMockEndpointsSatisfied();
65 | mock.reset();
66 | }
67 |
68 | @Override
69 | protected RouteBuilder createRouteBuilder() throws Exception {
70 |
71 | return new RouteBuilder() {
72 | @Override
73 | public void configure() throws Exception {
74 |
75 | from("direct:ateioutep").to("kafka:ateiout?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&transferExchange=true&producerType=async&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
76 | from("kafka:ateiout?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
77 | }
78 | };
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/KafkaConsumerIT.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.camel.component.kafka.itests;
18 |
19 | import java.io.IOException;
20 | import java.util.Properties;
21 |
22 | import kafka.javaapi.producer.Producer;
23 | import kafka.producer.KeyedMessage;
24 | import kafka.producer.ProducerConfig;
25 |
26 | import org.apache.camel.Endpoint;
27 | import org.apache.camel.EndpointInject;
28 | import org.apache.camel.builder.RouteBuilder;
29 | import org.apache.camel.component.kafka.KafkaConfiguration;
30 | import org.apache.camel.component.mock.MockEndpoint;
31 | import org.apache.camel.test.junit4.CamelTestSupport;
32 | import org.junit.After;
33 | import org.junit.Before;
34 | import org.junit.Ignore;
35 | import org.junit.Test;
36 |
37 | import static org.apache.commons.lang.SerializationUtils.serialize;
38 |
39 | /**
40 | * The Producer IT tests require a Kafka broker running on 9092 and a zookeeper instance running on 2181.
41 | * The broker must have a topic called test created.
42 | */
43 | @Ignore("to run manually!")
44 | public class KafkaConsumerIT extends CamelTestSupport {
45 |
46 | public static final String TOPIC = "test";
47 |
48 | @EndpointInject(uri = "mock:result")
49 | private MockEndpoint to;
50 |
51 | private Producer producer;
52 |
53 | @Before
54 | public void before() {
55 |
56 | KafkaConfiguration conf = new KafkaConfiguration();
57 |
58 | conf.setZkConnect("localhost:2181");
59 | conf.setMetadataBrokerList("localhost:9092");
60 |
61 | ProducerConfig config = new ProducerConfig(conf.getProperties());
62 | producer = new Producer(config);
63 | }
64 |
65 | @After
66 | public void after() {
67 | producer.close();
68 | }
69 |
70 | @Override
71 | protected RouteBuilder createRouteBuilder() throws Exception {
72 | return new RouteBuilder() {
73 |
74 | @Override
75 | public void configure() throws Exception {
76 |
77 | from("kafka:"+TOPIC+"?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&groupId=1")
78 | .to("mock:result");
79 | }
80 | };
81 | }
82 |
83 | /**
84 | * NOTE:
85 | * it can use only Default Encoder for now and therefore needs
86 | * the data to be of type byte[]
87 | */
88 | @Test
89 | public void kaftMessageIsConsumedByCamel() throws InterruptedException, IOException {
90 |
91 | to.expectedMessageCount(5);
92 | to.expectedBodiesReceived("message-0", "message-1", "message-2", "message-3", "message-4");
93 |
94 | for (int k = 0; k < 5; k++) {
95 |
96 | String msg = "message-" + k;
97 | KeyedMessage data = new KeyedMessage(TOPIC, "1", serialize(msg));
98 | producer.send(data);
99 | }
100 |
101 | to.assertIsSatisfied(3000);
102 | }
103 | }
104 |
105 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SimplePartitionerUseInAsyncInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class SimplePartitionerUseInAsyncInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void SimplePartitionerUseInAsyncInOutTest() throws Exception {
46 |
47 | final String TEST_PAYLOAD = "Test Payload InOut!";
48 |
49 | mock.expectedMessageCount(1);
50 | mock.expectedBodiesReceived(TEST_PAYLOAD);
51 |
52 |
53 | template.send("direct:spuiaioutep", ExchangePattern.InOut, new Processor() {
54 | public void process(Exchange exchange) throws Exception {
55 |
56 | exchange.getIn().setBody(TEST_PAYLOAD);
57 | }
58 | });
59 |
60 | assertMockEndpointsSatisfied();
61 | mock.reset();
62 | }
63 |
64 | @Override
65 | protected RouteBuilder createRouteBuilder() throws Exception {
66 |
67 | return new RouteBuilder() {
68 | @Override
69 | public void configure() throws Exception {
70 |
71 | from("direct:spuiaioutep").to("kafka:spuiaiout?zkConnect=localhost:2181&partitionerClass=org.apache.camel.component.kafka.partitioner.SimplePartitioner&producerType=async&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
72 | from("kafka:spuiaiout?zkConnect=localhost:2181&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
73 | }
74 | };
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SimplePartitionerUseInAsyncTransferExchangeInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class SimplePartitionerUseInAsyncTransferExchangeInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void simplePartitionerUseInAsyncTransferExchangeInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut!";
49 | final String TEST_HEADER = "Test.header";
50 | final String TEST_HEADER_VALUE = "test.header.value";
51 |
52 | mock.expectedMessageCount(1);
53 | mock.expectedBodiesReceived(TEST_PAYLOAD);
54 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
55 |
56 | template.send("direct:spuiateioutep", ExchangePattern.InOut, new Processor() {
57 | public void process(Exchange exchange) throws Exception {
58 |
59 | exchange.getIn().setBody(TEST_PAYLOAD);
60 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
61 | }
62 | });
63 |
64 | assertMockEndpointsSatisfied();
65 | mock.reset();
66 | }
67 |
68 | @Override
69 | protected RouteBuilder createRouteBuilder() throws Exception {
70 |
71 | return new RouteBuilder() {
72 | @Override
73 | public void configure() throws Exception {
74 |
75 | from("direct:spuiateioutep").to("kafka:spuiateiout?zkConnect=localhost:2181&transferExchange=true&partitionerClass=org.apache.camel.component.kafka.partitioner.SimplePartitioner&producerType=async&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
76 | from("kafka:spuiateiout?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
77 | }
78 | };
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SimplePartitionerUseInSyncInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic InOut Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class SimplePartitionerUseInSyncInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void simplePartitionerUseInSyncInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut with Simple Partitioner !";
49 |
50 | mock.expectedMessageCount(1);
51 | mock.expectedBodiesReceived(TEST_PAYLOAD);
52 |
53 | template.send("direct:spuisioutep", ExchangePattern.InOut, new Processor() {
54 | public void process(Exchange exchange) throws Exception {
55 |
56 | exchange.getIn().setBody(TEST_PAYLOAD);
57 | }
58 | });
59 |
60 | assertMockEndpointsSatisfied();
61 | mock.reset();
62 | }
63 |
64 | @Override
65 | protected RouteBuilder createRouteBuilder() throws Exception {
66 |
67 | return new RouteBuilder() {
68 | @Override
69 | public void configure() throws Exception {
70 |
71 | from("direct:spuisioutep").to("kafka:spuisiout?zkConnect=localhost:2181&partitionerClass=org.apache.camel.component.kafka.partitioner.SimplePartitioner&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
72 | from("kafka:spuisiout?zkConnect=localhost:2181&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
73 | }
74 | };
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SimplePartitionerUseInSyncTransferExchangeInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration tests
35 | */
36 | @Ignore("to run manually!")
37 | public class SimplePartitionerUseInSyncTransferExchangeInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void simplePartitionerUseInSyncTransferExchangeInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut with Simple Partitioner !";
49 | final String TEST_HEADER = "Test.header";
50 | final String TEST_HEADER_VALUE = "test.header.value";
51 |
52 | mock.expectedMessageCount(1);
53 | mock.expectedBodiesReceived(TEST_PAYLOAD);
54 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
55 |
56 | template.send("direct:bioouttps", ExchangePattern.InOut, new Processor() {
57 | public void process(Exchange exchange) throws Exception {
58 |
59 | exchange.getIn().setBody(TEST_PAYLOAD);
60 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
61 | }
62 | });
63 |
64 | assertMockEndpointsSatisfied();
65 | mock.reset();
66 | }
67 |
68 | @Override
69 | protected RouteBuilder createRouteBuilder() throws Exception {
70 |
71 | return new RouteBuilder() {
72 | @Override
73 | public void configure() throws Exception {
74 |
75 | from("direct:bioouttps").to("kafka:fooioutps?zkConnect=localhost:2181&transferExchange=true&partitionerClass=org.apache.camel.component.kafka.partitioner.SimplePartitioner&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
76 | from("kafka:fooioutps?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
77 | }
78 | };
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SpringSupportTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import org.apache.camel.EndpointInject;
21 | import org.apache.camel.component.mock.MockEndpoint;
22 | import org.apache.camel.test.spring.CamelSpringTestSupport;
23 | import org.junit.Ignore;
24 | import org.junit.Test;
25 | import org.springframework.context.support.ClassPathXmlApplicationContext;
26 |
27 | /**
28 | * Transfer Exchange Spring support tests
29 | */
30 | @Ignore("to run manually!")
31 | public class SpringSupportTest extends CamelSpringTestSupport {
32 |
33 | /**
34 | * Mock Endpoint used for verification
35 | * of the results.
36 | */
37 | @EndpointInject(uri = "mock:result")
38 | private MockEndpoint mock;
39 |
40 | /**
41 | * Test the Camel-Kafka, set topic by headers and sync producer
42 | *
43 | * @throws Exception
44 | */
45 | @Test
46 | public void springSupportTest() throws Exception {
47 |
48 | final String PAYLOAD = "SpringBasicTest";
49 |
50 | mock.reset(); // reset the mock
51 | template.sendBody("direct:basicTest", PAYLOAD);
52 |
53 | //setup the actual expectation
54 | mock.expectedMessageCount(1);
55 | mock.expectedBodiesReceived(PAYLOAD);
56 |
57 | assertMockEndpointsSatisfied();
58 | mock.reset();
59 | }
60 |
61 | /**
62 | * load spring context
63 | *
64 | * @return
65 | */
66 | @Override
67 | protected ClassPathXmlApplicationContext createApplicationContext() {
68 |
69 | return new ClassPathXmlApplicationContext("META-INF/spring/basic-spring-tests.xml");
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SyncInOnlyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic InOnly Integration test
35 | */
36 | @Ignore("to run manually!")
37 | public class SyncInOnlyTest extends KafkaTestSupport {
38 |
39 | @EndpointInject(uri = "mock:result")
40 | private MockEndpoint mock;
41 |
42 | final long uid = new Random().nextLong();
43 |
44 | @Test
45 | public void syncInOnlyTest() throws Exception {
46 |
47 | final String TEST_PAYLOAD = "Test Payload InOnly!";
48 |
49 | mock.expectedMessageCount(1);
50 | mock.expectedBodiesReceived(TEST_PAYLOAD);
51 |
52 | template.send("direct:sioep", ExchangePattern.InOnly, new Processor() {
53 | public void process(Exchange exchange) throws Exception {
54 |
55 | exchange.getIn().setBody(TEST_PAYLOAD);
56 | }
57 | });
58 |
59 | assertMockEndpointsSatisfied();
60 | mock.reset();
61 | }
62 |
63 | @Override
64 | protected RouteBuilder createRouteBuilder() throws Exception {
65 |
66 | return new RouteBuilder() {
67 | @Override
68 | public void configure() throws Exception {
69 |
70 | from("direct:sioep").to("kafka:sio?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
71 | from("kafka:sio?zkConnect=localhost:2181&groupId="+ uid +KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
72 | }
73 | };
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SyncInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration tests
35 | */
36 | @Ignore("to run manually!")
37 | public class SyncInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void syncInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut!";
49 |
50 | mock.expectedMessageCount(1);
51 | mock.expectedBodiesReceived(TEST_PAYLOAD);
52 |
53 | template.send("direct:sioutep", ExchangePattern.InOut, new Processor() {
54 | public void process(Exchange exchange) throws Exception {
55 |
56 | exchange.getIn().setBody(TEST_PAYLOAD);
57 | }
58 | });
59 |
60 | assertMockEndpointsSatisfied();
61 | mock.reset();
62 | }
63 |
64 | @Override
65 | protected RouteBuilder createRouteBuilder() throws Exception {
66 |
67 | return new RouteBuilder() {
68 | @Override
69 | public void configure() throws Exception {
70 |
71 | from("direct:sioutep").to("kafka:siout?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
72 | from("kafka:siout?zkConnect=localhost:2181&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
73 | }
74 | };
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SyncTransferExchangeInOnlyTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOnly Integration tests
35 | */
36 | @Ignore("to run manually!")
37 | public class SyncTransferExchangeInOnlyTest extends KafkaTestSupport {
38 |
39 | @EndpointInject(uri = "mock:result")
40 | private MockEndpoint mock;
41 |
42 | final long uid = new Random().nextLong();
43 |
44 | @Test
45 | public void syncTransferExchangeInOnlyTest() throws Exception {
46 |
47 | final String TEST_PAYLOAD = "Test Payload InOnly!";
48 | final String TEST_HEADER = "Test.header";
49 | final String TEST_HEADER_VALUE = "test.header.value";
50 |
51 | mock.expectedMessageCount(1);
52 | mock.expectedBodiesReceived(TEST_PAYLOAD);
53 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
54 |
55 | template.send("direct:steioep", ExchangePattern.InOnly, new Processor() {
56 | public void process(Exchange exchange) throws Exception {
57 |
58 | exchange.getIn().setBody(TEST_PAYLOAD);
59 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
60 | }
61 | });
62 |
63 | assertMockEndpointsSatisfied();
64 | mock.reset();
65 | }
66 |
67 | @Override
68 | protected RouteBuilder createRouteBuilder() throws Exception {
69 |
70 | return new RouteBuilder() {
71 | @Override
72 | public void configure() throws Exception {
73 |
74 | from("direct:steioep").to("kafka:steio?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
75 | from("kafka:steio?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid +KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
76 | }
77 | };
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/SyncTransferExchangeInOutTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import java.util.Random;
21 |
22 | import org.apache.camel.EndpointInject;
23 | import org.apache.camel.Exchange;
24 | import org.apache.camel.ExchangePattern;
25 | import org.apache.camel.Processor;
26 | import org.apache.camel.builder.RouteBuilder;
27 | import org.apache.camel.component.kafka.KafkaConstants;
28 | import org.apache.camel.component.kafka.KafkaTestSupport;
29 | import org.apache.camel.component.mock.MockEndpoint;
30 | import org.junit.Ignore;
31 | import org.junit.Test;
32 |
33 | /**
34 | * Camel-Kafka Basic Transfer Exchange InOut Integration tests
35 | */
36 | @Ignore("to run manually!")
37 | public class SyncTransferExchangeInOutTest extends KafkaTestSupport {
38 |
39 | final long uid = new Random().nextLong();
40 |
41 | @EndpointInject(uri = "mock:result")
42 | private MockEndpoint mock;
43 |
44 | @Test
45 | public void syncTransferExchangeInOutTest() throws Exception {
46 |
47 |
48 | final String TEST_PAYLOAD = "Test Payload InOut!";
49 | final String TEST_HEADER = "Test.header";
50 | final String TEST_HEADER_VALUE = "test.header.value";
51 |
52 | mock.expectedMessageCount(1);
53 | mock.expectedBodiesReceived(TEST_PAYLOAD);
54 | mock.expectedHeaderReceived(TEST_HEADER, TEST_HEADER_VALUE);
55 |
56 | template.send("direct:steioutep", ExchangePattern.InOut, new Processor() {
57 | public void process(Exchange exchange) throws Exception {
58 |
59 | exchange.getIn().setBody(TEST_PAYLOAD);
60 | exchange.getIn().setHeader(TEST_HEADER, TEST_HEADER_VALUE);
61 | }
62 | });
63 |
64 | assertMockEndpointsSatisfied();
65 | mock.reset();
66 | }
67 |
68 | @Override
69 | protected RouteBuilder createRouteBuilder() throws Exception {
70 |
71 | return new RouteBuilder() {
72 | @Override
73 | public void configure() throws Exception {
74 |
75 | from("direct:steioutep").to("kafka:steiout?zkConnect=localhost:2181&metadataBrokerList=localhost:9092&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value);
76 | from("kafka:steiout?zkConnect=localhost:2181&transferExchange=true&groupId="+ uid + KafkaConstants.DEFAULT_GROUP.value).to("mock:result");
77 | }
78 | };
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/itests/TransferExchangeSpringSupportTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.itests;
19 |
20 | import org.apache.camel.EndpointInject;
21 | import org.apache.camel.component.mock.MockEndpoint;
22 | import org.apache.camel.test.spring.CamelSpringTestSupport;
23 | import org.junit.Ignore;
24 | import org.junit.Test;
25 | import org.springframework.context.support.ClassPathXmlApplicationContext;
26 |
27 | /**
28 | * Transfer Exchange Spring support tests
29 | */
30 | @Ignore("to run manually!")
31 | public class TransferExchangeSpringSupportTest extends CamelSpringTestSupport {
32 |
33 | /**
34 | * Mock Endpoint used for verification
35 | * of the results.
36 | */
37 | @EndpointInject(uri = "mock:result")
38 | private MockEndpoint mock;
39 |
40 | /**
41 | * Test the Camel-Kafka, set topic by headers and sync producer
42 | *
43 | * @throws Exception
44 | */
45 | @Test
46 | public void transferExchangeSpringSupportTest() throws Exception {
47 |
48 | final String PAYLOAD = "SpringBasicTransferExchangeTest";
49 |
50 | mock.reset(); // reset the mock
51 | template.sendBody("direct:basicTransferExchangeTest", PAYLOAD);
52 |
53 | //setup the actual expectation
54 | mock.expectedMessageCount(1);
55 | mock.expectedBodiesReceived(PAYLOAD);
56 |
57 | assertMockEndpointsSatisfied();
58 | mock.reset();
59 | }
60 |
61 |
62 | /**
63 | * load spring context
64 | *
65 | * @return
66 | */
67 | @Override
68 | protected ClassPathXmlApplicationContext createApplicationContext() {
69 |
70 | return new ClassPathXmlApplicationContext("META-INF/spring/basic-spring-tests.xml");
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/load/SimpleLoadTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package org.apache.camel.component.kafka.load;
19 |
20 | import org.apache.camel.Main;
21 | import org.apache.camel.builder.RouteBuilder;
22 | import org.apache.camel.component.dataset.DataSet;
23 | import org.apache.camel.component.dataset.SimpleDataSet;
24 | import org.apache.camel.component.kafka.KafkaConstants;
25 | import org.slf4j.Logger;
26 | import org.slf4j.LoggerFactory;
27 |
28 | import java.util.Arrays;
29 | import java.util.Random;
30 |
31 | /**
32 | * Simple test for approximate load measurements through the throughput logger
33 | * please note that in this test the whole exchange is send
34 | *
35 | * NOTE: need to review for correctness
36 | */
37 | public class SimpleLoadTest extends Main {
38 |
39 | static Logger LOGGER = LoggerFactory.getLogger(SimpleLoadTest.class);
40 |
41 | final long uid = new Random().nextLong();
42 |
43 | /**
44 | * Test the Camel-Kafka, set topic by headers and sync producer
45 | *
46 | * @throws Exception
47 | */
48 | public static void main(String[] args) throws Exception {
49 |
50 | SimpleLoadTest main = new SimpleLoadTest();
51 |
52 | main.enableHangupSupport();
53 | main.addRouteBuilder(createRouteBuilder());
54 | main.bind("sampleGenerator", createDataSet());
55 |
56 | main.run(args);
57 | }
58 |
59 | /**
60 | * Create route
61 | *
62 | * @return
63 | */
64 | static RouteBuilder createRouteBuilder() {
65 | return new RouteBuilder() {
66 | public void configure() {
67 |
68 | from("dataset://sampleGenerator?produceDelay=0")
69 | .setHeader(KafkaConstants.PARTITION_KEY.value,constant("LoadTest"))
70 | .to("kafka:fooiout?zkConnect=localhost:2181&partitionKey=1&metadataBrokerList=localhost:9092&transferExchange=true&groupId="+KafkaConstants.DEFAULT_GROUP.value);
71 |
72 | from("kafka:fooiout?zkConnect=localhost:2181&partitionKey=1&transferExchange=true&groupId=" + KafkaConstants.DEFAULT_GROUP.value)
73 | .to("log://demo?groupSize=1000&level=INFO");
74 | }
75 | };
76 | }
77 |
78 | /**
79 | * Sample data-set
80 | */
81 | static DataSet createDataSet() {
82 |
83 | final SimpleDataSet result = new SimpleDataSet();
84 | result.setSize(10000);
85 | result.setDefaultBody(createSamplePayload(2000000));
86 |
87 | return result;
88 | }
89 |
90 | /**
91 | * Utility method to create a string of specific size (aprox)
92 | *
93 | * @param size Number of characters
94 | * @return
95 | */
96 | private static String createSamplePayload(final int size) {
97 |
98 | char[] chars = new char[size];
99 | Arrays.fill(chars, 'A');
100 |
101 | return new String(chars);
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/test/java/org/apache/camel/component/kafka/partitioner/SimplePartitioner.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package org.apache.camel.component.kafka;
18 |
19 | import kafka.producer.Partitioner;
20 | import kafka.utils.VerifiableProperties;
21 |
22 | public class SimplePartitioner implements Partitioner {
23 |
24 | public SimplePartitioner(VerifiableProperties props) {
25 | }
26 |
27 | /**
28 | * Uses the key to calculate a partition bucket id for routing
29 | * the data to the appropriate broker partition
30 | *
31 | * @return an integer between 0 and numPartitions-1
32 | */
33 | @Override
34 | public int partition(Object key, int numPartitions) {
35 | return key.hashCode() % numPartitions;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/resources/META-INF/spring/basic-spring-tests.xml:
--------------------------------------------------------------------------------
1 |
2 |
18 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | ## ------------------------------------------------------------------------
2 | ## Licensed to the Apache Software Foundation (ASF) under one or more
3 | ## contributor license agreements. See the NOTICE file distributed with
4 | ## this work for additional information regarding copyright ownership.
5 | ## The ASF licenses this file to You under the Apache License, Version 2.0
6 | ## (the "License"); you may not use this file except in compliance with
7 | ## the License. You may obtain a copy of the License at
8 | ##
9 | ## http://www.apache.org/licenses/LICENSE-2.0
10 | ##
11 | ## Unless required by applicable law or agreed to in writing, software
12 | ## distributed under the License is distributed on an "AS IS" BASIS,
13 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | ## See the License for the specific language governing permissions and
15 | ## limitations under the License.
16 | ## ------------------------------------------------------------------------
17 |
18 | #
19 | # The logging properties used during tests..
20 | #
21 | log4j.rootLogger=INFO, out
22 |
23 | #log4j.logger.org.apache.camel=DEBUG
24 |
25 | # CONSOLE appender not used by default
26 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender
27 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
28 | log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
29 |
30 | # File appender
31 | log4j.appender.out=org.apache.log4j.FileAppender
32 | log4j.appender.out.layout=org.apache.log4j.PatternLayout
33 | log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
34 | log4j.appender.out.file=target/camel-kafka-test.log
35 | log4j.appender.out.append=true
36 |
--------------------------------------------------------------------------------