Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix - NullPointerException in Kafka Connect Pipeline when JMS Property Key is Empty #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public ConnectHeaders convertJmsPropertiesToKafkaHeaders(final Message message)

jmsPropertyKeys.forEach(key -> {
try {
connectHeaders.addString(key.toString(), message.getObjectProperty(key.toString()).toString());
String value = "";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add empty value or null ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be to use null, please - as otherwise on the Kafka side you wouldn't be able to differentiate between a property that was explicitly set to an empty string or a property that was actually null

if (message.getObjectProperty(key.toString()) != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit comment: you can extract message.getObjectProperty(key.toString()) to a value and refer to both the places.

{
value = message.getObjectProperty(key.toString()).toString();
}
connectHeaders.addString(key.toString(), value);
//connectHeaders.addString(key.toString(), message.getObjectProperty(key.toString()).toString());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit comment: Remove the commented line. It is not needed.

} catch (final JMSException e) {
// Not failing the message processing if JMS properties cannot be read for some
// reason.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ public class JmsToKafkaHeaderConverterTest {
@Test
public void convertJmsPropertiesToKafkaHeaders() throws JMSException {

final List<String> keys = Arrays.asList("facilityCountryCode", "facilityNum");
final List<String> keys = Arrays.asList("facilityCountryCode", "facilityNum","nullProperty");

final Enumeration<String> keyEnumeration = Collections.enumeration(keys);

// Arrange
when(message.getPropertyNames()).thenReturn(keyEnumeration);
when(message.getObjectProperty("facilityCountryCode")).thenReturn("US");
when(message.getObjectProperty("facilityNum")).thenReturn("12345");
when(message.getObjectProperty("nullProperty")).thenReturn(null);

// Act
final ConnectHeaders actualConnectHeaders = jmsToKafkaHeaderConverter
.convertJmsPropertiesToKafkaHeaders(message);


//Verify
assertEquals("Both custom JMS properties were copied to kafka successfully.", 2, actualConnectHeaders.size());
assertEquals("Both custom JMS properties were copied to kafka successfully.", 3, actualConnectHeaders.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit comment: Both -> All three



}
Expand Down