Symfony 4 test with Doctrine subscribers/listeners - doctrine

I want to functional test my Symfony 4 app on a specific part using Doctrine subscribers. So i mock up my service as i mentionned it in this post : How to mock service with symfony 4 in functional tests?
But then i encoutered this problem : Doctrine subscribers (or listeners) services are not the same if fetched from container or fetched by doctrine events.
If i dump the spl_object_id of a doctrine subscriber fetched from the container and the spl_object_id of the same doctrine subscriber but fired by a doctrine event (like onFlush), this is not the same id.
That implies that in my tests i cannot update my mock with some asserts, because the doctrine subscriber that i fetched is not the one used by doctrine events...
Maybe this is a specific and normal behavior of Doctrine subscribers, but if someone can explains it and suggest how to retrieve the right doctrine service instance to update it (the mock in my case)
EDIT
This is also the case for Symfony messenger transport. Transport is duplicated in doctrine subscribers
Symfony issue : https://github.com/symfony/symfony/issues/36783

Related

Axon Event Handler not executing on a different microservice

I'm using CQRS with axon Framework in a project,and I'm using Kafka like evnt bus and MongoDB like event store
I have two Microservices, One for the Command Side and the Other for Query Side.
In the Query side I'm try using MySQL database for storing aggregates events, but the eventHandler does not work and I don Know why.
Command Microservice
Query Microservice
Could you share the configuration of the event handler? Or maybe some kind of error you are getting? Could be a lot of things. Might be because you use MySQL and Spring Boot, it actually trying to use MySQL as event store, and not MongoDB.

Axon Event Handler not working on a different microservice

I was implementing event-driven CQRS concept using Axon framework. I created two separate microservices and their own two write and read databases.
I am publishing an event in ms-1 and now I want to handle that event in ms-2, so that I can persist it in my read DB. But the #EventHandler is not working in my ms-2. The events are published on the AxonServer, I can see the updates there.
And yes, I do have #Component on the EventHandler class in ms-2. Also, I am running AxonServer on docker.
The #EventHandler works fine when it is in the same microservice.
My axon-framework dependency is 4.5.8 and google guava's is 30.1.1-jre

AxonIQ AxonFramework MongoEventStorageEngine framework table creation on business DB

I am using AxonIQ AxonFramework version 4.5.3 with Spring Boot and custom event store.
I'm using MongoEventStorageEngine and configured a separate MongoDB database for the EventStorage.
I am doing some business logic with my business database through a microservice. In the same microservice, I've configured the custom EventStorage.
But a few tables (viz. association_value_entry, saga_entry, token_entry) are getting created on my business database which is a PostgresDB.
Why is AxonFramework creating new tables in my business database as I have already configured a separate MongoDB database for EventStorage. All the related database objects for Axon to work should be ideally created in the EventStorage database rather than in my business database.
The tables you are mentioned should be part of your 'read' model (I believe that is what you called business database).
They are not used for Event Storage or Event Sourcing but rather to specific things that are controlled on client side. For example, token_entry, among other things, is the table where your app keep track of the tokens and events it already consumed - you can read more about it here. Similar to the saga tables, where Sagas are stored on the client side having nothing to do with the Event Store - you can read more about it here.

Should microservices connected with axon share the axon framework related tables?

I am starting a project where I want to have multiple services that communicate with each other using the axon server.
I have more than one service with the following stack:
Spring Boot 2.3.0.RELEASE (with starters: Data, JPA, web, mysql)
Axon
Spring Boot Starter - 4.2.1
Each one of the services uses different schemas in the mysql server.
When I start the spring boot service with the axon framework activated, some tables for tokens, sagas, etc are created in the database schema of each application.
I have two questions
In the architecture that I am trying to build, should I have only
one database for all the ‘axon enabled’ services, so the sagas,
tokens, events, etc are only in one place?
If so, can anyone
provide an example of how to configure a custom
EntityManagerProvider to have the database of the service separated
from the database of Axon?
I assume each of your microservices models a sub-domain. Since the events do model a (sub)domain, along with aggregates, entities and value objects, I very much favor keeping the Axon-related schemas separated, most likely along with the databases/schemas corresponding to each service. I would, thus, prefer a modeling-first approach when considering such technical options.
It is what we're currently doing in our microservices ecosystem.
There is at least one more technical reason to go with the same schema (one per sub-domain, that is), both for Axon assets and application-specific assets. It was pointed out to me by my colleague Marian. If you (will) use Event Sourcing (thus reconstructing the state of an aggregate by fetching and applying all past events resulted after handling the commands) then you will, most likely, need transactions which encompass this fetching as well as the command handling code which might, in turn, trigger (through events) writes to your microservice-specific database.
Axon can require five tables, depending on your usages of Axon of course.
These are:
The Event table.
The Snapshot Event table.
The Token table.
The Saga table.
The Association Value Entry table.
When using Axon Server, tables 1 and 2 will not be created since Axon Server is the storage solution for events and snapshots.
When not using Axon Server, I would indeed suggest to have a dedicated datasource for these.
Table 3 which services the TokenStore, should be as close as possible to your Query Models. The tokens portray how far a given EventProcessor is with handling events. As these EventProcessors typically service projectors which create your query models, keeping them together is sensible from a transactional perspective.
Table 4 and 5 are both required for Sagas. The "Saga table" stores the serialized sagas, whereas the "Association Value Entry table" carries the associations values between events and sagas so that the framework can load the right sagas. I'd store these either in a dedicated database or along with the other tables of the given (micro)service.

How to Restart Spring boot application (or just a particular instance of a class) when flyway migrate is ran or a new schema version is added

I have a particular instance of a class that loads some data from the database, so every time the database is updated the system should recreate the instance of that class to get the updated data
There are trivial several solutions:
(1) Use scheduling to load the latest data from DB periodically.
(2) Provide a web service such as RESTful API to load the latest data from DB.
(3) If your DB supports event-driven listeners, you can trigger your application to achieve this either by invoking a service described in (2) or send a message to queue and handle it by consumer.

Resources