/
Serializer.java
161 lines (140 loc) · 4.44 KB
/
Serializer.java
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.eventbus.impl.clustered;
import io.vertx.core.*;
import io.vertx.core.eventbus.Message;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
/**
* @author Thomas Segismont
*/
public class Serializer implements Closeable {
private final ContextInternal ctx;
private final Map<String, SerializerQueue> queues;
private Serializer(ContextInternal context) {
ContextInternal unwrapped = context.unwrap();
if (unwrapped.isEventLoopContext()) {
ctx = unwrapped;
} else {
VertxInternal vertx = unwrapped.owner();
ctx = vertx.createEventLoopContext(unwrapped.nettyEventLoop(), unwrapped.workerPool(), unwrapped.classLoader());
}
queues = new HashMap<>();
if (unwrapped.isDeployment()) {
unwrapped.addCloseHook(this);
}
}
public static Serializer get(ContextInternal context) {
ConcurrentMap<Object, Object> contextData = context.contextData();
Serializer serializer = (Serializer) contextData.get(Serializer.class);
if (serializer == null) {
Serializer candidate = new Serializer(context);
Serializer previous = (Serializer) contextData.putIfAbsent(Serializer.class, candidate);
if (previous == null) {
serializer = candidate;
} else {
serializer = previous;
}
}
return serializer;
}
public <T> void queue(Message<?> message, BiConsumer<String, Promise<T>> selectHandler, Promise<T> promise) {
ctx.emit(v -> {
String address = message.address();
SerializerQueue queue = queues.computeIfAbsent(address, SerializerQueue::new);
queue.add(message, selectHandler, promise);
});
}
@Override
public void close(Promise<Void> completion) {
ctx.emit(v -> {
for (SerializerQueue queue : queues.values()) {
queue.close();
}
completion.complete();
});
}
private class SerializerQueue {
private final Queue<SerializedTask<?>> tasks;
private final String address;
private boolean running;
private boolean closed;
SerializerQueue(String address) {
this.address = address;
this.tasks = new LinkedList<>();
}
void checkPending() {
if (!running) {
running = true;
while (true) {
SerializedTask<?> task = tasks.peek();
if (task != null) {
task.process();
if (tasks.peek() == task) {
// Task will be completed later
break;
}
} else {
queues.remove(address);
break;
}
}
running = false;
}
}
<U> void add(Message<?> msg, BiConsumer<String, Promise<U>> selectHandler, Promise<U> promise) {
SerializedTask<U> serializedTask = new SerializedTask<>(ctx, msg, selectHandler);
Future<U> fut = serializedTask.internalPromise.future();
fut.onComplete(promise);
fut.onComplete(serializedTask);
tasks.add(serializedTask);
checkPending();
}
void processed() {
if (!closed) {
tasks.poll();
checkPending();
}
}
void close() {
closed = true;
while (!tasks.isEmpty()) {
tasks.remove().internalPromise.tryFail("Context is closing");
}
}
private class SerializedTask<U> implements Handler<AsyncResult<U>> {
final Message<?> msg;
final BiConsumer<String, Promise<U>> selectHandler;
final Promise<U> internalPromise;
SerializedTask(
ContextInternal context,
Message<?> msg,
BiConsumer<String, Promise<U>> selectHandler) {
this.msg = msg;
this.selectHandler = selectHandler;
this.internalPromise = context.promise();
}
void process() {
selectHandler.accept(msg.address(), internalPromise);
}
@Override
public void handle(AsyncResult<U> ar) {
processed();
}
}
}
}