Skip to content

Commit f976c10

Browse files
New 1.5 build
Socket reconnect logic for streaming Change-Id: Ie6de34db8af5e88767a2b60967f7a36ddba727ee
1 parent ec07548 commit f976c10

10 files changed

Lines changed: 101 additions & 39 deletions

File tree

TODO

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Simple Load Balancing , round robin
2-
Connection retry/re-establishment logic in REST appenders
3-
Build in latest Splunk Java SDK
1+
Build in final release Splunk Java SDK

build/build.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ temp_build=temp_build
1111
main_jar=splunklogging.jar
1212
suffix=.tar.gz
1313
compressionType=gzip
14-
id=splunklogging
14+
id=splunklogging
15+
javaClassTarget=1.5

build/build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<mkdir dir="${temp_build}/${classes}" />
3030

31-
<javac srcdir="${src}" destdir="${temp_build}/${classes}">
31+
<javac srcdir="${src}" destdir="${temp_build}/${classes}" target="${javaClassTarget}">
3232
<classpath refid="project.class.path" />
3333
</javac>
3434

lib/splunk-sdk.jar

100644100755
-166 Bytes
Binary file not shown.

lib/splunk.jar

100644100755
-1.48 KB
Binary file not shown.
-1.95 KB
Binary file not shown.

src/com/dtdsoftware/splunk/logging/SplunkLogEvent.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@
4040
*/
4141
public class SplunkLogEvent {
4242

43-
/**
44-
* Simple shallow cloning method
45-
*/
46-
public SplunkLogEvent clone(){
47-
48-
SplunkLogEvent clone = new SplunkLogEvent();
49-
clone.quoteValues = this.quoteValues;
50-
clone.useInternalDate = this.useInternalDate;
51-
clone.eventMessage = new StringBuffer();
52-
clone.eventMessage.append(this.eventMessage);
53-
return clone;
54-
}
55-
/**
56-
* Default private constructor used for cloning
57-
*/
58-
private SplunkLogEvent(){}
59-
6043

6144
/**
6245
* Contents of the event message
@@ -67,7 +50,8 @@ private SplunkLogEvent(){}
6750
* Whether or not to put quotes around values
6851
*/
6952
private boolean quoteValues = true;
70-
53+
54+
7155
/**
7256
* Whether or not to add a date to the event string
7357
*/
@@ -1223,10 +1207,7 @@ public SplunkLogEvent(String eventName, String eventID,
12231207
this.eventMessage = new StringBuffer();
12241208
this.quoteValues = quoteValues;
12251209
this.useInternalDate = useInternalDate;
1226-
if (useInternalDate) {
1227-
this.eventMessage.append(DATEFORMATTER.format(new Date())).append(
1228-
PAIRDELIM);
1229-
}
1210+
12301211
addPair(PREFIX_NAME, eventName);
12311212
addPair(PREFIX_EVENT_ID, eventID);
12321213
}
@@ -1244,6 +1225,28 @@ public SplunkLogEvent(String eventName, String eventID) {
12441225

12451226
this(eventName, eventID, true, true);
12461227
}
1228+
1229+
/**
1230+
* Default constructor
1231+
*/
1232+
public SplunkLogEvent(){
1233+
1234+
this.eventMessage = new StringBuffer();
1235+
}
1236+
1237+
/**
1238+
* Simple shallow cloning method
1239+
*/
1240+
public SplunkLogEvent clone(){
1241+
1242+
SplunkLogEvent clone = new SplunkLogEvent();
1243+
clone.quoteValues = this.quoteValues;
1244+
clone.useInternalDate = this.useInternalDate;
1245+
clone.eventMessage.append(this.eventMessage);
1246+
1247+
return clone;
1248+
}
1249+
12471250

12481251
/**
12491252
* Add a key value pair
@@ -1328,10 +1331,20 @@ public void addPair(String key, String value) {
13281331
*/
13291332
public String toString() {
13301333

1331-
String event = eventMessage.toString();
1334+
String event="";
1335+
1336+
if (useInternalDate) {
1337+
StringBuffer clonedMessage = new StringBuffer();
1338+
clonedMessage.append(DATEFORMATTER.format(new Date())).append(
1339+
PAIRDELIM).append(this.eventMessage);
1340+
event = clonedMessage.toString();
1341+
}
1342+
else
1343+
event = eventMessage.toString();
13321344
// trim off trailing pair delim char(s)
13331345
return event.substring(0, event.length() - PAIRDELIM.length());
13341346
}
1347+
13351348

13361349
public void setAcManagementDestNtDomain(String acManagementDestNtDomain) {
13371350
addPair(AC_MANAGEMENT_DEST_NT_DOMAIN, acManagementDestNtDomain);

src/com/dtdsoftware/splunk/logging/SplunkLogEventFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public abstract class SplunkLogEventFactory {
3434
*/
3535
public static void addTemplate(String name, SplunkLogEvent template) {
3636

37-
templates.put(name, template);
37+
if(template != null)
38+
templates.put(name, template);
3839
}
3940

4041
/**

src/com/dtdsoftware/splunk/logging/SplunkRestInput.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public class SplunkRestInput {
2424
private Receivers receivers;
2525
private Args args;
2626

27+
// connection props
28+
private String user = "";
29+
private String pass = "";
30+
private String host = "";
31+
private int port;
32+
2733
// streaming objects
2834
private Socket streamSocket = null;
2935
private OutputStream ostream;
@@ -50,18 +56,42 @@ public class SplunkRestInput {
5056
public SplunkRestInput(String user, String pass, String host, int port,
5157
RestEventData red, boolean stream) throws Exception {
5258

59+
this.host = host;
60+
this.port = port;
61+
this.user = user;
62+
this.pass = pass;
63+
this.args = createArgs(red);
64+
65+
initService();
66+
67+
if (stream) {
68+
openStream();
69+
}
70+
71+
}
72+
73+
private void initService() {
74+
5375
this.service = new Service(host, port);
5476
this.service.login(user, pass);
5577
this.receivers = new Receivers(this.service);
5678

57-
this.args = createArgs(red);
79+
}
80+
81+
/**
82+
* open the stream
83+
*
84+
* @throws Exception
85+
*/
86+
private void openStream() throws Exception {
87+
88+
if (this.receivers != null) {
5889

59-
if (stream) {
6090
this.streamSocket = this.receivers.attach(args);
61-
ostream = streamSocket.getOutputStream();
62-
writerOut = new OutputStreamWriter(ostream, "UTF8");
63-
}
91+
this.ostream = streamSocket.getOutputStream();
92+
this.writerOut = new OutputStreamWriter(ostream, "UTF8");
6493

94+
}
6595
}
6696

6797
/**
@@ -76,16 +106,20 @@ private Args createArgs(RestEventData red) {
76106

77107
if (red != null) {
78108
if (red.getIndex().length() > 0)
79-
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_INDEX, red.getIndex());
109+
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_INDEX, red
110+
.getIndex());
80111
if (red.getSource().length() > 0)
81-
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_SOURCE, red.getSource());
112+
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_SOURCE, red
113+
.getSource());
82114
if (red.getSourcetype().length() > 0)
83115
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_SOURCETYPE, red
84116
.getSourcetype());
85117
if (red.getHost().length() > 0)
86-
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_HOST, red.getHost());
118+
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_HOST, red
119+
.getHost());
87120
if (red.getHostRegex().length() > 0)
88-
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_HOSTREGEX, red.getHostRegex());
121+
urlArgs.add(RestEventData.RECEIVERS_SIMPLE_ARG_HOSTREGEX, red
122+
.getHostRegex());
89123

90124
}
91125
return urlArgs;
@@ -96,6 +130,7 @@ private Args createArgs(RestEventData red) {
96130
*/
97131
public void closeStream() {
98132
try {
133+
99134
if (writerOut != null) {
100135
writerOut.flush();
101136
writerOut.close();
@@ -125,12 +160,25 @@ public void sendEvent(String message) {
125160
public void streamEvent(String message) {
126161

127162
try {
163+
128164
if (writerOut != null) {
165+
129166
writerOut.write(message + "\n");
130167
writerOut.flush();
168+
131169
}
170+
132171
} catch (IOException e) {
133-
}
172+
try {
173+
closeStream();
174+
} catch (Exception e1) {
175+
}
134176

177+
try {
178+
initService();
179+
openStream();
180+
} catch (Exception e2) {
181+
}
182+
}
135183
}
136184
}

src/com/dtdsoftware/splunk/logging/examples/Example.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static void main(String[] args) {
4141

4242
// log a splunk log event generated string
4343
logger.info(event.toString());
44+
4445

4546
}
4647

0 commit comments

Comments
 (0)