Skip to content

Commit d77f14f

Browse files
committed
docs(handler): add support for raw command and event handling with RabbitMQ
1 parent 16964a0 commit d77f14f

File tree

4 files changed

+2447
-1535
lines changed

4 files changed

+2447
-1535
lines changed

docs/docs/reactive-commons/6-handling-domain-events.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ To effectively start listening events you should add the annotation `@EnableEven
2727
```java
2828
@EnableEventListeners
2929
public class EventsHandler {
30+
3031
public Mono<Void> handleEventA(DomainEvent<Object/*change for proper model*/> event) {
3132
System.out.println("event received: " + event.getName() + " ->" + event.getData());
3233
return Mono.empty();
3334
}
35+
3436
}
3537
```
3638

@@ -55,9 +57,51 @@ Then you should create the handler like:
5557
```java
5658
@EnableNotificationListener
5759
public class EventsHandler {
60+
5861
public Mono<Void> handleEventA(DomainEvent<Object/*change for proper model*/> event) {
5962
System.out.println("event received: " + event.getName() + " ->" + event.getData());
6063
return Mono.empty();
6164
}
65+
6266
}
63-
```
67+
```
68+
69+
### Listening Raw Events
70+
71+
If you need direct access to the raw message from RabbitMQ without domain model conversion, you can use `RawEventHandler`.
72+
This approach applies to both domain events and notification events. Raw event handlers process all incoming events for the specified event name,
73+
giving you access to the message body, headers, and other low-level properties directly.
74+
75+
#### Example for Raw Domain Events
76+
77+
```java
78+
@Configuration
79+
public class HandlerRegistryConfiguration {
80+
81+
@Bean
82+
public HandlerRegistry handlerRegistry(EventsHandler events) {
83+
return HandlerRegistry.register()
84+
.listenRawEvent("some.event.name", events::handleRawEventOrNotification)
85+
.listenNotificationRawEvent("some.notification.event", events::handleRawEventOrNotification);
86+
}
87+
}
88+
```
89+
90+
The handler implementation receives a `RawMessage` which can be cast to `RabbitMessage` to access the underlying message properties:
91+
92+
```java
93+
@EnableEventListeners
94+
@EnableNotificationListener
95+
public class EventsHandler {
96+
97+
public Mono<Void> handleRawEventOrNotification(RawMessage event) {
98+
RabbitMessage rawMessage = (RabbitMessage) event;
99+
System.out.println("RawEvent received: " + new String(rawMessage.getBody()));
100+
System.out.println("Content Type: " + rawMessage.getProperties().getContentType());
101+
System.out.println("Headers: " + rawMessage.getProperties().getHeaders());
102+
// Process the raw event or notification
103+
return Mono.empty();
104+
}
105+
106+
}
107+
```

docs/docs/reactive-commons/7-handling-commands.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar_position: 7
66

77
## HandlerRegistry configuration
88

9-
To listen a Command you should register it in the HandlerRegistry and make it available as a Bean
9+
To listen a Command you should register it in the HandlerRegistry and make it available as a Bean.
1010

1111
### Listening Commands
1212

@@ -27,15 +27,52 @@ To effectively start listening commands you should add the annotation `@EnableCo
2727
```java
2828
@EnableCommandListeners
2929
public class CommandsHandler {
30+
3031
public Mono<Void> handleCommandA(Command<Object/*change for proper model*/> command) {
3132
System.out.println("command received: " + command.getName() + " ->" + command.getData());
3233
return Mono.empty();
3334
}
35+
3436
}
3537
```
3638

3739
As the model of commands is direct, a consumer always can send commands to the service provider, by this reason you may receive commands that you don`t have configured.
3840

41+
### Listening Raw Commands
42+
43+
If you need direct access to the raw message from RabbitMQ without domain model conversion, you can use `RawCommandHandler`.
44+
Raw command handlers process all incoming commands without filtering by command name. This is useful when you need to handle
45+
the message body, headers, or other low-level properties directly.
46+
47+
```java
48+
@Configuration
49+
public class HandlerRegistryConfiguration {
50+
51+
@Bean
52+
public HandlerRegistry handlerRegistry(CommandsHandler commands) {
53+
return HandlerRegistry.register()
54+
.handleRawCommand(commands::handleRawCommandA);
55+
}
56+
}
57+
```
58+
59+
The handler implementation receives a `RawMessage` which can be cast to `RabbitMessage` to access the underlying message properties:
60+
61+
```java
62+
@EnableCommandListeners
63+
public class CommandsHandler {
64+
65+
public Mono<Void> handleRawCommandA(RawMessage command) {
66+
RabbitMessage rawMessage = (RabbitMessage) command;
67+
System.out.println("RawEvent received: " + new String(rawMessage.getBody()));
68+
System.out.println("Content Type: " + rawMessage.getProperties().getContentType());
69+
System.out.println("Headers: " + rawMessage.getProperties().getHeaders());
70+
// Process the raw message
71+
return Mono.empty();
72+
}
73+
}
74+
```
75+
3976
### Wildcards
4077

4178
You may need to handle variable command names that have the same structure, in that case you can specfy a pattern with '*' wildcard, for example:

0 commit comments

Comments
 (0)