-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21889 from cescoffier/incoming-transactional-bloc…
…king Consider blocking reactive messaging methods using @transactional
- Loading branch information
Showing
7 changed files
with
119 additions
and
10 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
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
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
55 changes: 55 additions & 0 deletions
55
...ment/src/test/java/io/quarkus/smallrye/reactivemessaging/TransactionalSubscriberTest.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,55 @@ | ||
package io.quarkus.smallrye.reactivemessaging; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Outgoing; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.reactivestreams.Publisher; | ||
|
||
import io.quarkus.smallrye.reactivemessaging.blocking.beans.IncomingUsingTransactional; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Multi; | ||
|
||
public class TransactionalSubscriberTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ProduceIn.class, IncomingUsingTransactional.class) | ||
.addAsResource( | ||
new File("src/test/resources/config/worker-config.properties"), | ||
"application.properties")); | ||
|
||
@Inject | ||
IncomingUsingTransactional incoming; | ||
|
||
@Test | ||
public void testIncomingUsingRunOnWorkerThread() { | ||
await().until(() -> incoming.list().size() == 6); | ||
assertThat(incoming.list()).contains("a", "b", "c", "d", "e", "f"); | ||
|
||
List<String> threadNames = incoming.threads().stream().distinct() | ||
.collect(Collectors.toList()); | ||
assertThat(threadNames.contains(Thread.currentThread().getName())).isFalse(); | ||
for (String name : threadNames) { | ||
assertThat(name.startsWith("executor-thread-")).isTrue(); | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class ProduceIn { | ||
@Outgoing("in") | ||
public Publisher<String> produce() { | ||
return Multi.createFrom().items("a", "b", "c", "d", "e", "f"); | ||
} | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...java/io/quarkus/smallrye/reactivemessaging/blocking/beans/IncomingUsingTransactional.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 io.quarkus.smallrye.reactivemessaging.blocking.beans; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.transaction.Transactional; | ||
|
||
import org.eclipse.microprofile.reactive.messaging.Incoming; | ||
|
||
@ApplicationScoped | ||
public class IncomingUsingTransactional { | ||
private final List<String> list = new CopyOnWriteArrayList<>(); | ||
private final List<String> threads = new CopyOnWriteArrayList<>(); | ||
|
||
@Incoming("in") | ||
@Transactional | ||
public void consume(String s) { | ||
if (s.equals("b") || s.equals("d") || s.equals("f")) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
threads.add(Thread.currentThread().getName()); | ||
list.add(s); | ||
} | ||
|
||
public List<String> list() { | ||
return list; | ||
} | ||
|
||
public List<String> threads() { | ||
return threads; | ||
} | ||
} |