aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-core/src/main/java/com/yahoo/feedapi/SingleSender.java
blob: 896812b58e7b1d26073962df214d3e02b150b28e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedapi;

import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentRemove;
import com.yahoo.document.DocumentUpdate;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.RemoveDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.UpdateDocumentMessage;
import com.yahoo.messagebus.Message;
import java.util.ArrayList;
import java.util.List;

/** Simplifies sending messages belonging to a single result callback. */
public class SingleSender implements SimpleFeedAccess {

    private final SharedSender.ResultCallback owner;
    private final SharedSender sender;
    private final List<MessageProcessor> messageProcessors = new ArrayList<>();

    public SingleSender(SharedSender.ResultCallback owner, SharedSender sender) {
        this.owner = owner;
        this.sender = sender;
    }

    @Override
    public void put(DocumentPut put) {
        send(new PutDocumentMessage(put));
    }

    @Override
    public void remove(DocumentRemove remove) {
        send(new RemoveDocumentMessage(remove));
    }

    @Override
    public void update(DocumentUpdate update) {
        send(new UpdateDocumentMessage(update));
    }

    @Override
    public boolean isAborted() {
        return owner.isAborted();
    }

    public void addMessageProcessor(MessageProcessor processor) {
        messageProcessors.add(processor);
    }

    // Runs all message processors on the message and returns it.
    private Message processMessage(Message m) {
        for (MessageProcessor processor : messageProcessors) {
            processor.process(m);
        }
        return m;
    }

    /**
     * Sends the given message.
     *
     * @param m          The message to send
     */
    public void send(Message m) {
        sender.send(processMessage(m), owner, true);
    }

    public void done() {
        // empty
    }

    public boolean waitForPending(long timeoutMs) {
        return sender.waitForPending(owner, timeoutMs);
    }

    @Override
    public void close() { }
}