FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rabbitmq-tutorials/java/Worker.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
/
Worker.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (39 loc) · 1.62 KB
Breadcrumbs
rabbitmq-tutorials
/
java
/
Worker.java
Copy path
File metadata and controls
48 lines (39 loc) · 1.62 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
import
com
.
rabbitmq
.
client
.
Channel
;
import
com
.
rabbitmq
.
client
.
Connection
;
import
com
.
rabbitmq
.
client
.
ConnectionFactory
;
import
com
.
rabbitmq
.
client
.
DeliverCallback
;
import
java
.
util
.
Map
;
public
class
Worker
{
private
static
final
String
TASK_QUEUE_NAME
=
"task_queue"
;
public
static
void
main
(
String
[]
argv
)
throws
Exception
{
ConnectionFactory
factory
=
new
ConnectionFactory
();
factory
.
setHost
(
"localhost"
);
final
Connection
connection
=
factory
.
newConnection
();
final
Channel
channel
=
connection
.
createChannel
();
channel
.
queueDeclare
(
TASK_QUEUE_NAME
,
true
,
false
,
false
,
Map
.
of
(
"x-queue-type"
,
"quorum"
));
System
.
out
.
println
(
" [*] Waiting for messages. To exit press CTRL+C"
);
channel
.
basicQos
(
1
);
DeliverCallback
deliverCallback
= (
consumerTag
,
delivery
) -> {
String
message
=
new
String
(
delivery
.
getBody
(),
"UTF-8"
);
System
.
out
.
println
(
" [x] Received '"
+
message
+
"'"
);
try
{
doWork
(
message
);
}
finally
{
System
.
out
.
println
(
" [x] Done"
);
channel
.
basicAck
(
delivery
.
getEnvelope
().
getDeliveryTag
(),
false
);
}
};
channel
.
basicConsume
(
TASK_QUEUE_NAME
,
false
,
deliverCallback
,
consumerTag
-> { });
}
private
static
void
doWork
(
String
task
) {
for
(
char
ch
:
task
.
toCharArray
()) {
if
(
ch
==
'.'
) {
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
_ignored
) {
Thread
.
currentThread
().
interrupt
();
}
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL