Skip to content

Commit 1bf11d7

Browse files
committed
Change AsyncRawSocketSender implementing inferface to return Future<V>
1 parent 3d0acc4 commit 1bf11d7

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

src/main/java/org/fluentd/logger/sender/AsyncRawSocketSender.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author mxk
2525
*
2626
*/
27-
public class AsyncRawSocketSender implements Sender {
27+
public class AsyncRawSocketSender implements AsyncSender {
2828

2929
private final class EmitRunnable implements Callable<Boolean> {
3030
private final String tag;
@@ -107,21 +107,14 @@ public void close() {
107107
}
108108

109109
@Override
110-
public boolean emit(String tag, Map<String, Object> data) {
110+
public Future<Boolean> emit(String tag, Map<String, Object> data) {
111111
return emit(tag, System.currentTimeMillis() / 1000, data);
112112
}
113113

114114
@Override
115-
public boolean emit(final String tag, final long timestamp, final Map<String, Object> data) {
115+
public Future<Boolean> emit(final String tag, final long timestamp, final Map<String, Object> data) {
116116
final RawSocketSender sender = this.sender;
117-
try {
118-
Future<Boolean> result = senderTask.submit(new EmitRunnable(tag, data, sender, timestamp));
119-
return result.get();
120-
} catch (InterruptedException e) {
121-
throw new RuntimeException(e);
122-
} catch (ExecutionException e) {
123-
throw new RuntimeException(e);
124-
}
117+
return senderTask.submit(new EmitRunnable(tag, data, sender, timestamp));
125118
}
126119

127120
@Override
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.fluentd.logger.sender;
2+
3+
import org.fluentd.logger.errorhandler.ErrorHandler;
4+
5+
import java.util.Map;
6+
import java.util.concurrent.Future;
7+
8+
public interface AsyncSender {
9+
Future<Boolean> emit(String tag, Map<String, Object> data);
10+
11+
Future<Boolean> emit(String tag, long timestamp, Map<String, Object> data);
12+
13+
void flush();
14+
15+
void close();
16+
17+
String getName();
18+
19+
boolean isConnected();
20+
21+
void setErrorHandler(ErrorHandler errorHandler);
22+
23+
void removeErrorHandler();
24+
}

src/test/java/org/fluentd/logger/sender/TestAsyncRawSocketSender.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import java.util.Map;
1818
import java.util.concurrent.ConcurrentLinkedQueue;
1919
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.ExecutionException;
2021
import java.util.concurrent.ExecutorService;
2122
import java.util.concurrent.Executors;
23+
import java.util.concurrent.Future;
2224
import java.util.concurrent.TimeUnit;
2325
import java.util.concurrent.atomic.AtomicBoolean;
2426

@@ -52,7 +54,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
5254
fluentd.waitUntilReady();
5355

5456
// start asyncSenders
55-
Sender asyncSender = new AsyncRawSocketSender("localhost", port);
57+
AsyncSender asyncSender = new AsyncRawSocketSender("localhost", port);
5658
Map<String, Object> data = new HashMap<String, Object>();
5759
data.put("t1k1", "t1v1");
5860
data.put("t1k2", "t1v2");
@@ -115,7 +117,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
115117
fluentd.waitUntilReady();
116118

117119
// start asyncSenders
118-
Sender asyncSender = new AsyncRawSocketSender("localhost", port);
120+
AsyncSender asyncSender = new AsyncRawSocketSender("localhost", port);
119121
int count = 10000;
120122
for (int i = 0; i < count; i++) {
121123
String tag = "tag:i";
@@ -186,7 +188,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
186188
fluentds[1].waitUntilReady();
187189

188190
// start AsyncSenders
189-
Sender[] asyncSenders = new Sender[2];
191+
AsyncSender[] asyncSenders = new AsyncSender[2];
190192
int[] counts = new int[2];
191193
asyncSenders[0] = asyncRawSocketSender;
192194
counts[0] = 10000;
@@ -254,7 +256,7 @@ public void run() {
254256
}
255257

256258
@Test
257-
public void testBufferingAndResending() throws InterruptedException, IOException {
259+
public void testBufferingAndResending() throws InterruptedException, IOException, ExecutionException {
258260
final ConcurrentLinkedQueue<Event> readEvents = new ConcurrentLinkedQueue<Event>();
259261
final CountDownLatch countDownLatch = new CountDownLatch(4);
260262
int port = MockFluentd.randomPort();
@@ -278,12 +280,12 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
278280
fluentd.start();
279281
fluentd.waitUntilReady();
280282

281-
Sender asyncSender = new AsyncRawSocketSender("localhost", port);
283+
AsyncSender asyncSender = new AsyncRawSocketSender("localhost", port);
282284
assertFalse(asyncSender.isConnected());
283285
Map<String, Object> data = new HashMap<String, Object>();
284286
data.put("key0", "v0");
285-
boolean emitted1 = asyncSender.emit("tag0", data);
286-
assertTrue(emitted1);
287+
Future<Boolean> emitted1 = asyncSender.emit("tag0", data);
288+
assertTrue(emitted1.get());
287289

288290
// close fluentd to make the next sending failed
289291
TimeUnit.MILLISECONDS.sleep(500);
@@ -294,21 +296,21 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
294296

295297
data = new HashMap<String, Object>();
296298
data.put("key0", "v1");
297-
boolean emitted2 = asyncSender.emit("tag0", data);
298-
assertTrue(emitted2);
299+
Future<Boolean> emitted2 = asyncSender.emit("tag0", data);
300+
assertTrue(emitted2.get());
299301

300302
// wait to avoid the suppression of reconnection
301303
TimeUnit.MILLISECONDS.sleep(500);
302304

303305
data = new HashMap<String, Object>();
304306
data.put("key0", "v2");
305-
boolean emitted3 = asyncSender.emit("tag0", data);
306-
assertTrue(emitted3);
307+
Future<Boolean> emitted3 = asyncSender.emit("tag0", data);
308+
assertTrue(emitted3.get());
307309

308310
data = new HashMap<String, Object>();
309311
data.put("key0", "v3");
310-
boolean emitted4 = asyncSender.emit("tag0", data);
311-
assertTrue(emitted4);
312+
Future<Boolean> emitted4 = asyncSender.emit("tag0", data);
313+
assertTrue(emitted4.get());
312314

313315
countDownLatch.await(500, TimeUnit.MILLISECONDS);
314316

@@ -382,7 +384,7 @@ public void run() {
382384
});
383385

384386
// start asyncSenders
385-
Sender asyncSender = new AsyncRawSocketSender("localhost", port);
387+
AsyncSender asyncSender = new AsyncRawSocketSender("localhost", port);
386388
String tag = "tag";
387389
int i;
388390
for (i = 0; i < 1000000; i++) { // Enough to fill the sender's buffer
@@ -392,7 +394,7 @@ public void run() {
392394

393395
if (bufferFull.getCount() > 0) {
394396
// Fill the sender's buffer
395-
if (!asyncSender.emit(tag, record)) {
397+
if (!asyncSender.emit(tag, record).get()) {
396398
// Buffer full. Need to recover the fluentd
397399
bufferFull.countDown();
398400
Thread.sleep(2000);

0 commit comments

Comments
 (0)