Skip to content

Commit c0fc998

Browse files
committed
Added support for JsonLines to file
1 parent b63e716 commit c0fc998

File tree

6 files changed

+56
-45
lines changed

6 files changed

+56
-45
lines changed

src/com/googlecode/utterlyidle/monitoring/CarbonClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ default void close() throws IOException {
2222
}
2323

2424
static CarbonClient carbonClient(SocketAddress socketAddress, Clock clock) throws IOException {
25-
TcpMessenger messenger = new TcpMessenger(SocketChannel.open(socketAddress));
25+
ChannelMessenger messenger = ChannelMessenger.tcpMessager(socketAddress);
2626
return new CarbonClient() {
2727
@Override
2828
public void gauge(final String name, final long value) throws IOException {

src/com/googlecode/utterlyidle/monitoring/ChannelMessenger.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package com.googlecode.utterlyidle.monitoring;
22

3+
import com.googlecode.totallylazy.LazyException;
4+
5+
import java.io.File;
36
import java.io.IOException;
7+
import java.net.SocketAddress;
8+
import java.nio.channels.DatagramChannel;
9+
import java.nio.channels.FileChannel;
10+
import java.nio.channels.SocketChannel;
411
import java.nio.channels.WritableByteChannel;
512

613
import static com.googlecode.totallylazy.Bytes.bytes;
714
import static com.googlecode.totallylazy.Sequences.sequence;
815
import static java.nio.ByteBuffer.wrap;
16+
import static java.nio.file.StandardOpenOption.APPEND;
17+
import static java.nio.file.StandardOpenOption.CREATE;
18+
import static java.nio.file.StandardOpenOption.WRITE;
919

1020
public class ChannelMessenger<C extends WritableByteChannel> implements Messenger {
1121
protected final C channel;
@@ -23,4 +33,28 @@ public void message(Iterable<? extends String> values) throws IOException {
2333
public void close() throws IOException {
2434
channel.close();
2535
}
36+
37+
public static ChannelMessenger<DatagramChannel> udpMessager(SocketAddress socketAddress) {
38+
try {
39+
return new ChannelMessenger<>(DatagramChannel.open().connect(socketAddress));
40+
} catch (IOException e) {
41+
throw LazyException.lazyException(e);
42+
}
43+
}
44+
45+
public static ChannelMessenger<SocketChannel> tcpMessager(SocketAddress socketAddress) {
46+
try {
47+
return new ChannelMessenger<>(SocketChannel.open(socketAddress));
48+
} catch (IOException e) {
49+
throw LazyException.lazyException(e);
50+
}
51+
}
52+
53+
public static ChannelMessenger<FileChannel> fileMessenger(File file) {
54+
try {
55+
return new ChannelMessenger<>(FileChannel.open(file.toPath(), WRITE, APPEND, CREATE));
56+
} catch (IOException e) {
57+
throw LazyException.lazyException(e);
58+
}
59+
}
2660
}

src/com/googlecode/utterlyidle/monitoring/JsonLinesClient.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import com.googlecode.totallylazy.json.Json;
44

55
import java.io.Closeable;
6+
import java.io.File;
67
import java.io.IOException;
78
import java.net.SocketAddress;
9+
import java.nio.channels.FileChannel;
810
import java.nio.channels.SocketChannel;
11+
import java.nio.channels.WritableByteChannel;
12+
13+
import static com.googlecode.utterlyidle.monitoring.ChannelMessenger.fileMessenger;
14+
import static com.googlecode.utterlyidle.monitoring.ChannelMessenger.tcpMessager;
915

1016
public interface JsonLinesClient extends Closeable {
1117
void send(Object value) throws IOException;
@@ -15,7 +21,18 @@ default void close() throws IOException {
1521
}
1622

1723
static JsonLinesClient jsonClient(SocketAddress socketAddress) throws IOException {
18-
TcpMessenger messenger = new TcpMessenger(SocketChannel.open(socketAddress));
24+
return jsonLinesClient(socketAddress);
25+
}
26+
27+
static JsonLinesClient jsonLinesClient(File file) throws IOException {
28+
return jsonLinesClient(fileMessenger(file));
29+
}
30+
31+
static JsonLinesClient jsonLinesClient(SocketAddress socketAddress) throws IOException {
32+
return jsonLinesClient(tcpMessager(socketAddress));
33+
}
34+
35+
static JsonLinesClient jsonLinesClient(ChannelMessenger<?> messenger) {
1936
return new JsonLinesClient() {
2037
@Override
2138
public void send(final Object value) throws IOException {
@@ -33,4 +50,5 @@ static JsonLinesClient noOp() {
3350
return value -> {
3451
};
3552
}
53+
3654
}

src/com/googlecode/utterlyidle/monitoring/StatsDClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.InetSocketAddress;
66
import java.net.SocketAddress;
77

8+
import static com.googlecode.utterlyidle.monitoring.ChannelMessenger.udpMessager;
89
import static java.lang.String.format;
910

1011
/**
@@ -16,7 +17,7 @@ static StatsDClient statsDClient(String host, int port) throws IOException {
1617
}
1718

1819
static StatsDClient statsDClient(SocketAddress socketAddress) throws IOException {
19-
return statsDClient(UdpMessenger.udpMessager(socketAddress));
20+
return statsDClient(udpMessager(socketAddress));
2021
}
2122

2223
static StatsDClient statsDClient(final Messenger messenger) {

src/com/googlecode/utterlyidle/monitoring/TcpMessenger.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/com/googlecode/utterlyidle/monitoring/UdpMessenger.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)