Skip to content

Keep unsent messages when the session is reset by its schedule - #1345

Open
aymenjamaaoui wants to merge 6 commits into
quickfix-j:masterfrom
aymenjamaaoui:bug/hold-messages-outside-session-time-463-986
Open

aymenjamaaoui wants to merge 6 commits into
quickfix-j:masterfrom
aymenjamaaoui:bug/hold-messages-outside-session-time-463-986

Conversation

@aymenjamaaoui

Copy link
Copy Markdown
Contributor

Problem

When the app sends a message while the session is not logged on (for example outside the session time), the message is saved in the message store. Then the session is reset by its schedule, and the reset deletes the store. So the message is never sent.

This does not match the Javadoc of Application.onCreate(): "If no one is logged on, the messages will be sent at the time a connection is established with the counterparty."

Closes #986
Refs #463

Solution

This PR adds a new session setting: KeepUnsentMessagesOnReset (default N).

With KeepUnsentMessagesOnReset=Y:

  • When a message is saved but not sent, the store keeps the sequence number of the first unsent message.
  • When the session is reset by its schedule, the unsent application messages are copied into the new store, with new sequence numbers.
  • After the next logon, they are sent again with PossDupFlag=Y when the counterparty asks for them (ResendRequest).
  • The value is cleared after a successful logon.

The value is saved in the store, so it works after a restart.

As discussed in #986:

  • MessageStore has two new default methods: getFirstUnsentSenderMsgSeqNum() and setFirstUnsentSenderMsgSeqNum(int). By default they do nothing (get returns -1), so custom stores still compile and work as before.
  • The value is saved in a separate file or table:
    • MemoryStore: in memory.
    • FileStore and CachedFileStore: new file <session>.firstunsentseqnum.
    • JdbcStore: new table unsent_seqnums. The name can be changed with JdbcStoreUnsentSeqNumsTableName. The table is optional. Without it, JdbcStore works as before.
  • The SQL scripts for HSQLDB, MySQL, MSSQL, Oracle and PostgreSQL create the new table.

If the store does not support it, or if PersistMessages=N, the session logs a warning and works as before.

Messages are still lost with ResetOnLogon, ResetOnLogout, ResetOnDisconnect, a Logon with ResetSeqNumFlag=Y, or a call to Session.reset(). These resets are asked by the user or by the counterparty, so this PR does not change them.

Open question

The default is N, so nothing changes for current users. In #986 we did not decide if it should be Y by default. I am happy to change it.

Tests

  • SessionTest: unsent messages are kept after a scheduled reset, resent after logon, and kept after a restart; messages that were already sent are not kept; no change when the setting is off.
  • DefaultSessionFactoryTest: the new setting is read.
  • AbstractMessageStoreTest: get, set, refresh and reset of the new value. It runs for MemoryStore, FileStore, CachedFileStore and JdbcStore (also the legacy table layout).
  • JdbcStoreTest: JdbcStore works without the new table.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.68323% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.08%. Comparing base (72649cc) to head (b34a856).

Files with missing lines Patch % Lines
quickfixj-core/src/main/java/quickfix/Session.java 84.50% 6 Missing and 5 partials ⚠️
...ickfixj-core/src/main/java/quickfix/JdbcStore.java 96.07% 2 Missing ⚠️
...uickfixj-core/src/main/java/quickfix/JdbcUtil.java 80.00% 0 Missing and 1 partial ⚠️
...fixj-core/src/main/java/quickfix/MessageStore.java 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1345      +/-   ##
============================================
+ Coverage     71.73%   72.08%   +0.35%     
- Complexity     2306     2359      +53     
============================================
  Files           162      163       +1     
  Lines          9106     9264     +158     
  Branches       1193     1215      +22     
============================================
+ Hits           6532     6678     +146     
- Misses         2120     2127       +7     
- Partials        454      459       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aymenjamaaoui
aymenjamaaoui force-pushed the bug/hold-messages-outside-session-time-463-986 branch from aa3486c to d41b8ac Compare October 31, 2026 20:10
@aymenjamaaoui

Copy link
Copy Markdown
Contributor Author

I rebased the branch on the latest master. There was no conflict.

#1346 (fix for #327) is now in master. With this change, Session.reset() calls ApplicationExtended.onBeforeSessionReset() even when the session is not logged on.

With KeepUnsentMessagesOnReset=Y, the scheduled reset of a session that is not logged on does not use Session.reset(). So the callback was not called in this case, and #327 came back for users of the new option.

I added a commit to fix it: the callback is now also called in this case, before the message store is reset. I also added a test for it.

@chrjohn

chrjohn commented Nov 18, 2026

Copy link
Copy Markdown
Member

Sorry, am a little busy at the moment, but will review files one by one... thanks for your patience...

Comment on lines +2755 to 2762
if (keepUnsentMessagesOnReset && num == 0 && !isLoggedOn()) {
markFirstUnsentMessage(header.getInt(MsgSeqNum.FIELD));
}
messageString = message.toString();
persist(message.getHeader(), messageString, num);
if (isLoggedOn()) {
result = send(messageString);
}

@chrjohn chrjohn Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the result of send() should also be taken into account. If it returns false, the message was also not sent and should go through markFirstUnsentMessage()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. If send() returns false, the message is in the store but was not sent. I changed the code: the message is now marked as unsent in this case too, and I added a test (b34a856).

One limit: we only keep the first unsent sequence number. With SynchronousWrites=Y, send() can return false after a timeout while the session stays logged on. If later messages are sent and then the session is reset by its schedule, these later messages are kept too, so they can be sent two times. Also, with async writes (the default), send() returns true even if the write fails later. Is this OK for you?

@@ -2731,8 +2752,11 @@
} catch (final Throwable t) {

@chrjohn chrjohn Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing DoNotSend also means a message was not sent. See other comment below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When toApp() throws DoNotSend, sendRaw() returns before persist(). So the message is not in the store and the sequence number is not used. I think there is nothing to keep here, because the application decided not to send it. Or do you mean another case?

aymenjamaaoui and others added 6 commits November 22, 2026 13:17
Before this change, some messages were lost. This happened when the
app sent a message while the session was not logged on, for example
outside the session time. The message was saved in the store, and then
the scheduled reset deleted it.

With KeepUnsentMessagesOnReset=Y, the message store keeps the sequence
number of the first message that was not sent. When the session is
reset by its schedule, the unsent application messages are copied to
the new store with new sequence numbers. After the next logon, they
are sent again when the counterparty asks for them.

The value is saved in the store, so it works after a restart. It is
supported by MemoryStore, FileStore and CachedFileStore.

The default is N. So the reset works the same as before.

Refs quickfix-j#463, quickfix-j#986
JdbcStore can now keep the first unsent sequence number. The value is
saved in a new table, "unsent_seqnums". The name can be changed with
JdbcStoreUnsentSeqNumsTableName.

The table is optional. Without it, JdbcStore works as before and
KeepUnsentMessagesOnReset has no effect.

The SQL scripts for HSQLDB, MySQL, MSSQL, Oracle and PostgreSQL create
the new table.

Refs quickfix-j#463, quickfix-j#986
Add tests for several unsent messages, a scheduled reset without unsent
messages, the warnings when the setting has no effect, and the update
and custom table name in JdbcStore.
@aymenjamaaoui
aymenjamaaoui force-pushed the bug/hold-messages-outside-session-time-463-986 branch from d41b8ac to b34a856 Compare November 22, 2026 12:20

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Messages are dropped when sent after EndTime or before StartTime

2 participants