Skip to content
This repository
Browse code

I have change it to use Java NIO.

  • Loading branch information...
commit 82b1e29cd969f7b7fd772ed073611c00ebc1561c 1 parent 116dfe3
nire authored August 03, 2011

Showing 1 changed file with 33 additions and 26 deletions. Show diff stats Hide diff stats

  1. 59  StatsdClient.java
59  StatsdClient.java
@@ -28,13 +28,13 @@
28 28
  * You know... the "Java way."
29 29
  */
30 30
 
31  
-import java.util.Random;
32 31
 import java.io.IOException;
33  
-import java.net.DatagramPacket;
34  
-import java.net.DatagramSocket;
35 32
 import java.net.InetAddress;
36  
-import java.net.SocketException;
  33
+import java.net.InetSocketAddress;
37 34
 import java.net.UnknownHostException;
  35
+import java.nio.ByteBuffer;
  36
+import java.nio.channels.DatagramChannel;
  37
+import java.util.Random;
38 38
 
39 39
 import org.apache.log4j.Logger;
40 40
 
@@ -42,19 +42,16 @@
42 42
 	private static Random RNG = new Random();
43 43
 	private static Logger log = Logger.getLogger(StatsdClient.class.getName());
44 44
 
45  
-	private InetAddress _host;
46  
-	private int _port;
47  
-	
48  
-	private DatagramSocket _sock;
  45
+	private InetSocketAddress _address;
  46
+	private DatagramChannel _channel;
49 47
 
50  
-	public StatsdClient(String host, int port) throws UnknownHostException, SocketException {
  48
+	public StatsdClient(String host, int port) throws UnknownHostException, IOException {
51 49
 		this(InetAddress.getByName(host), port);
52 50
 	}
53 51
 
54  
-	public StatsdClient(InetAddress host, int port) throws SocketException {
55  
-		_host = host;
56  
-		_port = port;
57  
-		_sock = new DatagramSocket();
  52
+	public StatsdClient(InetAddress host, int port) throws IOException {
  53
+		_address = new InetSocketAddress(host, port);
  54
+		_channel = DatagramChannel.open();
58 55
 	}
59 56
 
60 57
 	public boolean timing(String key, int value) {
@@ -74,7 +71,7 @@ public boolean decrement(String key, int magnitude) {
74 71
 	}
75 72
 
76 73
 	public boolean decrement(String key, int magnitude, double sampleRate) {
77  
-		magnitude = magnitude < 0 ? magnitude: -magnitude;
  74
+		magnitude = magnitude < 0 ? magnitude : -magnitude;
78 75
 		return increment(key, magnitude, sampleRate);
79 76
 	}
80 77
 
@@ -83,12 +80,12 @@ public boolean decrement(String... keys) {
83 80
 	}
84 81
 
85 82
 	public boolean decrement(int magnitude, String... keys) {
86  
-		magnitude = magnitude < 0 ? magnitude: -magnitude;
  83
+		magnitude = magnitude < 0 ? magnitude : -magnitude;
87 84
 		return increment(magnitude, 1.0, keys);
88 85
 	}
89 86
 
90 87
 	public boolean decrement(int magnitude, double sampleRate, String... keys) {
91  
-		magnitude = magnitude < 0 ? magnitude: -magnitude;
  88
+		magnitude = magnitude < 0 ? magnitude : -magnitude;
92 89
 		return increment(magnitude, sampleRate, keys);
93 90
 	}
94 91
 
@@ -129,8 +126,7 @@ private boolean send(double sampleRate, String... stats) {
129 126
 					}
130 127
 				}
131 128
 			}
132  
-		}
133  
-		else {
  129
+		} else {
134 130
 			for (String stat : stats) {
135 131
 				if (doSend(stat)) {
136 132
 					retval = true;
@@ -143,13 +139,24 @@ private boolean send(double sampleRate, String... stats) {
143 139
 
144 140
 	private boolean doSend(String stat) {
145 141
 		try {
146  
-			byte[] data = stat.getBytes();
147  
-			_sock.send(new DatagramPacket(data, data.length, _host, _port));
148  
-			return true;
149  
-		}
150  
-		catch (IOException e) {
151  
-			log.error(String.format("Could not send stat %s to host %s:%d", stat, _host, _port), e);
  142
+			final byte[] data = stat.getBytes();
  143
+			final ByteBuffer buff = ByteBuffer.wrap(data);
  144
+			final int nbSentBytes = _channel.send(buff, _address);
  145
+
  146
+			if (data.length == nbSentBytes) {
  147
+				return true;
  148
+			} else {
  149
+				log.error(String.format(
  150
+						"Could not send entirely stat %s to host %s:%d. Only sent %i bytes out of %i bytes", stat,
  151
+						_address.getHostName(), _address.getPort(), nbSentBytes, data.length));
  152
+				return false;
  153
+			}
  154
+
  155
+		} catch (IOException e) {
  156
+			log.error(
  157
+					String.format("Could not send stat %s to host %s:%d", stat, _address.getHostName(),
  158
+							_address.getPort()), e);
  159
+			return false;
152 160
 		}
153  
-		return false;
154 161
 	}
155  
-}
  162
+}

0 notes on commit 82b1e29

Please sign in to comment.
Something went wrong with that request. Please try again.