如何在QuickFix中创建Socket Initiator实例时避免使用MessageStoreFactory
Hey there! Great question—yes, you absolutely can skip creating a persistent MessageStoreFactory when setting up a QuickFix Socket Initiator, and there are two practical approaches depending on your use case. Let’s break them down:
1. Implement a "No-Op" Null Message Store (For Zero State Persistence)
If your use case doesn’t require retaining session state (like sequence numbers) between restarts—say, for testing, one-off sessions, or if you’re managing sequence numbers entirely on your own—you can create a custom MessageStore and MessageStoreFactory that do nothing.
Here’s a Java example of the no-op store:
public class NullMessageStore implements MessageStore { // All methods are empty or return default values @Override public void setNextSenderSeqNum(int next) {} @Override public void setNextTargetSeqNum(int next) {} @Override public int getNextSenderSeqNum() { return 1; } @Override public int getNextTargetSeqNum() { return 1; } @Override public void incrementNextSenderSeqNum() {} @Override public void incrementNextTargetSeqNum() {} @Override public void reset() {} @Override public void refresh() {} @Override public void persist(Message message) {} @Override public void persist(int seqNum, String msg) {} @Override public boolean set(int seqNum, String msg) { return true; } @Override public String get(int seqNum) { return null; } @Override public void delete(int seqNum) {} @Override public void start() {} @Override public void stop() {} @Override public void destroy() {} }
Then the corresponding factory:
public class NullMessageStoreFactory implements MessageStoreFactory { @Override public MessageStore create(SessionID sessionID) { return new NullMessageStore(); } }
Use it when initializing your Socket Initiator:
SocketInitiator initiator = new SocketInitiator( yourApplicationInstance, new NullMessageStoreFactory(), // No persistent storage here yourSessionSettings, new DefaultLogFactory(yourSessionSettings), new DefaultMessageFactory() );
This setup will always start sequence numbers at 1 on every restart and won’t save any message history.
2. Use QuickFix’s Built-In Memory Store (For Temporary In-Memory State)
If you need to retain session state during the runtime of your application (but don’t want to persist it to disk), QuickFix provides a MemoryStoreFactory in most implementations. This stores sequence numbers and message history in RAM—no file system required.
Here’s how to use it:
SocketInitiator initiator = new SocketInitiator( yourApplicationInstance, new MemoryStoreFactory(), // In-memory only, no disk I/O yourSessionSettings, new DefaultLogFactory(yourSessionSettings), new DefaultMessageFactory() );
Unlike the null store, this will keep track of sequence numbers while your application is running. The state is lost once the app restarts, though.
Important Caveats
- Don’t use these in production if you need message durability: If your application crashes or restarts, the null store will reset sequence numbers to 1, which can lead to message rejection or duplicate processing by the counterparty.
- Check your QuickFix version: Some older versions might not have
MemoryStoreFactorybuilt-in, so you’ll need to fall back to the custom null store.
内容的提问来源于stack exchange,提问作者sr123

