Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cloudbees tcp syslog test
package org.ivan.cloudbees;
import com.cloudbees.syslog.Facility;
import com.cloudbees.syslog.Severity;
import com.cloudbees.syslog.sender.TcpSyslogMessageSender;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class InetFailureTest {
@Test
public void connectionDrop() throws Exception {
new Thread() {
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(601);
Socket peer = server.accept();
sleep(100);
pullMessage(peer);
peer.close();
peer = server.accept();
System.out.println("reaccept done");
sleep(200);
pullMessage(peer);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Thread.sleep(100);
TcpSyslogMessageSender syslog = new TcpSyslogMessageSender();
syslog.setDefaultMessageHostname("myhostname"); // some syslog cloud services may use this field to transmit a secret key
syslog.setDefaultAppName("myapp");
syslog.setDefaultFacility(Facility.USER);
syslog.setDefaultSeverity(Severity.INFORMATIONAL);
syslog.setSyslogServerHostname("localhost");
syslog.setSyslogServerPort(601);
syslog.sendMessage("Hello syslog!");
for(int i = 0; i < 5; i++) {
Thread.sleep(200);
try {
syslog.sendMessage("Bye syslog!");
} catch (IOException e) {
System.out.println("bye #" + i + ": " + e.getMessage());
}
}
Thread.sleep(1000);
}
private void pullMessage(Socket peer) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(peer.getInputStream()));
char[] chars = new char[1024];
while(br.ready()) {
int nread = br.read(chars);
System.out.println(new String(chars, 0, nread));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.