EventServiceKafkaBuilder property parser

I am unable to pass a <String, Integer> property to EventServiceKafkaBuilder with withConsumerProperties.

Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
at com.leidos.leaf.event.service.kafka.EventServiceKafka.lambda$getConsumerProperties$12(EventServiceKafka.java:191)
at java.base/java.util.concurrent.ConcurrentHashMap.forEach(Unknown Source)
at java.base/java.util.Properties.forEach(Unknown Source)
at com.leidos.leaf.event.service.kafka.EventServiceKafka.getConsumerProperties(EventServiceKafka.java:191)
at com.leidos.leaf.event.service.kafka.EventServiceKafka.lambda$createEventReaders$11(EventServiceKafka.java:155)
at com.leidos.leaf.beans.service.builder.BaseServiceBuilder.getDependency(BaseServiceBuilder.java:68)
at com.leidos.leaf.event.service.kafka.EventServiceKafka.createEventReaders(EventServiceKafka.java:153)

A Java Properties object is a mapping of String key to String value; it does not support Integer values.

https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Because Properties inherits from Hashtable , the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings . The setProperty method should be used instead.

It is an unfortunate historical artifact that Properties implements Map<Object,Object>, as this class predates generics in Java. For type safety, you should never use the Properties put() method, but should instead use setProperty() as described in the Javadoc above.

1 Like
Properties consumerProperties = new Properties();
consumerProperties.setProperty("some.integer.prop", "42");

var eventService = EventServiceKafka.newBuilder()
    .withConsumerProperties(consumerProperties)
    .build();
1 Like

Cool, thanks.