-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include reactive reply router as utils class, remove issue (#9)
* include reactive reply router as utils class, remove issue * add parametrized class
- Loading branch information
1 parent
eed6f54
commit a42ae27
Showing
9 changed files
with
103 additions
and
44 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
.../main/java/co/com/bancolombia/commons/jms/exceptions/RelatedMessageNotFoundException.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,9 @@ | ||
package co.com.bancolombia.commons.jms.exceptions; | ||
|
||
import javax.jms.JMSRuntimeException; | ||
|
||
public class RelatedMessageNotFoundException extends JMSRuntimeException { | ||
public RelatedMessageNotFoundException(String correlationId) { | ||
super("Processor not found for correlationId: " + correlationId); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...ons-jms-utils/src/main/java/co/com/bancolombia/commons/jms/utils/ReactiveReplyRouter.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,42 @@ | ||
package co.com.bancolombia.commons.jms.utils; | ||
|
||
import co.com.bancolombia.commons.jms.exceptions.RelatedMessageNotFoundException; | ||
import lombok.extern.log4j.Log4j2; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.publisher.Sinks; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
@Log4j2 | ||
public class ReactiveReplyRouter<T> { | ||
private final ConcurrentHashMap<String, Sinks.One<T>> processors = new ConcurrentHashMap<>(); | ||
|
||
public Mono<T> wait(String messageId) { | ||
final Sinks.One<T> processor = Sinks.one(); | ||
processors.put(messageId, processor); | ||
log.info("Waiting for: {}", messageId); | ||
return processor.asMono(); | ||
} | ||
|
||
public Mono<T> wait(String messageId, Duration timeout) { | ||
return this.wait(messageId).timeout(timeout).doOnError(TimeoutException.class, e -> clean(messageId)); | ||
} | ||
|
||
public void reply(String correlationID, T response) { | ||
if (correlationID != null) { | ||
log.info("Replying with id: {}", correlationID); | ||
final Sinks.One<T> processor = processors.remove(correlationID); | ||
if (processor == null) { | ||
throw new RelatedMessageNotFoundException(correlationID); | ||
} else { | ||
processor.tryEmitValue(response); | ||
} | ||
} | ||
} | ||
|
||
public void clean(String correlationId) { | ||
processors.remove(correlationId); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...jms-utils/src/test/java/co/com/bancolombia/commons/jms/utils/ReactiveReplyRouterTest.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,34 @@ | ||
package co.com.bancolombia.commons.jms.utils; | ||
|
||
import co.com.bancolombia.commons.jms.exceptions.RelatedMessageNotFoundException; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class ReactiveReplyRouterTest { | ||
private final ReactiveReplyRouter<String> router = new ReactiveReplyRouter<>(); | ||
|
||
@Test | ||
void shouldReply() { | ||
Mono<String> flow = router.wait("123"); | ||
router.reply("123", "result"); | ||
StepVerifier.create(flow).expectNext("result").verifyComplete(); | ||
} | ||
|
||
@Test | ||
void shouldFailNotFound() { | ||
assertThrows(RelatedMessageNotFoundException.class, () -> router.reply("1234", "result")); | ||
} | ||
|
||
@Test | ||
void shouldHandleTimeout() { | ||
Mono<String> flow = router.wait("1234", Duration.ZERO); | ||
router.reply(null, null); | ||
StepVerifier.create(flow).expectError(TimeoutException.class).verify(); | ||
} | ||
} |
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
37 changes: 0 additions & 37 deletions
37
...rc/main/java/co/com/bancolombia/jms/sample/domain/usecase/ReactiveReplyRouterUseCase.java
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version=0.1.0 | ||
version=0.1.1 | ||
springBootVersion=2.5.8 | ||
gradleVersionsVersion=0.28.0 | ||
mqJMSVersion=2.5.0 | ||
|