2222import java .lang .Thread .UncaughtExceptionHandler ;
2323import java .util .Arrays ;
2424import java .util .Collections ;
25+ import java .util .HashMap ;
2526import java .util .Map ;
2627import java .util .Properties ;
2728import java .util .concurrent .ExecutionException ;
3738import org .apache .kafka .common .config .SaslConfigs ;
3839import org .apache .kafka .common .config .SslConfigs ;
3940import org .apache .kafka .common .errors .TopicExistsException ;
40- import org .apache .log4j .Level ;
41- import org .apache .log4j .Logger ;
41+ import org .apache .kafka .common .serialization .StringDeserializer ;
42+ import org .apache .kafka .common .serialization .StringSerializer ;
43+ import org .slf4j .Logger ;
44+ import org .slf4j .LoggerFactory ;
4245
4346import com .eventstreams .samples .env .Environment ;
4447import com .eventstreams .samples .env .EventStreamsCredentials ;
@@ -55,7 +58,7 @@ public class EventStreamsConsoleSample {
5558 private static final String ARG_CONSUMER = "-consumer" ;
5659 private static final String ARG_PRODUCER_ = "-producer" ;
5760 private static final String ARG_TOPIC = "-topic" ;
58- private static final Logger logger = Logger .getLogger (EventStreamsConsoleSample .class );
61+ private static final Logger logger = LoggerFactory .getLogger (EventStreamsConsoleSample .class );
5962
6063 private static Thread consumerThread = null ;
6164 private static ConsumerRunnable consumerRunnable = null ;
@@ -67,14 +70,14 @@ public class EventStreamsConsoleSample {
6770 Runtime .getRuntime ().addShutdownHook (new Thread () {
6871 @ Override
6972 public void run () {
70- logger .log ( Level . WARN , "Shutdown received." );
73+ logger .warn ( "Shutdown received." );
7174 shutdown ();
7275 }
7376 });
7477 Thread .setDefaultUncaughtExceptionHandler (new UncaughtExceptionHandler () {
7578 @ Override
7679 public void uncaughtException (Thread t , Throwable e ) {
77- logger .log ( Level . ERROR , "Uncaught Exception on " + t .getName () + " : " + e , e );
80+ logger .error ( "Uncaught Exception on {} : {}" , t .getName (), e , e );
7881 shutdown ();
7982 }
8083 });
@@ -117,24 +120,19 @@ public static void main(String args[]) {
117120 }
118121 // Check environment: VCAP_SERVICES vs command line arguments, to obtain configuration parameters
119122 if (args .length == 0 ) {
120-
121- logger .log (Level .INFO , "Using VCAP_SERVICES to find credentials." );
122-
123+ logger .info ("Using VCAP_SERVICES to find credentials." );
123124 EventStreamsCredentials credentials = Environment .getEventStreamsCredentials ();
124-
125125 bootstrapServers = stringArrayToCSV (credentials .getKafkaBrokersSasl ());
126126 apiKey = credentials .getApiKey ();
127-
128127 } else {
129128 // If running locally, parse the command line
130129 if (args .length < 2 ) {
131- logger .log ( Level . ERROR , "It appears the application is running without VCAP_SERVICES but the arguments are incorrect for local mode." );
130+ logger .error ( "It appears the application is running without VCAP_SERVICES but the arguments are incorrect for local mode." );
132131 printUsage ();
133132 System .exit (-1 );
134133 }
135134
136- logger .log (Level .INFO , "Using command line arguments to find credentials." );
137-
135+ logger .info ("Using command line arguments to find credentials." );
138136 bootstrapServers = args [0 ];
139137 apiKey = args [1 ];
140138 if (apiKey .contains (":" )) {
@@ -162,50 +160,50 @@ public static void main(String args[]) {
162160 topicName = parsedArgs .get (ARG_TOPIC );
163161 }
164162 } catch (IllegalArgumentException e ) {
165- logger .log ( Level . ERROR , e .getMessage ());
163+ logger .error ( e .getMessage ());
166164 System .exit (-1 );
167165 }
168166 }
169167 }
170168
171- logger .log ( Level . INFO , "Kafka Endpoints: " + bootstrapServers );
169+ logger .info ( "Kafka Endpoints: {}" , bootstrapServers );
172170
173171 //Using Kafka Admin API to create topic
174172 try (AdminClient admin = AdminClient .create (getAdminConfigs (bootstrapServers , apiKey ))) {
175- logger .log ( Level . INFO , "Creating the topic " + topicName );
173+ logger .info ( "Creating the topic {}" , topicName );
176174 NewTopic newTopic = new NewTopic (topicName , 1 , (short ) 3 );
177175 CreateTopicsResult ctr = admin .createTopics (Collections .singleton (newTopic ));
178176 ctr .all ().get (10 , TimeUnit .SECONDS );
179177 } catch (ExecutionException ee ) {
180178 if (ee .getCause () instanceof TopicExistsException ) {
181- logger .log ( Level . INFO , "Topic " + topicName + " already exists" );
179+ logger .info ( "Topic {} already exists" , topicName );
182180 } else {
183- logger .log ( Level . ERROR , "Error occurred creating the topic " + topicName , ee );
181+ logger .error ( "Error occurred creating the topic " + topicName , ee );
184182 System .exit (-1 );
185183 }
186184 } catch (Exception e ) {
187- logger .log ( Level . ERROR , "Error occurred creating the topic " + topicName , e );
185+ logger .error ( "Error occurred creating the topic {}" , topicName , e );
188186 System .exit (-1 );
189187 }
190188
191189 //create the Kafka clients
192190 if (runConsumer ) {
193- Properties consumerProperties = getConsumerConfigs (bootstrapServers , apiKey );
194- consumerRunnable = new ConsumerRunnable (consumerProperties , topicName );
191+ Map < String , Object > consumerConfigs = getConsumerConfigs (bootstrapServers , apiKey );
192+ consumerRunnable = new ConsumerRunnable (consumerConfigs , topicName );
195193 consumerThread = new Thread (consumerRunnable , "Consumer Thread" );
196194 consumerThread .start ();
197195 }
198196
199197 if (runProducer ) {
200- Properties producerProperties = getProducerConfigs (bootstrapServers , apiKey );
201- producerRunnable = new ProducerRunnable (producerProperties , topicName );
198+ Map < String , Object > producerConfigs = getProducerConfigs (bootstrapServers , apiKey );
199+ producerRunnable = new ProducerRunnable (producerConfigs , topicName );
202200 producerThread = new Thread (producerRunnable , "Producer Thread" );
203201 producerThread .start ();
204202 }
205203
206- logger .log ( Level . INFO , "EventStreamsConsoleSample will run until interrupted." );
204+ logger .info ( "EventStreamsConsoleSample will run until interrupted." );
207205 } catch (Exception e ) {
208- logger .log ( Level . ERROR , "Exception occurred, application will terminate" , e );
206+ logger .error ( "Exception occurred, application will terminate" , e );
209207 System .exit (-1 );
210208 }
211209 }
@@ -236,21 +234,21 @@ private static String stringArrayToCSV(String[] sArray) {
236234 return sb .toString ();
237235 }
238236
239- static final Properties getProducerConfigs (String bootstrapServers , String apikey ) {
240- Properties configs = new Properties ();
241- configs .put (ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
242- configs .put (ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringSerializer" );
237+ static final Map < String , Object > getProducerConfigs (String bootstrapServers , String apikey ) {
238+ Map < String , Object > configs = new HashMap <> ();
239+ configs .put (ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG , StringSerializer . class . getName () );
240+ configs .put (ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG , StringSerializer . class . getName () );
243241 configs .put (ProducerConfig .CLIENT_ID_CONFIG , "kafka-java-console-sample-producer" );
244- configs .put (ProducerConfig .ACKS_CONFIG , "-1 " );
242+ configs .put (ProducerConfig .ACKS_CONFIG , "all " );
245243 configs .put (ProducerConfig .CLIENT_DNS_LOOKUP_CONFIG ,"use_all_dns_ips" );
246244 configs .putAll (getCommonConfigs (bootstrapServers , apikey ));
247245 return configs ;
248246 }
249247
250- static final Properties getConsumerConfigs (String bootstrapServers , String apikey ) {
251- Properties configs = new Properties ();
252- configs .put (ConsumerConfig .KEY_DESERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringDeserializer" );
253- configs .put (ConsumerConfig .VALUE_DESERIALIZER_CLASS_CONFIG , "org.apache.kafka.common.serialization.StringDeserializer" );
248+ static final Map < String , Object > getConsumerConfigs (String bootstrapServers , String apikey ) {
249+ Map < String , Object > configs = new HashMap <> ();
250+ configs .put (ConsumerConfig .KEY_DESERIALIZER_CLASS_CONFIG , StringDeserializer . class . getName () );
251+ configs .put (ConsumerConfig .VALUE_DESERIALIZER_CLASS_CONFIG , StringDeserializer . class . getName () );
254252 configs .put (ConsumerConfig .CLIENT_ID_CONFIG , "kafka-java-console-sample-consumer" );
255253 configs .put (ConsumerConfig .GROUP_ID_CONFIG , "kafka-java-console-sample-group" );
256254 configs .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "latest" );
@@ -259,8 +257,8 @@ static final Properties getConsumerConfigs(String bootstrapServers, String apike
259257 return configs ;
260258 }
261259
262- static final Properties getCommonConfigs (String boostrapServers , String apikey ) {
263- Properties configs = new Properties ();
260+ static final Map < String , Object > getCommonConfigs (String boostrapServers , String apikey ) {
261+ Map < String , Object > configs = new HashMap <> ();
264262 configs .put (CommonClientConfigs .BOOTSTRAP_SERVERS_CONFIG , boostrapServers );
265263 configs .put (CommonClientConfigs .SECURITY_PROTOCOL_CONFIG , "SASL_SSL" );
266264 configs .put (SaslConfigs .SASL_MECHANISM , "PLAIN" );
0 commit comments