Last active
April 24, 2017 12:13
-
-
Save ndemengel/77a8ffbb929bc55e3d87809820c1c5a4 to your computer and use it in GitHub Desktop.
Sample event used with our Spring Boot/RabbitMQ solution
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
import lombok.Value; | |
// This code only works if compiling the code using javac's -parameters option (Java 8 only). | |
// With this option, Jackons can use the all-args constructor of our type. | |
// See https://gist.github.com/ndemengel/72f362bc31afe5fcaa0499af8f269651 for a more general solution. | |
@Value | |
public class EmailUpdated implements Event { | |
public static final String EXCHANGE_NAME = "email.updated.exchange"; | |
String accountId; | |
String oldEmail; | |
String newEmail; | |
} |
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
@RequestMapping(path = "/some/path", method = POST) | |
public ModelAndView updateEmail(@Valid NewEmailForm form) { | |
// ... | |
// call some account-handling service which ultimately does the following: | |
eventsService.fire(new EmailUpdated(accountId, oldEmail(), newEmail)); | |
// ... | |
} |
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
@RabbitEventListener(queue = "email.updated.mailchimp.queue") | |
public void emailUpdated(EmailUpdated event) { | |
updateMailChimpMember(event.getOldEmail(), event.getNewEmail()); | |
} |
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
// just a marker interface | |
public interface Event {} |
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
// just a single fire method defined once and for all, no more overloads | |
public void fire(Event event) { | |
String exchangeName = obtainTheExchangeNameInSomeWay(event); | |
rabbitTemplate.convertAndSend(exchangeName, event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment