-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getLogger(..., Sender) method to the factory; add an asynchronous…
… wrapper around RawSocketSender
- Loading branch information
Mike Kobyakov
committed
Sep 4, 2014
1 parent
cb20c25
commit f38dafb
Showing
2 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/org/fluentd/logger/sender/AsyncRawSocketSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
package org.fluentd.logger.sender; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import org.fluentd.logger.sender.ExponentialDelayReconnector; | ||
import org.fluentd.logger.sender.RawSocketSender; | ||
import org.fluentd.logger.sender.Reconnector; | ||
import org.fluentd.logger.sender.Sender; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* @author mkobyakov | ||
* | ||
*/ | ||
public class AsyncRawSocketSender implements Sender { | ||
|
||
private RawSocketSender sender; | ||
private Reconnector reconnector; | ||
|
||
@SuppressWarnings("unused") | ||
private static final Logger logger = LoggerFactory.getLogger(AsyncRawSocketSender.class); | ||
|
||
private static final ExecutorService flusher = Executors.newSingleThreadExecutor(); | ||
|
||
public AsyncRawSocketSender() { | ||
this("localhost", 24224); | ||
} | ||
|
||
public AsyncRawSocketSender(String host, int port) { | ||
this(host, port, 3 * 1000, 8 * 1024 * 1024); | ||
} | ||
|
||
public AsyncRawSocketSender(String host, int port, int timeout, | ||
int bufferCapacity) { | ||
this(host, port, timeout, bufferCapacity, | ||
new ExponentialDelayReconnector()); | ||
} | ||
|
||
public AsyncRawSocketSender(String host, int port, int timeout, | ||
int bufferCapacity, Reconnector reconnector) { | ||
this.reconnector = reconnector; | ||
this.sender = new RawSocketSender(host, port, timeout, bufferCapacity, | ||
reconnector); | ||
} | ||
|
||
@Override | ||
public synchronized void flush() { | ||
final RawSocketSender sender = this.sender; | ||
flusher.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
sender.flush(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
sender.close(); | ||
} | ||
|
||
@Override | ||
public boolean emit(String tag, Map<String, Object> data) { | ||
return emit(tag, System.currentTimeMillis() / 1000, data); | ||
} | ||
|
||
@Override | ||
public boolean emit(final String tag, final long timestamp, final Map<String, Object> data) { | ||
final RawSocketSender sender = this.sender; | ||
flusher.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
sender.emit(tag, timestamp, data); | ||
} | ||
}); | ||
|
||
return sender.isConnected() || reconnector.enableReconnection(System.currentTimeMillis()); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return sender.getName(); | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return sender.isConnected(); | ||
} | ||
} |