@@ -14,34 +14,74 @@ func Example_direct_exchange() {
14
14
m := mockMessage {
15
15
Message : "foo" ,
16
16
}
17
- w := NewWorker (
17
+ w1 := NewWorker (
18
18
WithSubj ("direct_queue" ),
19
19
WithExchangeName ("direct_exchange" ),
20
- WithRoutingKey ( "direct_queue " ),
21
- WithTag ( "direct_queue " ),
20
+ WithExchangeType ( "direct " ),
21
+ WithRoutingKey ( "direct_exchange " ),
22
22
WithRunFunc (func (ctx context.Context , m core.QueuedMessage ) error {
23
- fmt .Println ("get data:" , string (m .Bytes ()))
23
+ fmt .Println ("worker01 get data:" , string (m .Bytes ()))
24
+ time .Sleep (100 * time .Millisecond )
24
25
return nil
25
26
}),
26
27
)
28
+
29
+ q1 , err := queue .NewQueue (
30
+ queue .WithWorker (w1 ),
31
+ )
32
+ if err != nil {
33
+ w1 .opts .logger .Error (err )
34
+ }
35
+ q1 .Start ()
36
+
37
+ w2 := NewWorker (
38
+ WithSubj ("direct_queue" ),
39
+ WithExchangeName ("direct_exchange" ),
40
+ WithExchangeType ("direct" ),
41
+ WithRoutingKey ("direct_exchange" ),
42
+ WithRunFunc (func (ctx context.Context , m core.QueuedMessage ) error {
43
+ fmt .Println ("worker02 get data:" , string (m .Bytes ()))
44
+ time .Sleep (100 * time .Millisecond )
45
+ return nil
46
+ }),
47
+ )
48
+
49
+ q2 , err := queue .NewQueue (
50
+ queue .WithWorker (w2 ),
51
+ )
52
+ if err != nil {
53
+ w2 .opts .logger .Error (err )
54
+ }
55
+ q2 .Start ()
56
+
57
+ w := NewWorker (
58
+ WithExchangeName ("direct_exchange" ),
59
+ WithExchangeType ("direct" ),
60
+ WithRoutingKey ("direct_exchange" ),
61
+ )
62
+
27
63
q , err := queue .NewQueue (
28
64
queue .WithWorker (w ),
29
- queue .WithWorkerCount (1 ),
30
65
)
31
66
if err != nil {
32
67
w .opts .logger .Error (err )
33
68
}
34
69
35
- q .Start ()
36
70
time .Sleep (200 * time .Millisecond )
37
71
q .Queue (m )
38
72
q .Queue (m )
73
+ q .Queue (m )
74
+ q .Queue (m )
39
75
time .Sleep (200 * time .Millisecond )
40
76
q .Release ()
77
+ q1 .Release ()
78
+ q2 .Release ()
41
79
42
- // Output:
43
- // get data: foo
44
- // get data: foo
80
+ // Unordered Output:
81
+ // worker01 get data: foo
82
+ // worker02 get data: foo
83
+ // worker01 get data: foo
84
+ // worker02 get data: foo
45
85
}
46
86
47
87
// Fanout Exchange
@@ -65,9 +105,7 @@ func Example_fanout_exchange() {
65
105
if err != nil {
66
106
w1 .opts .logger .Error (err )
67
107
}
68
-
69
108
q1 .Start ()
70
- time .Sleep (200 * time .Millisecond )
71
109
72
110
w2 := NewWorker (
73
111
WithSubj ("fanout_queue_2" ),
@@ -85,9 +123,7 @@ func Example_fanout_exchange() {
85
123
if err != nil {
86
124
w2 .opts .logger .Error (err )
87
125
}
88
-
89
126
q2 .Start ()
90
- time .Sleep (200 * time .Millisecond )
91
127
92
128
w := NewWorker (
93
129
WithExchangeName ("fanout_exchange" ),
0 commit comments