-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made consumer restart on error programmatically to replay unacknowled…
…ged messages for robust delivery
- Loading branch information
Showing
7 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...am-consumer/src/main/java/me/vsadokhin/iot/stream/consumer/KafkaConsumerErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.vsadokhin.iot.stream.consumer; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.kafka.listener.KafkaListenerErrorHandler; | ||
import org.springframework.kafka.listener.ListenerExecutionFailedException; | ||
import org.springframework.messaging.Message; | ||
|
||
public class KafkaConsumerErrorHandler implements KafkaListenerErrorHandler { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConsumerErrorHandler.class); | ||
|
||
@Override | ||
public Object handleError(Message<?> message, ListenerExecutionFailedException exception) { | ||
LOGGER.error("Exception occurred on metric receiving: " + message, exception); | ||
StreamConsumerApplication.restart(); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...onsumer/src/test/java/me/vsadokhin/iot/stream/consumer/KafkaConsumerErrorHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package me.vsadokhin.iot.stream.consumer; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.core.IsNull.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.verify; | ||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.kafka.listener.ListenerExecutionFailedException; | ||
import org.springframework.messaging.Message; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({StreamConsumerApplication.class, LoggerFactory.class }) | ||
public class KafkaConsumerErrorHandlerTest { | ||
|
||
private KafkaConsumerErrorHandler handler; | ||
private static Logger MOCK_LOGGER= mock(Logger.class); | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
mockStatic(LoggerFactory.class); | ||
when(LoggerFactory.getLogger(KafkaConsumerErrorHandler.class)).thenReturn(MOCK_LOGGER); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
reset(MOCK_LOGGER); | ||
mockStatic(StreamConsumerApplication.class); | ||
handler = new KafkaConsumerErrorHandler(); | ||
} | ||
|
||
@Test | ||
public void handleError_callLoggerError() { | ||
// setup | ||
Message mockMessage = mock(Message.class); | ||
ListenerExecutionFailedException mockListenerExecutionFailedException = mock(ListenerExecutionFailedException.class); | ||
|
||
// act | ||
handler.handleError(mockMessage, mockListenerExecutionFailedException); | ||
|
||
// verify | ||
verify(MOCK_LOGGER).error("Exception occurred on metric receiving: " + mockMessage, mockListenerExecutionFailedException); | ||
} | ||
|
||
@Test | ||
public void handleError_callStreamConsumerApplicationError() { | ||
// act | ||
handler.handleError(mock(Message.class), new ListenerExecutionFailedException(null)); | ||
|
||
// verify | ||
verifyStatic(StreamConsumerApplication.class); | ||
StreamConsumerApplication.restart(); | ||
} | ||
|
||
@Test | ||
public void handleError_checkResultIsNull() { | ||
// act | ||
Object result = handler.handleError(mock(Message.class), new ListenerExecutionFailedException(null)); | ||
|
||
// verify | ||
assertThat(result, is(nullValue())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters