SBE Log Decoder decodes provided summary file according to the template and writes decoded messages into a separate file.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import biz.onixs.cme.ilink3.handler.Utils; import biz.onixs.sbe.ByteDecoder; import biz.onixs.sbe.ByteDecoderFactory; import biz.onixs.sbe.IMessage; import biz.onixs.sbe.def.MessageSchema; import biz.onixs.util.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.Base64; /** * Sbe Log Decoder sample. */ public class SbeLogDecoder implements Runnable { private static final Logger LOG = LoggerFactory.getLogger(SbeLogDecoder. class ); private static final String templateFile = "sample/ilinkbinary.xml" ; private static final char FileExtensionDelimiter = '.' ; private static final String DecodedSummaryFileExtension = ".txt" ; private final String summaryFileName; private final ByteDecoder decoder; public SbeLogDecoder( final String summaryFileName) { this .summaryFileName = summaryFileName; final MessageSchema messageSchema = Utils.loadTemplate(templateFile); decoder = new ByteDecoderFactory().create(messageSchema); } public void run() { final String decodedSummaryFileName = summaryFileName. substring( 0 , summaryFileName.indexOf(FileExtensionDelimiter)) + DecodedSummaryFileExtension; int messageCounter = 0 ; try { final File file = new File(summaryFileName, "rw" ); final File outputFile = new File(decodedSummaryFileName, "rw" ); String line; while (!( "" ).equals(line = file.getLine( '\n' ))) { final String rawMsg = line.substring(line.lastIndexOf( ' ' ) + 1 ) .replace( "\n" , "" ).replace( "\r" , "" ); final byte [] rawMsgBytes = Base64.getDecoder().decode(rawMsg); try { final IMessage msg = decoder.decodePreCreatedMessage(rawMsgBytes, 0 , rawMsgBytes.length); outputFile.write(line.substring( 0 , 26 )); outputFile.write(msg.toString()); outputFile.write(System.lineSeparator()); ++messageCounter; } catch ( final Exception e) { LOG.warn( "Failed to parse message: " + rawMsg + " : " + e.getMessage()); } } } catch ( final IOException e) { throw new RuntimeException( "Failed to open file: " + e.getMessage()); } finally { LOG.info( "{} messages are decoded successfully and written into {} file." , messageCounter, decodedSummaryFileName); } } public static void main( final String[] args) { LOG.info( "CME iLink 3 SbeLogDecoder Sample." ); String summaryFileName = "" ; if (args.length >= 1 ) { summaryFileName = args[ 0 ]; } if (summaryFileName.isEmpty()) { LOG.error( "Usage: SbeLogDecoder [SummaryFileName]" ); return ; } try { LOG.info( "Decoding {} log file..." , summaryFileName); final SbeLogDecoder sbeLogDecoder = new SbeLogDecoder(summaryFileName); sbeLogDecoder.run(); } catch ( final Exception e) { LOG.error( "EXCEPTION: {}" , e.getMessage()); e.printStackTrace(); } } } |