Skip to content

Commit 84b81e3

Browse files
Nyholmweaverryan
authored andcommitted
Added fixes
1 parent ca22c3c commit 84b81e3

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

messenger/message-recorder.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
.. index::
22
single: Messenger; Record messages
33

4-
Record Events Produced by a Handler
5-
===================================
4+
Events Recorder: Handle Events After CommandHandler Is Done
5+
===========================================================
66

7-
In an example application there is a command (a CQRS message) named ``CreateUser``.
8-
That command is handled by the ``CreateUserHandler`` which creates
9-
a ``User`` object, stores that object to a database and dispatches a ``UserCreatedEvent``.
7+
Let's take the example of an application that has a command (a CQRS message) named
8+
``CreateUser``. That command is handled by the ``CreateUserHandler`` which creates
9+
a ``User`` object, stores that object to a database and dispatches a ``UserCreated`` event.
1010
That event is also a normal message but is handled by an *event* bus.
1111

12-
There are many subscribers to the ``UserCreatedEvent``, one subscriber may send
13-
a welcome email to the new user. Since we are using the ``DoctrineTransactionMiddleware``
14-
we wrap all database queries in one database transaction and rollback that transaction
15-
if an exception is thrown. That means that if an exception is thrown when sending
16-
the welcome email, then the user will not be created.
12+
There are many subscribers to the ``UserCreated`` event, one subscriber may send
13+
a welcome email to the new user. We are using the ``DoctrineTransactionMiddleware``
14+
to wrap all database queries in one database transaction.
1715

18-
The solution to this issue is to not dispatch the ``UserCreatedEvent`` in the
16+
**Problem:** If an exception is thrown when sending the welcome email, then the user
17+
will not be created because the ``DoctrineTransactionMiddleware`` will rollback the
18+
Doctrine transaction, in which the user has been created.
19+
20+
**Solution:** The solution is to not dispatch the ``UserCreated`` event in the
1921
``CreateUserHandler`` but to just "record" the events. The recorded events will
2022
be dispatched after ``DoctrineTransactionMiddleware`` has committed the transaction.
2123

@@ -69,6 +71,7 @@ in the middleware chain.
6971
$user = new User($command->getUuid(), $command->getName(), $command->getEmail());
7072
$this->em->persist($user);
7173
74+
// "Record" this event to be processed later by "handles_recorded_messages".
7275
$this->eventRecorder->record(new UserCreatedEvent($command->getUuid());
7376
}
7477
}

0 commit comments

Comments
 (0)