-
-
Save anonymous/98b16029b552f6d29282 to your computer and use it in GitHub Desktop.
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 UserLockSaga extends AbstractAnnotatedSaga { | |
private static final long serialVersionUID = 1L; | |
public static final Long UNLOCK_AFTER_MILLIS = new Long(DateTimeConstants.MILLIS_PER_HOUR); | |
@Autowired @Setter private transient CommandGateway commandGateway; | |
@Autowired @Setter private transient EventScheduler eventScheduler; | |
@Autowired @Setter private transient UserQueryRepository userQueryRepository; | |
private ScheduleToken userUnlockToken; | |
@StartSaga | |
@SagaEventHandler(associationProperty = "userId") | |
public void handle(UserLockedEvent event, @Timestamp DateTime eventTime) { | |
if (userUnlockToken != null) { | |
eventScheduler.cancelSchedule(userUnlockToken); | |
} | |
this.userUnlockToken = eventScheduler.schedule(getUnlockTime(eventTime), new UserUnlockSagaEvent(event.getUserId())); | |
} | |
@SagaEventHandler(associationProperty = "userId") | |
public void handle(final UserUnlockSagaEvent unlockSagaEvent) { | |
commandGateway.sendAndWait(new UserUnlockCommand(unlockSagaEvent.getUserId())); | |
end(); | |
} | |
private AxonUserReference getAxonUserReference(UUID userId) { | |
UserDto user = userQueryRepository.findOne(userId); | |
return new AxonUserReference(userId, user.getUsername()); | |
} | |
private DateTime getUnlockTime(DateTime eventTime) { | |
return new DateTime(eventTime.getMillis() + UNLOCK_AFTER_MILLIS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment