Skip to content

Commit

Permalink
* switched from using UDP to socket
Browse files Browse the repository at this point in the history
* experimented with different client examples
  • Loading branch information
r1j0 committed May 1, 2012
1 parent 7adb996 commit 347ca31
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
22 changes: 22 additions & 0 deletions src/com/github/r1j0/statsd/client/StatsdClient.java
@@ -1,7 +1,10 @@
package com.github.r1j0.statsd.client;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

Expand Down Expand Up @@ -70,6 +73,23 @@ private boolean createMessage(final String key, final int value, final double un


public boolean send(final String message) {
try {
Socket socket = new Socket(host, port);
Writer writer = new OutputStreamWriter(socket.getOutputStream());
writer.write(message);
writer.flush();
writer.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}


public boolean sendUdp(final String message) {
final DatagramChannel channel;

try {
Expand All @@ -88,6 +108,8 @@ public boolean send(final String message) {
log.warn("Bytes written: " + bytesWritten + " but message had: " + messageLength + " bytes.");
}

log.debug("MESSAGE:" + message);

channel.close();
} catch (IOException e) {
e.printStackTrace();
Expand Down
21 changes: 15 additions & 6 deletions src/com/github/r1j0/statsd/client/StatsdTestClient.java
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Random;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
Expand All @@ -15,14 +14,19 @@

public class StatsdTestClient {

private static final String HOST = "127.0.0.1";
private static final int PORT = 39390;
private static final String HOST = "192.168.1.21";
private static final int PORT = 2003;


public static void main(String[] args) throws IOException, InterruptedException {
StatsdClient statsdClient = new StatsdClient("192.168.1.21", 2003);

while (true) {
for (int i = 0; i < 5; i++) {
new Automatic().start();
String message = "system.loadavg_1min 0.60 " + System.currentTimeMillis() / 1000L + "\nsystem.loadavg_5min 0.80 " + System.currentTimeMillis() / 1000L + "\nsystem.loadavg_15min 0.50 " + System.currentTimeMillis() / 1000L + "\n";

for (int i = 0; i < 1; i++) {
statsdClient.send(message);
// new Automatic().start();
}

Thread.sleep(2000);
Expand All @@ -47,7 +51,12 @@ public void run() {
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

connector.setHandler(new StatsdTestClientHandler("bucket:value|type@sample RAND: " + new Random().nextInt()));
String message = "\nsystem.loadavg_1min 0.60 " + System.currentTimeMillis() / 1000L + "\nsystem.loadavg_5min 0.80 " + System.currentTimeMillis() / 1000L + "\nsystem.loadavg_15min 0.50 " + System.currentTimeMillis() / 1000L + "\n\n";
System.out.println("MESSAGE:" + message);
connector.setHandler(new StatsdTestClientHandler(message));
// connector.setHandler(new
// StatsdTestClientHandler("bucket:value|type@sample RAND: " + new
// Random().nextInt()));
ConnectFuture future = connector.connect(new InetSocketAddress(HOST, PORT));

if (!future.isConnected()) {
Expand Down
5 changes: 4 additions & 1 deletion src/com/github/r1j0/statsd/example/StatsdClientExample.java
Expand Up @@ -5,7 +5,10 @@
public class StatsdClientExample {

public static void main(String[] args) {
StatsdClient statsdClient = new StatsdClient("127.0.0.1", 39390);
// StatsdClient statsdClient = new StatsdClient("192.168.1.21", 2003);
// statsdClient.send("system.loadavg_1min 0.2 " + System.currentTimeMillis() / 1000L + "\n" + "system.loadavg_5min 0.7 " + System.currentTimeMillis() / 1000L + "\n");

StatsdClient statsdClient = new StatsdClient("192.168.1.21", 2003);
statsdClient.increment("my.bucket.test", 1);
statsdClient.increment("my.bucket.test", 1);
statsdClient.increment("my.bucket.test", 1);
Expand Down

0 comments on commit 347ca31

Please sign in to comment.