How to Receive a Notification from the IBM Message Hub with Java

MessageHubSample.java

import static java.lang.System.out;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public final class MessageHubSample {

  public static void main(final String[] args) {
    final Properties props = new Properties();

    props.put("bootstrap.servers", "host:9093");

    props.put("enable.auto.commit", "true");

    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    props.put("security.protocol", "SASL_SSL");

    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

    consumer.subscribe(Arrays.asList("SampleTopic"));

    while (true) {
      final ConsumerRecords<String, String> records = consumer.poll(1000);

      for (final ConsumerRecord<String, String> record : records)
        out.println(record.value());

    }

  }

}

jaas.conf

KafkaClient {
  com.ibm.messagehub.login.MessageHubLoginModule required
  serviceName="kafka"
  username="username"
  password="password";
};

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s