Index: src/test/java/quickfix/JdbcLogTest.java =================================================================== --- src/test/java/quickfix/JdbcLogTest.java (revision 576) +++ src/test/java/quickfix/JdbcLogTest.java (working copy) @@ -27,22 +27,31 @@ import junit.framework.TestCase; public class JdbcLogTest extends TestCase { - protected void tearDown() throws Exception { - JdbcTestSupport.assertNoActiveConnections(); - super.tearDown(); - } + private JdbcLog log; + private Connection connection; + private SessionID sessionID; - public void testLog() throws Exception { - Connection connection = JdbcTestSupport.getConnection(); + protected void setUp() throws Exception { + super.setUp(); + connection = JdbcTestSupport.getConnection(); SessionSettings settings = new SessionSettings(); JdbcTestSupport.setHypersonicSettings(settings); initializeTableDefinitions(connection); JdbcLogFactory logFactory = new JdbcLogFactory(settings); long now = System.currentTimeMillis(); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER-" + now, "TARGET-" + now); + sessionID = new SessionID("FIX.4.2", "SENDER-" + now, "TARGET-" + now); settings.setString(sessionID, "ConnectionType", "acceptor"); - JdbcLog log = (JdbcLog) logFactory.create(sessionID); + log = (JdbcLog) logFactory.create(sessionID); + assertEquals(0, getRowCount(connection, "messages_log")); + } + protected void tearDown() throws Exception { + JdbcTestSupport.assertNoActiveConnections(); + log.clear(); + super.tearDown(); + } + + public void testLog() throws Exception { assertEquals(0, getRowCount(connection, "messages_log")); log.onIncoming("INCOMING"); assertEquals(1, getRowCount(connection, "messages_log")); @@ -63,6 +72,59 @@ assertEquals(0, getRowCount(connection, "event_log")); } + public void testHearbeats_basic() throws Exception + { + log.setSkipHeartbeats(true); + assertFalse("fails when should always skip heartbeats", log.skipHeartbeats("bob")); + assertTrue("fails when should always skip heartbeats", log.skipHeartbeats("35=0\001INCOMING\001")); + + log.setSkipHeartbeats(false); + assertFalse("fails when should always not skip heartbeats", log.skipHeartbeats("bob")); + assertFalse("fails when should always not skip heartbeats", log.skipHeartbeats("35=0\001INCOMING\001")); + } + + /** First don't ignore heartbeats, then do */ + public void testHeartbeats_incoming() throws Exception { + assertEquals(0, getRowCount(connection, "messages_log")); + log.onIncoming("INCOMING"); + assertEquals(1, getRowCount(connection, "messages_log")); + // send a heartbeat + log.onIncoming("35=0\001INCOMING\001"); + assertEquals("didn't log a heartbeat as expected", 2, getRowCount(connection, "messages_log")); + + log.onIncoming("8=FIX.4.2\0019=67\00135=0\00134=5\00149=MRKTC-DEMO-OMS\00152=20070206-02:23:40.122\00156=MRKTC-EXCH\00110=145"); + assertEquals("didn't log a heartbeat as expected", 3, getRowCount(connection, "messages_log")); + + log.clear(); + assertEquals(0, getRowCount(connection, "messages_log")); + log.setSkipHeartbeats(true); + log.onIncoming("35=0\001INCOMING\001"); + assertEquals("logged a heartbeat when not expected", 0, getRowCount(connection, "messages_log")); + log.onIncoming("8=FIX.4.2\0019=67\00135=0\00134=5\00149=MRKTC-DEMO-OMS\00152=20070206-02:23:40.122\00156=MRKTC-EXCH\00110=145"); + assertEquals("logged a heartbeat when not expected", 0, getRowCount(connection, "messages_log")); + } + + /** First don't ignore heartbeats, then do */ + public void testHeartbeats_outgoing() throws Exception { + assertEquals(0, getRowCount(connection, "messages_log")); + log.onOutgoing("INCOMING"); + assertEquals(1, getRowCount(connection, "messages_log")); + // send a heartbeat + log.onOutgoing("35=0\001INCOMING\001"); + assertEquals("didn't log a heartbeat as expected", 2, getRowCount(connection, "messages_log")); + + log.onOutgoing("8=FIX.4.2\0019=67\00135=0\00134=5\00149=MRKTC-DEMO-OMS\00152=20070206-02:23:40.122\00156=MRKTC-EXCH\00110=145"); + assertEquals("didn't log a heartbeat as expected", 3, getRowCount(connection, "messages_log")); + + log.clear(); + assertEquals(0, getRowCount(connection, "messages_log")); + log.setSkipHeartbeats(true); + log.onOutgoing("35=0\001INCOMING\001"); + assertEquals("logged a heartbeat when not expected", 0, getRowCount(connection, "messages_log")); + log.onOutgoing("8=FIX.4.2\0019=67\00135=0\00134=5\00149=MRKTC-DEMO-OMS\00152=20070206-02:23:40.122\00156=MRKTC-EXCH\00110=145"); + assertEquals("logged a heartbeat when not expected", 0, getRowCount(connection, "messages_log")); + } + private void assertLogData(Connection connection, int rowOffset, SessionID sessionID, String text, String tableName) throws SQLException { Statement s = connection.createStatement(); Index: src/test/java/quickfix/SessionFactoryTestSupport.java =================================================================== --- src/test/java/quickfix/SessionFactoryTestSupport.java (revision 576) +++ src/test/java/quickfix/SessionFactoryTestSupport.java (working copy) @@ -12,8 +12,10 @@ } public static Session createSession(SessionID sessionID, Application application, boolean isInitiator) { - return new Session(application, new MemoryStoreFactory(), sessionID, null, null, + Session session = new Session(application, new MemoryStoreFactory(), sessionID, null, null, new ScreenLogFactory(true, true, true), new DefaultMessageFactory(), isInitiator ? 30 : 0); + Session.registerSession(session); + return session; } public static Session createSession() throws ConfigError { Index: src/main/java/quickfix/LogBase.java =================================================================== --- src/main/java/quickfix/LogBase.java (revision 0) +++ src/main/java/quickfix/LogBase.java (revision 0) @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) quickfixengine.org All rights reserved. + * + * This file is part of the QuickFIX FIX Engine + * + * This file may be distributed under the terms of the quickfixengine.org + * license as defined by quickfixengine.org and appearing in the file + * LICENSE included in the packaging of this file. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING + * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. + * + * See http://www.quickfixengine.org/LICENSE for licensing information. + * + * Contact ask@quickfixengine.org if any conditions of this licensing + * are not clear to you. + * + * @version $Id$ + */ + +package quickfix; + +import quickfix.field.MsgType; + +/** + * Common abastract superclass for all the loggers. + * Contains logic on whether or not to log heartbeats + */ + +public abstract class LogBase implements Log { + + protected boolean skipHeartbeats = false; + + + public void setSkipHeartbeats(boolean heartbeats) { + this.skipHeartbeats = heartbeats; + } + + public boolean skipHeartbeats(String inMessage) + { + try { + return (skipHeartbeats && MsgType.HEARTBEAT.equals(MessageUtils.getMessageType(inMessage))); + } catch (InvalidMessage e) { + return false; + } + + } +} Index: src/main/java/quickfix/JdbcLogFactory.java =================================================================== --- src/main/java/quickfix/JdbcLogFactory.java (revision 576) +++ src/main/java/quickfix/JdbcLogFactory.java (working copy) @@ -24,6 +24,7 @@ */ public class JdbcLogFactory implements LogFactory { private SessionSettings settings; + private boolean skipHeartbeats = false; /** * Create a JDBC logger. @@ -32,7 +33,9 @@ */ public Log create(SessionID sessionID) { try { - return new JdbcLog(settings, sessionID); + JdbcLog log = new JdbcLog(settings, sessionID); + log.setSkipHeartbeats(skipHeartbeats); + return log; } catch (Exception e) { throw new RuntimeError(e); } @@ -44,8 +47,13 @@ public JdbcLogFactory(SessionSettings settings) { this.settings = settings; } - - /** + + + public void setSkipHeartbeats(boolean skipHeartbeats) { + this.skipHeartbeats = skipHeartbeats; + } + + /** * Used to support the MySQL-specific class (JNI compatibility) * * @return the session settings Index: src/main/java/quickfix/ScreenLog.java =================================================================== --- src/main/java/quickfix/ScreenLog.java (revision 576) +++ src/main/java/quickfix/ScreenLog.java (working copy) @@ -8,12 +8,12 @@ * LICENSE included in the packaging of this file. * * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING - * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE. - * - * See http://www.quickfixengine.org/LICENSE for licensing information. - * - * Contact ask@quickfixengine.org if any conditions of this licensing + * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. + * + * See http://www.quickfixengine.org/LICENSE for licensing information. + * + * Contact ask@quickfixengine.org if any conditions of this licensing * are not clear to you. ******************************************************************************/ @@ -21,7 +21,6 @@ import java.io.PrintStream; -import quickfix.field.MsgType; import quickfix.field.converter.UtcTimestampConverter; /** @@ -31,7 +30,7 @@ * * @see quickfix.ScreenLogFactory */ -public class ScreenLog implements Log { +public class ScreenLog extends LogBase { private static final String EVENT_CATEGORY = "event"; private static final String OUTGOING_CATEGORY = "outgoing"; private static final String INCOMING_CATEGORY = "incoming"; @@ -40,7 +39,6 @@ private final boolean incoming; private final boolean outgoing; private final boolean events; - private final boolean heartBeats; ScreenLog(boolean incoming, boolean outgoing, boolean events, boolean heartBeats, SessionID sessionID, PrintStream out) { @@ -48,7 +46,7 @@ this.incoming = incoming; this.outgoing = outgoing; this.events = events; - this.heartBeats = heartBeats; + setSkipHeartbeats(!heartBeats); this.sessionID = sessionID; } @@ -65,14 +63,9 @@ } private void logMessage(String message, String type) { - try { - if (!heartBeats && MsgType.HEARTBEAT.equals(MessageUtils.getMessageType(message))) { - return; - } - } catch (InvalidMessage e) { - // ignore + if(!skipHeartbeats(message)) { + log(message, type); } - log(message, type); } public void onEvent(String message) { Index: src/main/java/quickfix/JdbcLog.java =================================================================== --- src/main/java/quickfix/JdbcLog.java (revision 576) +++ src/main/java/quickfix/JdbcLog.java (working copy) @@ -26,7 +26,7 @@ import javax.sql.DataSource; -class JdbcLog implements quickfix.Log { +class JdbcLog extends LogBase { private static final String MESSAGES_LOG_TABLE = "messages_log"; private static final String EVENT_LOG_TABLE = "event_log"; private final SessionID sessionID; @@ -44,11 +44,15 @@ } public void onIncoming(String value) { - insert(MESSAGES_LOG_TABLE, value); + if(!skipHeartbeats(value)) { + insert(MESSAGES_LOG_TABLE, value); + } } public void onOutgoing(String value) { - insert(MESSAGES_LOG_TABLE, value); + if(!skipHeartbeats(value)) { + insert(MESSAGES_LOG_TABLE, value); + } } private void insert(String tableName, String value) {