CID-1254832 tyr-with-resource

This commit is contained in:
Daan Hoogland 2014-11-18 15:48:31 +01:00
parent 22ad646aca
commit 6bc0defa1d
1 changed files with 3 additions and 6 deletions

View File

@ -78,17 +78,14 @@ public class GraphiteClient {
* @param timeStamp the timestamp
*/
public void sendMetrics(Map<String, Integer> metrics, long timeStamp) {
try {
DatagramSocket sock = new DatagramSocket();
InetAddress addr = InetAddress.getByName(graphiteHost);
try (DatagramSocket sock = new DatagramSocket()){
InetAddress addr = InetAddress.getByName(this.graphiteHost);
for (Map.Entry<String, Integer> metric: metrics.entrySet()) {
byte[] message = new String(metric.getKey() + " " + metric.getValue() + " " + timeStamp + "\n").getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, addr, graphitePort);
sock.send(packet);
}
sock.close();
} catch (UnknownHostException e) {
throw new GraphiteException("Unknown host: " + graphiteHost);
} catch (IOException e) {
@ -122,4 +119,4 @@ public class GraphiteClient {
metrics.put(key, value);
sendMetrics(metrics, timeStamp);
}
}
}