Skip to content

Instantly share code, notes, and snippets.

@omernaci
Created December 13, 2023 10:22
Show Gist options
  • Save omernaci/96d33f1df300166241f0f0710e9e6d16 to your computer and use it in GitHub Desktop.
Save omernaci/96d33f1df300166241f0f0710e9e6d16 to your computer and use it in GitHub Desktop.
Event Driven Architecture - Event Versioning
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