Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Splunk JMS Modular Input message handler to output just the message payload
package com.splunk.modinput.jms.customhandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jms.Message;
import com.splunk.modinput.Stream;
import com.splunk.modinput.StreamEvent;
import com.splunk.modinput.jms.AbstractMessageHandler;
import com.splunk.modinput.jms.JMSModularInput.MessageReceiver;
/**
* Custom message handler to output just the message payload
*
* @author ddallimore
*
*/
public class PayloadOnlyMessageHandler extends AbstractMessageHandler {
@Override
public Stream handleMessage(Message message, MessageReceiver context)
throws Exception {
String rawMessage = getMessageBody(message);
Stream stream = new Stream();
ArrayList<StreamEvent> list = new ArrayList<StreamEvent>();
List<String> chunks = chunkData(rawMessage, 1024);
for (int i = 0; i < chunks.size(); i++) {
StreamEvent event = new StreamEvent();
event.setUnbroken("1");
event.setData(chunks.get(i));
event.setStanza(context.stanzaName);
// if we are seeing the last chunk, set the "done" element
if (i == chunks.size() - 1)
event.setDone(" ");
list.add(event);
}
stream.setEvents(list);
return stream;
}
public static List<String> chunkData(String text, int size) {
List<String> ret = new ArrayList<String>((text.length() + size - 1)
/ size);
for (int start = 0; start < text.length(); start += size) {
ret.add(text.substring(start, Math.min(text.length(), start + size)));
}
return ret;
}
@Override
public void setParams(Map<String, String> params) {
// Do nothing , params not used
}
}
@billclark91

This comment has been minimized.

Copy link

billclark91 commented Feb 14, 2015

Hello,
I would like to build this custome message handler but cannot seem to locate a Splunk SDK which includes com.splunk.modinput. Could you please list the dependencies required to compile?
Thank you in advanced.
Bill Clark

UPDATE:
Figured it out.

For everyone else, the jars are included in the jms_ta app from "Splunk Modular Inputs Java Framework" (https://github.com/damiendallimore/SplunkModularInputsJavaFramework/tree/master/releases). The jars are contained under jms_ta/bin/lib jms.jar and jmsmodinput.jar. These same files are available from your Splunk Server under splunk/etc/apps/jms_ta/bin/lib. Not sure what's better, use server jars or download from github.

@reggie-k

This comment has been minimized.

Copy link

reggie-k commented Jun 1, 2016

Hi,

The code doesn't compile because the return type of handleMessage is void and not Stream.
I am using the jmsmodinput.jar downloaded with the app.

Also, is this code good for outputting JSON message payload as well?
Can you please assist me with this?

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.