aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/ai/vespa/logserver/protocol/RpcServer.java
blob: 48e64b029ccdbf1cb15dabe0b0dc5b782e023243 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.logserver.protocol;

import com.yahoo.jrt.Acceptor;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.Method;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;

/**
 * A JRT based RPC server for handling log requests
 *
 * @author bjorncs
 */
public class RpcServer implements AutoCloseable {

    private final Supervisor supervisor;
    private final int listenPort;
    private Acceptor acceptor;

    public RpcServer(int listenPort) {
        supervisor = new Supervisor(new Transport("logserver-" + listenPort));
        this.listenPort = listenPort;
    }

    public void addMethod(Method method) {
        supervisor.addMethod(method);
    }

    public void start() {
        try {
            acceptor = supervisor.listen(new Spec(listenPort));
        } catch (ListenFailedException e) {
            throw new RuntimeException(e);
        }
    }

    int listenPort() {
        return acceptor.port();
    }

    @Override
    public void close() {
        if (acceptor != null) {
            acceptor.shutdown().join();
        }
        supervisor.transport().shutdown().join();
    }
}