|
| 1 | +--- |
| 2 | +sidebar_position: 10 |
| 3 | +--- |
| 4 | + |
| 5 | +# Wildcards |
| 6 | + |
| 7 | +You may need to listen variable event names that have the same structure, in that case you have the method `handleDynamicEvents` in the `HandlerRegistry`, so you can specify a pattern with '*' wildcard, it does not creates a binding in the broker, but allows that you do it dynamically through a `DynamicRegistry` class. |
| 8 | + |
| 9 | +You can also create binding with '#' wildcard, it is used to listen multiple words, for example `animals.#` will listen to `animals.dog`, `animals.dog.bark`, `animals.cat`, `animals.cat.meow`, etc. |
| 10 | + |
| 11 | +## DynamicRegistry API |
| 12 | + |
| 13 | +```java |
| 14 | +public interface DynamicRegistry { |
| 15 | + |
| 16 | + Mono<Void> startListeningEvent(String eventName); |
| 17 | + |
| 18 | + Mono<Void> stopListeningEvent(String eventName); |
| 19 | + |
| 20 | + //... other definitions for queries |
| 21 | + |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +To start listening a new event dynamically at runtime, you should inject and call a method of the DynamicReggistry, for example: |
| 26 | + |
| 27 | +```java |
| 28 | +@Component |
| 29 | +@AllArgsConstructor |
| 30 | +public class DynamicSubscriber { |
| 31 | + private final DynamicRegistry registry; |
| 32 | + |
| 33 | + public Mono<Void> listenNewEvent(String eventName) { |
| 34 | + return registry.startListeningEvent(eventName); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +The conditions for a success dynamic registry functionality are: |
| 40 | + |
| 41 | +- You should handle dynamic events with specific wildcard |
| 42 | + |
| 43 | +```java |
| 44 | +@Configuration |
| 45 | +public class HandlerRegistryConfiguration { |
| 46 | + |
| 47 | + @Bean |
| 48 | + public HandlerRegistry handlerRegistry(EventsHandler events) { |
| 49 | + return HandlerRegistry.register() |
| 50 | + .handleDynamicEvents("purchase.*", events::handleEventA, Object.class/*change for proper model*/); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- Start a listener dynamically through |
| 56 | + |
| 57 | +```java |
| 58 | +registry.startListeningEvent("purchase.cancelled"); |
| 59 | +``` |
| 60 | + |
| 61 | +You also can listen with * wildcard or # wildcard, the * wildcard is for a single word and # wildcard is for multiple words, for example: |
| 62 | + |
| 63 | +`animals.*` will listen to `animals.dog`, `animals.cat`, `animals.bird`, etc. |
| 64 | +`animals.#` will listen to `animals.dog`, `animals.dog.bark`, `animals.cat`, `animals.cat.meow`, etc. |
| 65 | + |
| 66 | +```java |
| 67 | +@Configuration |
| 68 | +public class HandlerRegistryConfiguration { |
| 69 | + |
| 70 | + @Bean |
| 71 | + public HandlerRegistry handlerRegistry(EventsHandler events) { |
| 72 | + return HandlerRegistry.register() |
| 73 | + .listenEvent("animals.*", events::handleEventA, Object.class/*change for proper model*/) |
| 74 | + .listenEvent("pets.#.any", events::handleEventA, Object.class/*change for proper model*/); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +This last approach is useful when you have a dynamic event name, for example, you can have a `purchase.cancelled` event, but you can also have a `purchase.cancelled.2021` event, so you can listen to all of them with `purchase.*` or `purchase.#` respectively. |
| 80 | + |
| 81 | +## Priorities |
| 82 | + |
| 83 | +The handlers with wildcards have the lowest priority, so if you have a specific handler for an event name, it will be called before the wildcard handler. |
| 84 | + |
| 85 | +The wildcard handler will be called if there is no specific handler for the event name. And the wildcard matches the pattern. |
| 86 | + |
| 87 | +General conditions for handler priority are: |
| 88 | +- fixed words has priority over wildcard |
| 89 | +- wildcard with * has priority over wildcard with # |
| 90 | +- wildcard with # has the lowest priority |
| 91 | + |
| 92 | +The next code will help you to avoid unexpected behaviors, which indicates you the handler that will be called. |
| 93 | +```java |
| 94 | + public static void main(String[] args) { |
| 95 | + Set<String> names = Set.of("prefix.*.*", "prefix.*.#"); |
| 96 | + String target = "prefix.middle.suffix"; |
| 97 | + String handler = new KeyMatcher().match(names, target); |
| 98 | + System.out.println(handler); |
| 99 | + } |
| 100 | +``` |
| 101 | + |
| 102 | +## Example |
| 103 | + |
| 104 | +You can see a real example at [samples/async/async-receiver-responder](https://github.com/reactive-commons/reactive-commons-java/tree/master/samples/async/async-receiver-responder) |
0 commit comments