blob: 9020bfaffe32a6a4e1ebb96cec60978a28723b79 (
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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.messagebus;
import com.yahoo.component.AbstractComponent;
import com.yahoo.messagebus.network.Network;
import com.yahoo.messagebus.network.NetworkMultiplexer;
import com.yahoo.messagebus.network.rpc.RPCNetwork;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.messagebus.shared.NullNetwork;
/**
* Holds a reference to a singleton {@link NetworkMultiplexer}.
*
* @author jonmv
*/
public class NetworkMultiplexerHolder extends AbstractComponent {
private final Object monitor = new Object();
private boolean destroyed = false;
private NetworkMultiplexer net;
/** Get the singleton RPCNetworkAdapter, creating it if this hasn't yet been done. */
public NetworkMultiplexer get(RPCNetworkParams params) {
synchronized (monitor) {
if (destroyed)
throw new IllegalStateException("Component already destroyed");
return net = net != null ? net : NetworkMultiplexer.shared(newNetwork(params));
}
}
private Network newNetwork(RPCNetworkParams params) {
return params.getSlobroksConfig().slobrok().isEmpty() ? new NullNetwork() : new RPCNetwork(params);
}
@Override
public void deconstruct() {
synchronized (monitor) {
net.destroy();
net = null;
destroyed = true;
}
}
}
|