Created
December 13, 2023 10:22
-
-
Save omernaci/96d33f1df300166241f0f0710e9e6d16 to your computer and use it in GitHub Desktop.
Event Driven Architecture - Event Versioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountOpenedEvent { | |
private String accountId; | |
private String customerDetails; | |
private String accountOpeningDetails; | |
private int version; // Add a version field | |
} | |
@Component | |
public class AccountOpenedEventHandler { | |
private List<AccountOpenedEventProcessor> processors; | |
public AccountOpenedEventHandler() { | |
processors = new ArrayList<>(); | |
processors.add(new AccountOpenedEventProcessorV1()); | |
processors.add(new AccountOpenedEventProcessorV2()); | |
} | |
@StreamListener(target = "accountOpenedInput") | |
public void handleAccountOpenedEvent(AccountOpenedEvent event) { | |
for (AccountOpenedEventProcessor processor : processors) { | |
if (processor.supports(event)) { | |
processor.process(event); | |
return; | |
} | |
} | |
handleUnsupportedVersion(event); | |
} | |
public void handleUnsupportedVersion(AccountOpenedEvent event) { | |
// Handle unsupported version or fallback logic | |
} | |
} | |
interface AccountOpenedEventProcessor { | |
boolean supports(AccountOpenedEvent event); | |
void process(AccountOpenedEvent event); | |
} | |
class AccountOpenedEventProcessorV1 implements AccountOpenedEventProcessor { | |
@Override | |
public boolean supports(AccountOpenedEvent event) { | |
return event.getVersion() == 1; | |
} | |
@Override | |
public void process(AccountOpenedEvent event) { | |
// Handle version 1 logic for AccountOpened event | |
} | |
} | |
class AccountOpenedEventProcessorV2 implements AccountOpenedEventProcessor { | |
@Override | |
public boolean supports(AccountOpenedEvent event) { | |
return event.getVersion() == 2; | |
} | |
@Override | |
public void process(AccountOpenedEvent event) { | |
// Handle version 2 logic for AccountOpened event | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment