aboutsummaryrefslogtreecommitdiffstats
path: root/container-messagebus/src/main/java/com/yahoo/messagebus/shared/SharedSourceSession.java
blob: 016fb8975a713793d68701a76c60e63f838443da (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.shared;

import com.yahoo.jdisc.AbstractResource;
import com.yahoo.jdisc.ResourceReference;
import java.util.logging.Level;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.ReplyHandler;
import com.yahoo.messagebus.Result;
import com.yahoo.messagebus.SourceSession;
import com.yahoo.messagebus.SourceSessionParams;

import java.util.logging.Logger;

/**
 * @author Simon Thoresen Hult
 */
public class SharedSourceSession extends AbstractResource implements ClientSession, ReplyHandler {

    private static final Logger log = Logger.getLogger(SharedSourceSession.class.getName());
    private final SourceSession session;
    private final ResourceReference mbusReference;

    public SharedSourceSession(SharedMessageBus mbus, SourceSessionParams params) {
        if (params.getReplyHandler() != null) {
            throw new IllegalArgumentException("Reply handler must be null.");
        }
        this.session = mbus.messageBus().createSourceSession(params.setReplyHandler(this));
        this.mbusReference = mbus.refer(this);
    }

    public SourceSession session() {
        return session;
    }

    @Override
    public Result sendMessage(Message msg) {
        return session.send(msg);
    }

    public Result sendMessageBlocking(Message msg) throws InterruptedException {
        return session.sendBlocking(msg);
    }

    @Override
    public void handleReply(Reply reply) {
        reply.popHandler().handleReply(reply);
    }

    @Override
    protected void destroy() {
        log.log(Level.FINE, "Destroying shared source session.");
        session.close();
        mbusReference.close();
    }

}