Skip to content

Instantly share code, notes, and snippets.

View ndemengel's full-sized avatar

Nicolas Grisey Demengel ndemengel

View GitHub Profile
@ndemengel
ndemengel / PushTransactionForInvoice.kt
Last active April 26, 2020 10:49
Commanq queue: a command and its companion specification class
@Component
class PushTransactionForInvoice(
private val transactionPushService: TransactionPushService
) : Command(PushTransactionForInvoiceSpec::class) {
override fun execute(arguments: Map<String, Any?>): CommandExecutionResult {
val invoiceId = InvoiceId(arguments["invoiceId"] as String)
val transactionId = TransactionId(arguments["transactionId"] as String)
transactionPushService.pushTransactionForInvoice(transactionId, invoiceId)
@ndemengel
ndemengel / 1 EventsDocGeneratorTest.java
Last active April 30, 2017 18:13
Using QDox to generate a documentation of our Spring/RabbitMQ setup
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.*;
import org.junit.Test;
import java.io.*;
import java.text.MessageFormat;
import java.util.*;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
@ndemengel
ndemengel / SomeTest.java
Last active April 30, 2017 18:42
Sample use of JvmSynchronizedEventsService
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
// only populate the context with the classes that interest us for this test
SomePublisher.class,
JvmSynchronizedEventsService.class,
SomeConsumer.class
@Value
public class EmailUpdated implements Event {
public static final String EXCHANGE_NAME = "email.updated.exchange";
String accountId;
String oldEmail;
String newEmail;
}
@ndemengel
ndemengel / 01 EmailUpdated.java
Last active July 18, 2017 05:48
RabbitMQ/Spring migration - Result
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";
@ndemengel
ndemengel / 1 MailChimpSynchronization.java
Last active April 30, 2017 19:00
RabbitMQ/Spring migration - Step 3: extraction of the exchange name from the event
// the exchange is not mentioned anymore here
@RabbitEventListener(queue = "email.updated.mailchimp.queue")
public void emailUpdated(EmailUpdated event) {
updateMailChimpMember(event.getOldEmail(), event.getNewEmail());
}
@ndemengel
ndemengel / 1 MailChimpSynchronization.java
Last active April 30, 2017 18:59
RabbitMQ/Spring migration - Step 2: let's craft our own annotation
// let's use our own annotation!
@RabbitEventListener(queue = "email.updated.mailchimp.queue", exchange = RabbitConfig.EMAIL_UPDATED_EXCHANGE)
public void emailUpdated(EmailUpdated event) {
updateMailChimpMember(event.getOldEmail(), event.getNewEmail());
}
@ndemengel
ndemengel / RabbitConfig.java
Last active April 2, 2017 15:23
RabbitMQ/Spring migration - Step 1: Less boilerplate
@PostConstruct
public void createExchangesAndQueuesAndBindings() {
// only one line left for each exchange/queues association!
createAndBindQueuesToFanoutExchangeAndDeadLetter(EMAIL_UPDATED_EXCHANGE, MAILCHIMP_EMAIL_UPDATED_QUEUE);
// ... other associations...
}
private void createAndBindQueuesToFanoutExchangeAndDeadLetter(String exchangeName, String... queueNames) {
FanoutExchange exchangeBean = createExchangeBean(exchangeName);
@ndemengel
ndemengel / 1 EmailUpdated.java
Last active April 30, 2017 18:14
Spring/Rabbit boilerplate
import lombok.Data;
@Data
public final class EmailUpdated {
private String accountId;
private String oldEmail;
private String newEmail;
public EmailUpdated(String accountId, String oldEmail, String newEmail) {
this.accountId = accountId;
import lombok.*;
// The aim here is to have a type that looks like a value type, while still
// allowing Jackson to unmarshal our type from JSON.
// See https://gist.github.com/ndemengel/77a8ffbb929bc55e3d87809820c1c5a4 for a more elegant solution.
@AllArgsConstructor @Getter
@EqualsAndHashCode @ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EmailUpdated implements Event<EmailUpdated> {