/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.coheigea.camel.kafka; import java.util.Properties; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.ZkConnection; import org.apache.camel.spring.Main; import org.apache.curator.test.TestingServer; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerRecord; import kafka.admin.AdminUtils; import kafka.admin.RackAwareMode; import kafka.server.KafkaConfig; import kafka.server.KafkaServerStartable; import kafka.utils.ZKStringSerializer$; import kafka.utils.ZkUtils; public class KafkaTest extends org.junit.Assert { private static KafkaServerStartable kafkaServer; private static TestingServer zkServer; private static int port = 12345; @org.junit.BeforeClass public static void setup() throws Exception { zkServer = new TestingServer(); port = Integer.parseInt(System.getProperty("kafka.port")); Properties props = new Properties(); props.put("broker.id", 1); props.put("host.name", "localhost"); props.put("port", port); props.put("log.dir", "/tmp/kafka"); props.put("zookeeper.connect", zkServer.getConnectString()); props.put("replica.socket.timeout.ms", "1500"); props.put("controlled.shutdown.enable", Boolean.TRUE.toString()); KafkaConfig config = new KafkaConfig(props); kafkaServer = new KafkaServerStartable(config); kafkaServer.startup(); // Create a "test" topic ZkClient zkClient = new ZkClient(zkServer.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$); final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkServer.getConnectString()), false); AdminUtils.createTopic(zkUtils, "test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$); } @org.junit.AfterClass public static void cleanup() throws Exception { if (kafkaServer != null) { kafkaServer.shutdown(); } if (zkServer != null) { zkServer.stop(); } } @org.junit.Test public void testKafka() throws Exception { // Set up the Producer Properties producerProps = new Properties(); producerProps.put("bootstrap.servers", "localhost:" + port); producerProps.put("acks", "all"); producerProps.put("retries", 0); producerProps.put("batch.size", 16384); producerProps.put("linger.ms", 1); producerProps.put("buffer.memory", 33554432); producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(producerProps); // Send a message producer.send(new ProducerRecord<String, String>("test", "somekey", "somevalue")); producer.flush(); // Start up the Camel route Main main = new Main(); main.setApplicationContextUri("camel-kafka.xml"); main.start(); // Sleep to allow time for the demo to work Thread.sleep(60 * 1000); main.stop(); producer.close(); } }