-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ExecutorService for sender and receiver tasks
- Loading branch information
1 parent
1d87774
commit d3be113
Showing
7 changed files
with
122 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,8 @@ | |
// [email protected]. | ||
package com.rabbitmq.model.amqp; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.Future; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.concurrent.*; | ||
|
||
abstract class Utils { | ||
|
||
|
@@ -78,4 +76,18 @@ public boolean isReleasable() { | |
} | ||
}); | ||
} | ||
|
||
static ExecutorService virtualThreadExecutorServiceIfAvailable() { | ||
boolean java21OrMore = Runtime.version().compareTo(Runtime.Version.parse("21")) >= 0; | ||
if (java21OrMore) { | ||
try { | ||
return (ExecutorService) | ||
Executors.class.getDeclaredMethod("newVirtualThreadPerTaskExecutor").invoke(null); | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} else { | ||
return Executors.newCachedThreadPool(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,17 @@ | |
// [email protected]. | ||
package com.rabbitmq.model; | ||
|
||
import static com.rabbitmq.model.TestUtils.*; | ||
import static java.nio.charset.StandardCharsets.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.rabbitmq.model.amqp.AmqpEnvironmentBuilder; | ||
import java.io.OutputStream; | ||
import java.util.Arrays; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
import org.apache.qpid.protonj2.client.*; | ||
import org.apache.qpid.protonj2.client.Message; | ||
import org.junit.jupiter.api.*; | ||
|
||
public class ClientTest { | ||
|
@@ -35,7 +38,7 @@ public class ClientTest { | |
|
||
@BeforeAll | ||
static void initAll() { | ||
environment = new AmqpEnvironmentBuilder().build(); | ||
environment = environmentBuilder().build(); | ||
management = environment.management(); | ||
} | ||
|
||
|
@@ -57,8 +60,7 @@ static void tearDownAll() { | |
|
||
@Test | ||
void deliveryCount() throws Exception { | ||
ClientOptions clientOptions = new ClientOptions(); | ||
try (Client client = Client.create(clientOptions); | ||
try (Client client = client(); | ||
Publisher publisher = environment.publisherBuilder().address(q).build()) { | ||
int messageCount = 10; | ||
CountDownLatch publishLatch = new CountDownLatch(5); | ||
|
@@ -69,21 +71,57 @@ void deliveryCount() throws Exception { | |
publisher.message().addData("".getBytes(UTF_8)), | ||
context -> publishLatch.countDown())); | ||
|
||
ConnectionOptions connectionOptions = new ConnectionOptions(); | ||
connectionOptions.user("guest"); | ||
connectionOptions.password("guest"); | ||
connectionOptions.virtualHost("vhost:/"); | ||
// only the mechanisms supported in RabbitMQ | ||
connectionOptions.saslOptions().addAllowedMechanism("PLAIN").addAllowedMechanism("EXTERNAL"); | ||
|
||
Connection connection = client.connect("localhost", 5672, connectionOptions); | ||
Connection connection = connection(client); | ||
Receiver receiver = connection.openReceiver(q, new ReceiverOptions()); | ||
int receivedMessages = 0; | ||
while (receiver.receive(100, TimeUnit.MILLISECONDS) != null) { | ||
receivedMessages++; | ||
} | ||
|
||
assertThat(receivedMessages).isEqualTo(messageCount); | ||
} | ||
} | ||
|
||
@Test | ||
void largeMessageWithSender() throws Exception { | ||
try (Client client = client()) { | ||
int maxFrameSize = 1000; | ||
Connection connection = | ||
connection(client, o -> o.traceFrames(false).maxFrameSize(maxFrameSize)); | ||
|
||
Sender sender = | ||
connection.openSender(q, new SenderOptions().deliveryMode(DeliveryMode.AT_LEAST_ONCE)); | ||
byte[] body = new byte[maxFrameSize * 4]; | ||
Arrays.fill(body, (byte) 'A'); | ||
Tracker tracker = sender.send(Message.create(body)); | ||
tracker.awaitSettlement(); | ||
} | ||
} | ||
|
||
@Test | ||
void largeMessageWithStreamSender() throws Exception { | ||
try (Client client = client()) { | ||
int maxFrameSize = 1000; | ||
Connection connection = | ||
connection(client, o -> o.traceFrames(false).maxFrameSize(maxFrameSize)); | ||
|
||
StreamSender sender = | ||
connection.openStreamSender( | ||
q, new StreamSenderOptions().deliveryMode(DeliveryMode.AT_LEAST_ONCE)); | ||
StreamSenderMessage message = sender.beginMessage(); | ||
byte[] body = new byte[maxFrameSize / 4]; | ||
Arrays.fill(body, (byte) 'A'); | ||
|
||
OutputStreamOptions streamOptions = new OutputStreamOptions().bodyLength(body.length); | ||
OutputStream output = message.body(streamOptions); | ||
|
||
final int chunkSize = 10; | ||
|
||
for (int i = 0; i < body.length; i += chunkSize) { | ||
output.write(body, i, chunkSize); | ||
} | ||
|
||
output.close(); | ||
message.tracker().awaitSettlement(); | ||
} | ||
} | ||
} |
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