aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/test/CustomPolicy.java
blob: 70fee668d06d47cb18d8114b187ac11093fdf634 (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
50
51
52
53
54
55
56
57
58
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing.test;

import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.messagebus.routing.RoutingContext;
import com.yahoo.messagebus.routing.RoutingNodeIterator;
import com.yahoo.messagebus.routing.RoutingPolicy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Simon Thoresen Hult
 */
public class CustomPolicy implements RoutingPolicy  {

    private boolean selectOnRetry;
    private final List<Integer> consumableErrors = new ArrayList<Integer>();
    private final List<Route> routes = new ArrayList<Route>();

    public CustomPolicy(boolean selectOnRetry, List<Integer> consumableErrors, List<Route> routes) {
        this.selectOnRetry = selectOnRetry;
        this.consumableErrors.addAll(consumableErrors);
        this.routes.addAll(routes);
    }

    public void select(RoutingContext context) {
        context.trace(1, "Selecting " + routes + ".");
        context.setSelectOnRetry(selectOnRetry);
        for (int e : consumableErrors) {
            context.addConsumableError(e);
        }
        context.addChildren(routes);
    }

    public void merge(RoutingContext context) {
        List<String> lst = new ArrayList<String>();
        Reply ret = new EmptyReply();
        for (RoutingNodeIterator it = context.getChildIterator();
             it.isValid(); it.next())
        {
            lst.add(it.getRoute().toString());
            Reply reply = it.getReplyRef();
            for (int i = 0; i < reply.getNumErrors(); ++i) {
                ret.addError(reply.getError(i));
            }
        }
        context.setReply(ret);
        context.trace(1, "Merged " + lst + ".");
    }

    @Override
    public void destroy() {
    }

}