aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/RoutingPolicy.java
blob: c9e5cc794f41c44baa4a1f14c03b34e9accfaf6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;

/**
 * Decides how to choose between candidate recipients of a hop template point.
 * <p>
 * The routing policy will be given an address and a set of recipients matching this address. It is responsible for
 * choosing one of the given recipients each time {@link #select} is called. The routing policy chooses which recipient
 * to return each time at its sole discretion, using information in the RoutingContext argument to choose or not as required
 * to implement the policy.
 * <p>
 * Example:
 * <ul>
 *     <li>The given <i>address</i> is <code>a/b/?</code>
 *     <li>The given <i>recipients</i> are <code>a/b/c, a/b/d and a/b/e</code> - one of these three must be returned on every call to <code>choose</code>
 * </ul>
 * <p>
 * This class is pluggable per template point in the address of a hop.
 *
 * @author bratseth
 * @author Simon Thoresen Hult
 */
public interface RoutingPolicy {

    /**
     * This function must choose a set of services that is to receive the given message from a list of possible
     * recipients. This is done by adding child routing contexts to the argument object. These children can then be
     * iterated and manipulated even before selection pass is concluded.
     *
     * @param context the complete context for the invocation of this policy. Contains all available data.
     */
    void select(RoutingContext context);

    /**
     * This function is called when all replies have arrived for some message. The implementation is responsible for
     * merging multiple replies into a single sensible reply. The replies is contained in the child context objects of
     * the argument context, and then response must be set in that context.
     *
     * @param context the complete context for the invocation of this policy. Contains all available data.
     */
    void merge(RoutingContext context);

    /**
     * Destroys this factory and frees up any resources it has held. Making further calls on a destroyed
     * factory causes a runtime exception.
     */
    void destroy();

}