-
Notifications
You must be signed in to change notification settings - Fork 51
Add support for multiple datasources and transactionManagers in Spring #959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nilo85
wants to merge
11
commits into
gruelbox:master
Choose a base branch
from
nilo85:support-multiple-datasources
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e244c42
make SpringTransactionManager programatically use spring transaction …
nilo85 50c0a63
moved spring example to example.simple
nilo85 fecaba2
multi datasource test setup without outbox
nilo85 7ca70ef
made it boot with outbox (still no logic applied)
nilo85 d891842
implemented remaining stuff and added TransactionOutboxBackgroundProc…
nilo85 b2a01f7
reverted IDE autoremoved wildcard import to reduce noice and not forc…
nilo85 3c1606b
Catch and wrap then unwrap exception in transaction
nilo85 bde0927
use UncheckedException instead of making my own one
nilo85 5aff717
forgot to stash removal of custom exception
nilo85 901614b
ran mvn com.spotify.fmt:fmt-maven-plugin:format
nilo85 0c75fd6
added unit test for SpringTransactionManager that verifies inTransact…
nilo85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
89 changes: 89 additions & 0 deletions
89
.../transactionoutbox/spring/example/multipledatasources/EventuallyConsistentController.java
This file contains hidden or 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,89 @@ | ||
| package com.gruelbox.transactionoutbox.spring.example.multipledatasources; | ||
|
|
||
| import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
|
||
| import com.gruelbox.transactionoutbox.TransactionOutbox; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.Computer; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.ComputerExternalQueueService; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.ComputerRepository; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.employee.Employee; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.employee.EmployeeExternalQueueService; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.employee.EmployeeRepository; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @RestController | ||
| public class EventuallyConsistentController { | ||
|
|
||
| @Autowired | ||
| private ComputerRepository computerRepository; | ||
|
|
||
| @Autowired | ||
| private TransactionOutbox computerTransactionOutbox; | ||
|
|
||
| @Autowired | ||
| private EmployeeRepository employeeRepository; | ||
|
|
||
| @Autowired | ||
| private TransactionOutbox employeeTransactionOutbox; | ||
|
|
||
| @SuppressWarnings("SameReturnValue") | ||
| @PostMapping("/computer") | ||
| @Transactional(transactionManager = "computerTransactionManager") | ||
| public void createComputer( | ||
| @RequestBody Computer computer, | ||
| @RequestParam(name = "ordered", required = false) Boolean ordered) { | ||
|
|
||
| computerRepository.save(computer); | ||
| if (ordered != null && ordered) { | ||
| computerTransactionOutbox | ||
| .with() | ||
| .ordered("justonetopic") | ||
| .schedule(ComputerExternalQueueService.class) | ||
| .sendComputerCreatedEvent(computer); | ||
| } else { | ||
| computerTransactionOutbox.schedule(ComputerExternalQueueService.class).sendComputerCreatedEvent(computer); | ||
| } | ||
| } | ||
|
|
||
| @GetMapping("/computer/{id}") | ||
| public Computer getComputer(@PathVariable long id) { | ||
| return computerRepository | ||
| .findById(id) | ||
| .orElseThrow(() -> new ResponseStatusException(NOT_FOUND)); | ||
| } | ||
|
|
||
| @SuppressWarnings("SameReturnValue") | ||
| @PostMapping("/employee") | ||
| @Transactional(transactionManager = "employeeTransactionManager") | ||
| public void createEmployee( | ||
| @RequestBody Employee employee, | ||
| @RequestParam(name = "ordered", required = false) Boolean ordered) { | ||
|
|
||
| employeeRepository.save(employee); | ||
| if (ordered != null && ordered) { | ||
| employeeTransactionOutbox | ||
| .with() | ||
| .ordered("justonetopic") | ||
| .schedule(EmployeeExternalQueueService.class) | ||
| .sendEmployeeCreatedEvent(employee); | ||
| } else { | ||
| employeeTransactionOutbox.schedule(EmployeeExternalQueueService.class).sendEmployeeCreatedEvent(employee); | ||
| } | ||
| } | ||
|
|
||
| @GetMapping("/employee/{id}") | ||
| public Employee getEmployee(@PathVariable long id) { | ||
| return employeeRepository | ||
| .findById(id) | ||
| .orElseThrow(() -> new ResponseStatusException(NOT_FOUND)); | ||
| } | ||
| } |
164 changes: 164 additions & 0 deletions
164
...nsactionoutbox/spring/example/multipledatasources/EventuallyConsistentControllerTest.java
This file contains hidden or 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,164 @@ | ||
| package com.gruelbox.transactionoutbox.spring.example.multipledatasources; | ||
|
|
||
| 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 com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.Computer; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.Computer.Type; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.computer.ComputerExternalQueueService; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.employee.Employee; | ||
| import com.gruelbox.transactionoutbox.spring.example.multipledatasources.employee.EmployeeExternalQueueService; | ||
| import java.net.URL; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.test.web.client.TestRestTemplate; | ||
| import org.springframework.boot.test.web.server.LocalServerPort; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
|
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
| class EventuallyConsistentControllerTest { | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @LocalServerPort | ||
| private int port; | ||
|
|
||
| private URL base; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @Autowired | ||
| private TestRestTemplate template; | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate employeeJdbcTemplate; | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate computerJdbcTemplate; | ||
|
|
||
| @Autowired | ||
| private EmployeeExternalQueueService employeeExternalQueueService; | ||
| @Autowired | ||
| private ComputerExternalQueueService computerExternalQueueService; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| this.base = new URL("http://localhost:" + port + "/"); | ||
| employeeExternalQueueService.clear(); | ||
| computerExternalQueueService.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| void testCheckNormalEmployees() throws InterruptedException { | ||
| var joe = new Employee(1L, "Joe", "Strummer"); | ||
| var dave = new Employee(2L, "Dave", "Grohl"); | ||
| var neil = new Employee(3L, "Neil", "Diamond"); | ||
| var tupac = new Employee(4L, "Tupac", "Shakur"); | ||
| var jeff = new Employee(5L, "Jeff", "Mills"); | ||
|
|
||
| var url = base.toString() + "/employee"; | ||
| assertTrue(template.postForEntity(url, joe, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, dave, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, neil, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, tupac, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, jeff, Void.class).getStatusCode().is2xxSuccessful()); | ||
|
|
||
| employeeJdbcTemplate.execute( | ||
| "UPDATE txno_outbox SET invocation='non-deserializable invocation' WHERE invocation LIKE '%" | ||
| + neil.getLastName() | ||
| + "%'"); | ||
|
|
||
| await() | ||
| .atMost(10, SECONDS) | ||
| .pollDelay(1, SECONDS) | ||
| .untilAsserted( | ||
| () -> | ||
| assertThat(employeeExternalQueueService.getSent()) | ||
| .containsExactlyInAnyOrder(joe, dave, tupac, jeff)); | ||
| } | ||
|
|
||
| @Test | ||
| void testCheckNormalComputers() throws InterruptedException { | ||
| var computerPc1 = new Computer(1L, "pc-001", Type.DESKTOP); | ||
| var computerPc2 = new Computer(2L, "pc-002", Type.LAPTOP); | ||
| var computerPc3 = new Computer(3L, "pc-003", Type.LAPTOP); | ||
| var computerWebserver1 = new Computer(4L, "webserver-001", Type.SERVER); | ||
| var computerWebserver2 = new Computer(5L, "webserver-002", Type.SERVER); | ||
|
|
||
| var computerUrl = base.toString() + "/computer"; | ||
| assertTrue(template.postForEntity(computerUrl, computerPc1, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(computerUrl, computerPc2, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(computerUrl, computerPc3, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(computerUrl, computerWebserver1, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(computerUrl, computerWebserver2, Void.class).getStatusCode().is2xxSuccessful()); | ||
|
|
||
| computerJdbcTemplate.execute( | ||
| "UPDATE txno_outbox SET invocation='non-deserializable invocation' WHERE invocation LIKE '%" | ||
| + computerPc3.getName() | ||
| + "%'"); | ||
|
|
||
| await() | ||
| .atMost(10, SECONDS) | ||
| .pollDelay(1, SECONDS) | ||
| .untilAsserted( | ||
| () -> | ||
| assertThat(computerExternalQueueService.getSent()) | ||
| .containsExactlyInAnyOrder(computerPc1, computerPc2, computerWebserver1, computerWebserver2)); | ||
| } | ||
|
|
||
| @Test | ||
| void testCheckOrderedEmployees() { | ||
|
|
||
| var joe = new Employee(1L, "Joe", "Strummer"); | ||
| var dave = new Employee(2L, "Dave", "Grohl"); | ||
| var neil = new Employee(3L, "Neil", "Diamond"); | ||
| var tupac = new Employee(4L, "Tupac", "Shakur"); | ||
| var jeff = new Employee(5L, "Jeff", "Mills"); | ||
|
|
||
| var url = base.toString() + "/employee?ordered=true"; | ||
| assertTrue(template.postForEntity(url, joe, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, dave, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, neil, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, tupac, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, jeff, Void.class).getStatusCode().is2xxSuccessful()); | ||
|
|
||
| employeeJdbcTemplate.execute( | ||
| "UPDATE txno_outbox SET invocation='non-deserializable invocation' WHERE invocation LIKE '%" | ||
| + neil.getLastName() | ||
| + "%'"); | ||
|
|
||
| await() | ||
| .atMost(10, SECONDS) | ||
| .pollDelay(1, SECONDS) | ||
| .untilAsserted(() -> assertThat(employeeExternalQueueService.getSent()).containsExactly(joe, dave)); | ||
| } | ||
|
|
||
| @Test | ||
| void testCheckOrderedComputers() { | ||
|
|
||
| var computerPc1 = new Computer(1L, "pc-001", Type.DESKTOP); | ||
| var computerPc2 = new Computer(2L, "pc-002", Type.LAPTOP); | ||
| var computerPc3 = new Computer(3L, "pc-003", Type.LAPTOP); | ||
| var computerWebserver1 = new Computer(4L, "webserver-001", Type.SERVER); | ||
| var computerWebserver2 = new Computer(5L, "webserver-002", Type.SERVER); | ||
|
|
||
| var url = base.toString() + "/computer?ordered=true"; | ||
| assertTrue(template.postForEntity(url, computerPc1, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, computerPc2, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, computerPc3, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, computerWebserver1, Void.class).getStatusCode().is2xxSuccessful()); | ||
| assertTrue(template.postForEntity(url, computerWebserver2, Void.class).getStatusCode().is2xxSuccessful()); | ||
|
|
||
| employeeJdbcTemplate.execute( | ||
| "UPDATE txno_outbox SET invocation='non-deserializable invocation' WHERE invocation LIKE '%" | ||
| + computerPc3.getName() | ||
| + "%'"); | ||
|
|
||
| await() | ||
| .atMost(10, SECONDS) | ||
| .pollDelay(1, SECONDS) | ||
| .untilAsserted(() -> assertThat(computerExternalQueueService.getSent()).containsExactly(computerPc1, computerPc2)); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...gruelbox/transactionoutbox/spring/example/multipledatasources/ExternalsConfiguration.java
This file contains hidden or 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,15 @@ | ||
| package com.gruelbox.transactionoutbox.spring.example.multipledatasources; | ||
|
|
||
| import com.gruelbox.transactionoutbox.spring.SpringInstantiator; | ||
| import com.gruelbox.transactionoutbox.spring.SpringTransactionManager; | ||
| import javax.sql.DataSource; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.transaction.PlatformTransactionManager; | ||
|
|
||
| @Configuration | ||
| @Import({SpringInstantiator.class}) | ||
| class ExternalsConfiguration { | ||
| } |
33 changes: 33 additions & 0 deletions
33
...actionoutbox/spring/example/multipledatasources/TransactionOutboxBackgroundProcessor.java
This file contains hidden or 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,33 @@ | ||
| package com.gruelbox.transactionoutbox.spring.example.multipledatasources; | ||
|
|
||
| import com.gruelbox.transactionoutbox.TransactionOutbox; | ||
| import java.util.Collection; | ||
| 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 Collection<TransactionOutbox> outboxes; | ||
|
|
||
| @Scheduled(fixedRateString = "${outbox.repeatEvery}") | ||
| void poll() { | ||
| outboxes.forEach(outbox -> { | ||
| try { | ||
| do { | ||
| log.info("Flushing"); | ||
| } while (outbox.flush()); | ||
| } catch (Exception e) { | ||
| log.error("Error flushing transaction outbox. Pausing", e); | ||
| } | ||
| }); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...box/transactionoutbox/spring/example/multipledatasources/TransactionOutboxProperties.java
This file contains hidden or 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,16 @@ | ||
| package com.gruelbox.transactionoutbox.spring.example.multipledatasources; | ||
|
|
||
| import java.time.Duration; | ||
| import lombok.Data; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @ConfigurationProperties("outbox") | ||
| @Data | ||
| class TransactionOutboxProperties { | ||
| private Duration repeatEvery; | ||
| private boolean useJackson; | ||
| private Duration attemptFrequency; | ||
| private int blockAfterAttempts; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.