FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rabbitmq-tutorials/java/RPCClient.java at main · rabbitmq/rabbitmq-tutorials · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
rabbitmq
/
rabbitmq-tutorials
Public
Notifications
You must be signed in to change notification settings
Fork
3.5k
Star
6.9k
Code
Issues
3
Pull requests
1
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
rabbitmq-tutorials
/
java
/
RPCClient.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (53 loc) · 2.25 KB
Breadcrumbs
rabbitmq-tutorials
/
java
/
RPCClient.java
Copy path
File metadata and controls
67 lines (53 loc) · 2.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import
com
.
rabbitmq
.
client
.
AMQP
;
import
com
.
rabbitmq
.
client
.
Channel
;
import
com
.
rabbitmq
.
client
.
Connection
;
import
com
.
rabbitmq
.
client
.
ConnectionFactory
;
import
java
.
io
.
IOException
;
import
java
.
util
.
UUID
;
import
java
.
util
.
concurrent
.*;
public
class
RPCClient
implements
AutoCloseable
{
private
Connection
connection
;
private
Channel
channel
;
private
String
requestQueueName
=
"rpc_queue"
;
public
RPCClient
()
throws
IOException
,
TimeoutException
{
ConnectionFactory
factory
=
new
ConnectionFactory
();
factory
.
setHost
(
"localhost"
);
connection
=
factory
.
newConnection
();
channel
=
connection
.
createChannel
();
}
public
static
void
main
(
String
[]
argv
) {
try
(
RPCClient
fibonacciRpc
=
new
RPCClient
()) {
for
(
int
i
=
0
;
i
<
32
;
i
++) {
String
i_str
=
Integer
.
toString
(
i
);
System
.
out
.
println
(
" [x] Requesting fib("
+
i_str
+
")"
);
String
response
=
fibonacciRpc
.
call
(
i_str
);
System
.
out
.
println
(
" [.] Got '"
+
response
+
"'"
);
}
}
catch
(
IOException
|
TimeoutException
|
InterruptedException
|
ExecutionException
e
) {
e
.
printStackTrace
();
}
}
public
String
call
(
String
message
)
throws
IOException
,
InterruptedException
,
ExecutionException
{
final
String
corrId
=
UUID
.
randomUUID
().
toString
();
String
replyQueueName
=
channel
.
queueDeclare
().
getQueue
();
AMQP
.
BasicProperties
props
=
new
AMQP
.
BasicProperties
.
Builder
()
.
correlationId
(
corrId
)
.
replyTo
(
replyQueueName
)
.
build
();
channel
.
basicPublish
(
""
,
requestQueueName
,
props
,
message
.
getBytes
(
"UTF-8"
));
final
CompletableFuture
<
String
>
response
=
new
CompletableFuture
<>();
String
ctag
=
channel
.
basicConsume
(
replyQueueName
,
true
, (
consumerTag
,
delivery
) -> {
if
(
delivery
.
getProperties
().
getCorrelationId
().
equals
(
corrId
)) {
response
.
complete
(
new
String
(
delivery
.
getBody
(),
"UTF-8"
));
}
},
consumerTag
-> {
});
String
result
=
response
.
get
();
channel
.
basicCancel
(
ctag
);
return
result
;
}
public
void
close
()
throws
IOException
{
connection
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL