Github user neykov commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/742#discussion_r34775865
  
    --- Diff: 
software/messaging/src/test/java/brooklyn/entity/messaging/kafka/KafkaSupport.java
 ---
    @@ -54,16 +52,33 @@ public KafkaSupport(KafkaCluster cluster) {
          * Send a message to the {@link KafkaCluster} on the given topic.
          */
         public void sendMessage(String topic, String message) {
    -        ZooKeeperNode zookeeper = cluster.getZooKeeper();
    -        Properties props = new Properties();
    -        props.put("zk.connect", String.format("%s:%d", 
zookeeper.getAttribute(Attributes.HOSTNAME), zookeeper.getZookeeperPort()));
    -        props.put("serializer.class", "kafka.serializer.StringEncoder");
    -        ProducerConfig config = new ProducerConfig(props);
    -
    -        Producer<String, String> producer = new Producer<String, 
String>(config);
    -        ProducerData<String, String> data = new ProducerData<String, 
String>(topic, message);
    -        producer.send(data);
    -        producer.close();
    +        Optional<Entity> anyBrokerNodeInCluster = 
Iterables.tryFind(cluster.getCluster().getChildren(), Predicates.and(
    +                Predicates.instanceOf(KafkaBroker.class),
    +                EntityPredicates.attributeEqualTo(KafkaBroker.SERVICE_UP, 
true)));
    +        if (anyBrokerNodeInCluster.isPresent()) {
    +            KafkaBroker broker = (KafkaBroker)anyBrokerNodeInCluster.get();
    +
    +            Properties props = new Properties();
    +
    +            props.put("metadata.broker.list", format("%s:%d", 
broker.getAttribute(KafkaBroker.HOSTNAME), broker.getKafkaPort()));
    +            props.put("bootstrap.servers", format("%s:%d", 
broker.getAttribute(KafkaBroker.HOSTNAME), broker.getKafkaPort()));
    +            props.put("key.serializer", 
"org.apache.kafka.common.serialization.StringSerializer");
    +            props.put("value.serializer", 
"org.apache.kafka.common.serialization.StringSerializer");
    +
    +            Producer<String, String> producer = new KafkaProducer<>(props);
    +            try {
    +                
((KafkaZooKeeper)cluster.getZooKeeper()).createTopic(topic);
    +                Thread.sleep(Duration.seconds(1).toMilliseconds());
    +
    +                ProducerRecord<String, String> data = new 
ProducerRecord<>(topic, message);
    +                producer.send(data);
    +                producer.close();
    +            } catch (InterruptedException e) {
    +                e.printStackTrace();
    --- End diff --
    
    If the sleep really needs to stay can use `Time.sleep(Duration.ONE_SECOND)` 
which doesn't declare `InterruptedException` so you can remove the try-catch. 
At the very least replace the `printStackTrace` with a call to a logger or 
`Excpetions.propagate`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to