aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/RPCMessageBus.java
blob: da930dba513a0a1c6cd5295d07c208b97f8f87e8 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.messagebus.network.Identity;
import com.yahoo.messagebus.network.rpc.RPCNetwork;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;

import java.util.Arrays;
import java.util.List;

/**
 * The RPCMessageBus class wraps a MessageBus with an RPCNetwork and handles reconfiguration. Please note that according
 * to the object shutdown order, you must shut down all sessions before shutting down this object.
 *
 * @author Simon Thoresen Hult
 */
public class RPCMessageBus extends NetworkMessageBus {

    private final ConfigAgent configAgent;

    /**
     * Constructs a new instance of this class.
     *
     * @param mbusParams   A complete set of message bus parameters, including messagebus config.
     * @param rpcParams    A complete set of network parameters, including rpc network config.
     */
    public RPCMessageBus(MessageBusParams mbusParams, RPCNetworkParams rpcParams) {
        this(mbusParams, new RPCNetwork(rpcParams));
    }

    private RPCMessageBus(MessageBusParams mbusParams, RPCNetwork network) {
        super(network, new MessageBus(network, mbusParams));
        configAgent = new ConfigAgent(mbusParams.getMessageBusConfig(), getMessageBus());
    }

    /**
     * Constructs a new instance of this class.
     *
     * @param mbusParams   A complete set of message bus parameters.
     * @param rpcParams    A complete set of network parameters.
     * @param routingCfgId The config id for message bus routing specs.
     */
    public RPCMessageBus(MessageBusParams mbusParams, RPCNetworkParams rpcParams, String routingCfgId) {
        this(mbusParams, new RPCNetwork(rpcParams), routingCfgId);
    }

    private RPCMessageBus(MessageBusParams mbusParams, RPCNetwork network, String routingCfgId) {
        this(new MessageBus(network, mbusParams), network, routingCfgId);
    }

    private RPCMessageBus(MessageBus messageBus, RPCNetwork network, String routingCfgId) {
        super(network, messageBus);
        configAgent = new ConfigAgent(routingCfgId != null ? routingCfgId : "client", messageBus);
        configAgent.subscribe();
    }

    /**
     * This constructor requires an array of protocols that it is to support, as well as the host application's config
     * identifier. That identifier is necessary so that all created sessions can be uniquely identified on the network.
     *
     * @param protocols    An array of known protocols.
     * @param rpcParams    A complete set of network parameters.
     * @param routingCfgId The config id for message bus routing specs.
     */
    public RPCMessageBus(List<Protocol> protocols, RPCNetworkParams rpcParams, String routingCfgId) {
        this(new MessageBusParams().addProtocols(protocols), rpcParams, routingCfgId);
    }

    /**
     * This constructor requires a single protocol that it is to support, as well as the host application's config
     * identifier.
     *
     * @param protocol An instance of the known protocol.
     * @param configId The host application's config id. This will be used to resolve the service name prefix used when
     *                 registering with the slobrok. Using null here is allowed, but will not allow intermediate- or
     *                 destination sessions to be routed to.
     */
    public RPCMessageBus(Protocol protocol, String configId) {
        this(Arrays.asList(protocol), new RPCNetworkParams().setIdentity(new Identity(configId)), null);
    }

    /**
     * Sets the destroyed flag to true. The very first time this method is called, it cleans up all its dependencies.
     * Even if you retain a reference to this object, all of its content is allowed to be garbage collected.
     *
     * @return true if content existed and was destroyed.
     */
    @Override
    public boolean destroy() {
        boolean destroyed = super.destroy();
        if (destroyed)
            configAgent.shutdown();
        return destroyed;
    }

    /** Returns the network of this as a RPCNetwork */
    public RPCNetwork getRPCNetwork() { return (RPCNetwork)getNetwork(); }

}