-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract a class for the UDP sending, making it easier to play with ba…
…tching implementations for issue #15
- Loading branch information
Showing
2 changed files
with
82 additions
and
59 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
77 changes: 77 additions & 0 deletions
77
src/main/java/com/timgroup/statsd/NonBlockingUdpSender.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,77 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.DatagramChannel; | ||
import java.nio.charset.Charset; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class NonBlockingUdpSender { | ||
private final Charset encoding; | ||
private final DatagramChannel clientSocket; | ||
private final ExecutorService executor; | ||
private StatsDClientErrorHandler handler; | ||
|
||
public NonBlockingUdpSender(String hostname, int port, Charset encoding, StatsDClientErrorHandler handler) throws IOException { | ||
this.encoding = encoding; | ||
this.handler = handler; | ||
this.clientSocket = DatagramChannel.open(); | ||
this.clientSocket.connect(new InetSocketAddress(hostname, port)); | ||
|
||
this.executor = Executors.newSingleThreadExecutor(new ThreadFactory() { | ||
final ThreadFactory delegate = Executors.defaultThreadFactory(); | ||
@Override public Thread newThread(Runnable r) { | ||
Thread result = delegate.newThread(r); | ||
result.setName("StatsD-" + result.getName()); | ||
result.setDaemon(true); | ||
return result; | ||
} | ||
}); | ||
} | ||
|
||
public void stop() { | ||
try { | ||
executor.shutdown(); | ||
executor.awaitTermination(30, TimeUnit.SECONDS); | ||
} | ||
catch (Exception e) { | ||
handler.handle(e); | ||
} | ||
finally { | ||
if (clientSocket != null) { | ||
try { | ||
clientSocket.close(); | ||
} | ||
catch (Exception e) { | ||
handler.handle(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void send(final String message) { | ||
try { | ||
executor.execute(new Runnable() { | ||
@Override public void run() { | ||
blockingSend(message); | ||
} | ||
}); | ||
} | ||
catch (Exception e) { | ||
handler.handle(e); | ||
} | ||
} | ||
|
||
private void blockingSend(String message) { | ||
try { | ||
final byte[] sendData = message.getBytes(encoding); | ||
clientSocket.write(ByteBuffer.wrap(sendData)); | ||
} catch (Exception e) { | ||
handler.handle(e); | ||
} | ||
} | ||
} |