Implementations

When it comes to dispatching queries, as explained in the Dispatching Queries section, there are a couple of implementations when it comes to actually sending the query message in question. The next sections provide an overview of the possible implementations, as well as pointing out how to set up query dispatching infrastructure with Sophon.

Query gateway

The query gateway is a convenient interface towards the query dispatching mechanism. While you are not required to use a gateway to dispatch queries, it is generally the easiest option to do so.

Sophon provides a QueryGateway interface and the DefaultQueryGateway implementation. The query gateway provides a number of methods that allow you to send a query and wait for a single or multiple results either synchronously, with a timeout or asynchronously. The query gateway needs to be configured with access to the query bus and a (possibly empty) list of `QueryDispatchInterceptor`s.

Query bus

The query bus is the mechanism that dispatches queries to query handlers. Queries are registered using the combination of the query request name and query response type. It is possible to register multiple handlers for the same request-response combination, which can be used to implement the scatter-gather pattern. When dispatching queries, the client must indicate whether it wants a response from a single handler or from all handlers.

SimpleQueryBus

The SimpleQueryBus does straightforward processing of queries in the thread that dispatches them. To configure a SimpleQueryBus:

  • Configuration API

  • Spring Boot

public class SophonConfig {
    // omitting other configuration methods...
    public void configureQueryBus(Configurer configurer) {
        configurer.configureQueryBus(
                config -> SimpleQueryBus.builder()
                                        .transactionManager(config.getComponent(TransactionManager.class))
                                        .messageMonitor(config.messageMonitor(SimpleQueryBus.class, "queryBus"))
                                        .build()
        );
    }
}
@Configuration
public class SophonConfig {
    // omitting other configuration methods...
    @Bean
    public QueryBus queryBus(GlobalMetricRegistry metricRegistry,
                             SpanFactory spanFactory,
                             TransactionManager transactionManager,
                             QueryUpdateEmitter updateEmitter) {
        return SimpleQueryBus.builder()
                             .messageMonitor(metricRegistry.registerQueryBus("queryBus"))
                             .transactionManager(transactionManager)
                             .spanFactory(spanFactory)
                             .queryUpdateEmitter(updateEmitter)
                             // ..
                             .build();
    }
}