-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
386a751
commit 7e6a0cb
Showing
20 changed files
with
259 additions
and
174 deletions.
There are no files selected for viewing
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
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
19 changes: 0 additions & 19 deletions
19
...onoutbox-spring/src/test/java/com/gruelbox/transactionoutbox/spring/acceptance/Event.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...spring/src/test/java/com/gruelbox/transactionoutbox/spring/acceptance/EventPublisher.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...pring/src/test/java/com/gruelbox/transactionoutbox/spring/acceptance/EventRepository.java
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
...java/com/gruelbox/transactionoutbox/spring/acceptance/EventuallyConsistentController.java
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
.../com/gruelbox/transactionoutbox/spring/acceptance/EventuallyConsistentControllerTest.java
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
...rc/test/java/com/gruelbox/transactionoutbox/spring/acceptance/ExternalsConfiguration.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
.../gruelbox/transactionoutbox/spring/acceptance/TransactionOutboxSpringDemoApplication.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...ionoutbox/spring/acceptance/Customer.java → ...actionoutbox/spring/example/Customer.java
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
2 changes: 1 addition & 1 deletion
2
...spring/acceptance/CustomerRepository.java → ...ox/spring/example/CustomerRepository.java
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
37 changes: 37 additions & 0 deletions
37
...st/java/com/gruelbox/transactionoutbox/spring/example/EventuallyConsistentController.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.gruelbox.transactionoutbox.spring.example; | ||
|
||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
import com.gruelbox.transactionoutbox.TransactionOutbox; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@SuppressWarnings("unused") | ||
@RestController | ||
class EventuallyConsistentController { | ||
|
||
private static final Logger LOGGER = | ||
LoggerFactory.getLogger(EventuallyConsistentController.class); | ||
|
||
@Autowired private CustomerRepository customerRepository; | ||
@Autowired private TransactionOutbox outbox; | ||
|
||
@SuppressWarnings("SameReturnValue") | ||
@PostMapping(path = "/customer") | ||
@Transactional | ||
public void createCustomer(@RequestBody Customer customer) { | ||
customerRepository.save(customer); | ||
outbox.schedule(ExternalQueueService.class).sendCustomerCreatedEvent(customer); | ||
} | ||
|
||
@GetMapping("/customer/{id}") | ||
public Customer getCustomer(@PathVariable long id) { | ||
return customerRepository | ||
.findById(id) | ||
.orElseThrow(() -> new ResponseStatusException(NOT_FOUND)); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ava/com/gruelbox/transactionoutbox/spring/example/EventuallyConsistentControllerTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.gruelbox.transactionoutbox.spring.example; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URL; | ||
import javax.inject.Inject; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class EventuallyConsistentControllerTest { | ||
|
||
@SuppressWarnings("unused") | ||
@LocalServerPort | ||
private int port; | ||
|
||
private URL base; | ||
|
||
@SuppressWarnings("unused") | ||
@Inject | ||
private TestRestTemplate template; | ||
|
||
@Inject private ExternalQueueService externalQueueService; | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
this.base = new URL("http://localhost:" + port + "/"); | ||
} | ||
|
||
@Test | ||
void testCheck() throws Exception { | ||
|
||
var joe = new Customer(1L, "Joe", "Strummer"); | ||
var dave = new Customer(2L, "Dave", "Grohl"); | ||
var neil = new Customer(3L, "Neil", "Diamond"); | ||
var tupac = new Customer(4L, "Tupac", "Shakur"); | ||
var jeff = new Customer(5L, "Jeff", "Mills"); | ||
var customer = base.toString() + "/customer"; | ||
|
||
assertTrue(template.postForEntity(customer, joe, Void.class).getStatusCode().is2xxSuccessful()); | ||
assertTrue( | ||
template.postForEntity(customer, dave, Void.class).getStatusCode().is2xxSuccessful()); | ||
assertTrue( | ||
template.postForEntity(customer, neil, Void.class).getStatusCode().is2xxSuccessful()); | ||
assertTrue( | ||
template.postForEntity(customer, tupac, Void.class).getStatusCode().is2xxSuccessful()); | ||
assertTrue( | ||
template.postForEntity(customer, jeff, Void.class).getStatusCode().is2xxSuccessful()); | ||
|
||
await() | ||
.atMost(10, SECONDS) | ||
.pollDelay(1, SECONDS) | ||
.untilAsserted( | ||
() -> | ||
assertThat(externalQueueService.getSent()) | ||
.containsExactlyInAnyOrder(joe, dave, neil, tupac, jeff)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ing/src/test/java/com/gruelbox/transactionoutbox/spring/example/ExternalQueueService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.gruelbox.transactionoutbox.spring.example; | ||
|
||
import lombok.Getter; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
@Getter | ||
@Service | ||
class ExternalQueueService { | ||
|
||
private final Set<Long> attempted = new HashSet<>(); | ||
private final List<Customer> sent = new CopyOnWriteArrayList<>(); | ||
|
||
void sendCustomerCreatedEvent(Customer customer) { | ||
if (attempted.add(customer.getId())) { | ||
throw new RuntimeException("Temporary failure, try again"); | ||
} | ||
sent.add(customer); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...g/src/test/java/com/gruelbox/transactionoutbox/spring/example/ExternalsConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.gruelbox.transactionoutbox.spring.example; | ||
|
||
import com.gruelbox.transactionoutbox.spring.SpringInstantiator; | ||
import com.gruelbox.transactionoutbox.spring.SpringTransactionManager; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@Configuration | ||
@Import({SpringInstantiator.class, SpringTransactionManager.class}) | ||
class ExternalsConfiguration {} |
31 changes: 31 additions & 0 deletions
31
...a/com/gruelbox/transactionoutbox/spring/example/TransactionOutboxBackgroundProcessor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.gruelbox.transactionoutbox.spring.example; | ||
|
||
import com.gruelbox.transactionoutbox.TransactionOutbox; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Simple implementation of a background processor for {@link TransactionOutbox}. You don't need to | ||
* use this if you need different semantics, but this is a good start for most purposes. | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor(onConstructor_ = {@Autowired}) | ||
class TransactionOutboxBackgroundProcessor { | ||
|
||
private final TransactionOutbox outbox; | ||
|
||
@Scheduled(fixedRateString = "${outbox.repeatEvery}") | ||
void poll() { | ||
try { | ||
do { | ||
log.info("Flushing"); | ||
} while (outbox.flush()); | ||
} catch (Exception e) { | ||
log.error("Error flushing transaction outbox. Pausing", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.