Skip to content

Commit 6bd0d6f

Browse files
committed
update docs
1 parent 72f1897 commit 6bd0d6f

File tree

1 file changed

+59
-22
lines changed

1 file changed

+59
-22
lines changed

docs/asciidoc/api-guide.adoc

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This section describes the reactive API for producing and consuming messages usi
77
There are three main classes in Reactive Commons:
88

99
. `HandlerRegistry` for listening to events and commands messages and for registering their respective handlers.
10-
. `DomainEventBus` for emiting events to an event bus
11-
. `DirectAsyncGateway` for emiting commands to an event bus
10+
. `DomainEventBus` for emiting events to an event bus
11+
. `DirectAsyncGateway` for emiting commands to an event bus
1212

1313
The project uses https://github.com/reactor/reactor-core[Reactor Core] to expose a https://github.com/reactive-streams/reactive-streams-jvm["Reactive Streams"] API.
1414

@@ -92,12 +92,12 @@ public class MainApplication {
9292
public static void main(String[] args) {
9393
SpringApplication.run(MainApplication.class, args);
9494
}
95-
95+
9696
@Bean
9797
public ManageTasksUseCase manageTasksUseCase(TaskToDoRepository tasks, DomainEventBus eventBus) {
9898
return new ManageTasksUseCase(tasks, eventBus);
99-
}
100-
99+
}
100+
101101
}
102102
--------
103103

@@ -160,9 +160,9 @@ public class AnyUseCase {
160160
}
161161
--------
162162

163-
The second parameter for sendCommand method is the name of the target component for that command, It's the name stablished in the properties file of Spring "application.properties" in the "spring.application.name" field.
163+
The second parameter for sendCommand method is the name of the target component for that command, It's the name stablished in the properties file of Spring "application.properties" in the "spring.application.name" field.
164164

165-
[NOTE]
165+
[NOTE]
166166
you don't need this parameter in the emit method of DomainEventBus class, because an event is a fact for cero or more subscribers.
167167

168168
==== DirectAsyncGateway - Request/Reply
@@ -194,15 +194,15 @@ Inbound messages are listened from an event bus using `HandlerRegistry` class. T
194194
@SpringBootApplication
195195
@EnableMessageListeners
196196
public class MainApplication {
197-
...
197+
...
198198
}
199199
--------
200200

201201
The HandlerRegistry implements builder patter, so each time you use some method, it will return a HanlderRegistry object. HanlderRegistry has the following methods:
202202

203-
* listenEvent: It lets listen for an event
204-
* serveQuery: It lets listen for a query
205-
* handleCommand: It lets listen for a command
203+
* listenEvent: It lets listen for an event
204+
* serveQuery: It lets listen for a query
205+
* handleCommand: It lets listen for a command
206206

207207
===== HandlerRegistry - listenEvent
208208

@@ -221,7 +221,7 @@ public interface EventHandler<T> extends GenericHandler<Void, DomainEvent<T>> {
221221
}
222222
--------
223223

224-
[NOTE]
224+
[NOTE]
225225
The return type of the EventHandler is Void
226226

227227
So, for example, if your application want react to user.registered event, you can define a handler for that event like this:
@@ -237,7 +237,7 @@ public class SomeConfigurationClass {
237237
@Bean
238238
public HandlerRegistry eventMessages() {
239239
return HandlerRegistry.register()
240-
.listenEvent("user.registered", event -> someBusinessDependency.functionReturningMonoVoid(event), UserRegistered.class)
240+
.listenEvent("user.registered", event -> someBusinessDependency.functionReturningMonoVoid(event), UserRegistered.class)
241241
}
242242
}
243243
--------
@@ -258,7 +258,7 @@ Where the CommandHandler interface signature is:
258258
public interface CommandHandler<T> extends GenericHandler<Void, Command<T>> {
259259
}
260260
--------
261-
[NOTE]
261+
[NOTE]
262262
The return type of the CommandHandler is Void
263263

264264
So, for example, if your application want react to user.register command, you can define a handler for that command like this:
@@ -268,17 +268,19 @@ So, for example, if your application want react to user.register command, you ca
268268
@Bean
269269
public HandlerRegistry commandMessages() {
270270
return HandlerRegistry.register()
271-
.handleCommand("user.register", cmd -> someBusinessDependency.handleCommand(cmd), UserRegister.class);
271+
.handleCommand("user.register", cmd -> someBusinessDependency.handleCommand(cmd), UserRegister.class);
272272
}
273273
--------
274274

275275
===== HandlerRegistry - serveQuery
276276

277-
serveQuery method lets you register a handler for a specific query. It has the next signature:
277+
serveQuery method lets you register a handler for a specific query. It has the next signatures:
278278

279279
[source,java]
280280
--------
281281
HandlerRegistry serveQuery(String resource, QueryHandler<T, R> handler, Class<R> queryClass)
282+
283+
HandlerRegistry serveQuery(String resource, QueryHandlerDelegate<Void, R> handler, Class<R> queryClass)
282284
--------
283285

284286
Where the QueryHandler interface signature is:
@@ -287,8 +289,22 @@ Where the QueryHandler interface signature is:
287289
--------
288290
public interface QueryHandler<T, C> extends GenericHandler<T, C> {
289291
}
292+
293+
public interface GenericHandler<T, M> {
294+
Mono<T> handle(M message);
295+
}
296+
--------
297+
298+
The QueryHandlerDelegate interface signature is:
299+
300+
[source,java]
301+
--------
302+
public interface QueryHandlerDelegate<T, M> {
303+
Mono<T> handle(From from, M message);
304+
}
290305
--------
291-
[NOTE]
306+
307+
[NOTE]
292308
The return type of the QueryHandler is a generic C
293309

294310
For example, if your application want react to user.information query, you can define a handler for that query like this:
@@ -298,9 +314,30 @@ For example, if your application want react to user.information query, you can d
298314
@Bean
299315
public HandlerRegistry queryMessages() {
300316
return HandlerRegistry.register()
301-
.serveQuery("user.information", query -> someBusinessDependency.findSomething(query), SomeQuery.class);
317+
.serveQuery("user.information", query -> someBusinessDependency.findSomething(query), SomeQuery.class);
318+
}
319+
--------
320+
321+
[NOTE]
322+
The return type of the QueryHandlerDelegate should be Void
323+
324+
This second interface allows the request / reply pattern not to have to be resolved in the same flow. For example,
325+
when the execution of a task is requested, and it can be processed by a group of workers, who leave the result in a database. There is another group of workers who are in charge of listening to the changes in the database and responding to that request. In this scenario, the process who receives the request is not the same process who responds to it. That is the usage scenario for QueryHandlerDelegate:
326+
327+
[source,java]
328+
--------
329+
@Bean
330+
public HandlerRegistry queryMessages() {
331+
return HandlerRegistry.register()
332+
.serveQuery("report.create", (from, query) -> someTask.startReportProcess(from, query), SomeQuery.class);
302333
}
303334
--------
335+
When the report creation task is completed, the process responsible of responding should call the reply method of DirectAsyncGateway passing the from object like this:
336+
337+
[source,java]
338+
--------
339+
gateway.reply(response, from)
340+
--------
304341

305342
TIP: Remember HandlerRegistry use builder pattern, So, you can build a chain of listener for each message:
306343

@@ -316,9 +353,9 @@ public class SomeConfigurationClass {
316353
public HandlerRegistry notificationEvents() {
317354
return HandlerRegistry.register()
318355
.listenNotificationEvent("some.event.name", event -> someBusinessDependency.someFunctionReturningMonoVoid(event), SomeClass.class)
319-
.listenEvent("some.event.name2", event -> someBusinessDependency.functionReturningMonoVoid(event), Some.class)
320-
.serveQuery("query.name", query -> someBusinessDependency.findSomething(query), SomeQuery.class)
321-
.handleCommand("command.name", cmd -> someBusinessDependency.handleCommand(cmd), CmdClass.class);
356+
.listenEvent("some.event.name2", event -> someBusinessDependency.functionReturningMonoVoid(event), Some.class)
357+
.serveQuery("query.name", query -> someBusinessDependency.findSomething(query), SomeQuery.class)
358+
.handleCommand("command.name", cmd -> someBusinessDependency.handleCommand(cmd), CmdClass.class);
322359
}
323360
}
324-
--------
361+
--------

0 commit comments

Comments
 (0)