16
16
package com .rabbitmq .client .test ;
17
17
18
18
import com .rabbitmq .client .AMQP ;
19
- import com .rabbitmq .client .Command ;
20
19
import com .rabbitmq .client .DefaultConsumer ;
21
20
import com .rabbitmq .client .Envelope ;
22
21
import com .rabbitmq .client .impl .AMQImpl ;
26
25
27
26
import java .io .IOException ;
28
27
import java .util .UUID ;
29
- import java .util .concurrent .*;
28
+ import java .util .concurrent .CountDownLatch ;
29
+ import java .util .concurrent .ExecutorService ;
30
+ import java .util .concurrent .Executors ;
31
+ import java .util .concurrent .TimeUnit ;
30
32
31
33
import static org .junit .Assert .assertTrue ;
32
34
@@ -35,46 +37,73 @@ public class ChannelAsyncCompletableFutureTest extends BrokerTestCase {
35
37
ExecutorService executor ;
36
38
37
39
String queue ;
40
+ String exchange ;
38
41
39
42
@ Before public void init () {
40
- executor = Executors .newSingleThreadExecutor ( );
43
+ executor = Executors .newFixedThreadPool ( Runtime . getRuntime (). availableProcessors () );
41
44
queue = UUID .randomUUID ().toString ();
45
+ exchange = UUID .randomUUID ().toString ();
42
46
}
43
47
44
48
@ After public void tearDown () throws IOException {
45
49
executor .shutdownNow ();
46
50
channel .queueDelete (queue );
51
+ channel .exchangeDelete (exchange );
47
52
}
48
53
49
54
@ Test
50
55
public void async () throws Exception {
51
56
CountDownLatch latch = new CountDownLatch (1 );
52
- AMQP .Queue .Declare method = new AMQImpl .Queue .Declare .Builder ()
57
+ AMQP .Queue .Declare queueDeclare = new AMQImpl .Queue .Declare .Builder ()
53
58
.queue (queue )
54
59
.durable (true )
55
60
.exclusive (false )
56
61
.autoDelete (false )
57
62
.arguments (null )
58
63
.build ();
59
- CompletableFuture <Command > future = channel .asyncCompletableRpc (method );
60
- future .thenAcceptAsync (action -> {
61
- try {
62
- channel .basicPublish ("" , queue , null , "dummy" .getBytes ());
63
- } catch (IOException e ) {
64
- throw new RuntimeException (e );
65
- }
66
- }, executor ).thenAccept ((whatever ) -> {
67
- try {
68
- channel .basicConsume (queue , true , new DefaultConsumer (channel ) {
69
- @ Override
70
- public void handleDelivery (String consumerTag , Envelope envelope , AMQP .BasicProperties properties , byte [] body ) throws IOException {
71
- latch .countDown ();
72
- }
73
- });
74
- } catch (IOException e ) {
75
- throw new RuntimeException (e );
76
- }
77
- });
64
+
65
+ channel .asyncCompletableRpc (queueDeclare )
66
+ .thenComposeAsync (action -> {
67
+ try {
68
+ return channel .asyncCompletableRpc (new AMQImpl .Exchange .Declare .Builder ()
69
+ .exchange (exchange )
70
+ .type ("fanout" )
71
+ .durable (false )
72
+ .autoDelete (false )
73
+ .arguments (null )
74
+ .build ());
75
+ } catch (IOException e ) {
76
+ throw new RuntimeException (e );
77
+ }
78
+ }, executor ).thenComposeAsync (action -> {
79
+ try {
80
+ return channel .asyncCompletableRpc (new AMQImpl .Queue .Bind .Builder ()
81
+ .queue (queue )
82
+ .exchange (exchange )
83
+ .routingKey ("" )
84
+ .arguments (null )
85
+ .build ());
86
+ } catch (IOException e ) {
87
+ throw new RuntimeException (e );
88
+ }
89
+ }, executor ).thenAcceptAsync (action -> {
90
+ try {
91
+ channel .basicPublish ("" , queue , null , "dummy" .getBytes ());
92
+ } catch (IOException e ) {
93
+ throw new RuntimeException (e );
94
+ }
95
+ }, executor ).thenAcceptAsync ((whatever ) -> {
96
+ try {
97
+ channel .basicConsume (queue , true , new DefaultConsumer (channel ) {
98
+ @ Override
99
+ public void handleDelivery (String consumerTag , Envelope envelope , AMQP .BasicProperties properties , byte [] body ) throws IOException {
100
+ latch .countDown ();
101
+ }
102
+ });
103
+ } catch (IOException e ) {
104
+ throw new RuntimeException (e );
105
+ }
106
+ }, executor );
78
107
assertTrue (latch .await (1 , TimeUnit .SECONDS ));
79
108
}
80
109
0 commit comments