Skip to content

Instantly share code, notes, and snippets.

@ndemengel
Last active April 24, 2017 12:13
Show Gist options
  • Save ndemengel/77a8ffbb929bc55e3d87809820c1c5a4 to your computer and use it in GitHub Desktop.
Save ndemengel/77a8ffbb929bc55e3d87809820c1c5a4 to your computer and use it in GitHub Desktop.
Sample event used with our Spring Boot/RabbitMQ solution
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;
}
@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));
// ...
}
@RabbitEventListener(queue = "email.updated.mailchimp.queue")
public void emailUpdated(EmailUpdated event) {
updateMailChimpMember(event.getOldEmail(), event.getNewEmail());
}
// just a marker interface
public interface Event {}
// 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