summaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/RPCMessageBus.java
blob: c1c948cbdf4da824b66592d946c1cc5392d4338e (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
100
101
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.log.LogLevel;
import com.yahoo.messagebus.network.Identity;
import com.yahoo.messagebus.network.Network;
import com.yahoo.messagebus.network.rpc.RPCNetwork;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

/**
 * 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 <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class RPCMessageBus extends NetworkMessageBus {

    private static final Logger log = Logger.getLogger(RPCMessageBus.class.getName());
    private final ConfigAgent configAgent;

    /**
     * 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);
    }

    // Overrides Object.
    @Override
    protected void finalize() throws Throwable {
        try {
            if (destroy()) {
                log.log(LogLevel.WARNING, "RPCMessageBus destroyed by finalizer, please review application shutdown logic.");
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * 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(); }

}