8000 Sync with upstream · falcon677/rabbitmq-tutorials@4d95c34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d95c34

Browse files
Sync with upstream
2 parents 8aa3567 + 9d2aa9c commit 4d95c34

File tree

8 files changed

+38
-17
lines changed

8 files changed

+38
-17
lines changed

go/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Code examples are executed via `go run`:
3737

3838
[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html)
3939

40-
go run receive_logs_topic.go info warn
41-
go run emit_log_topic.go warn "a warning"
40+
go run receive_logs_topic.go "kern.*" "*.critical"
41+
go run emit_log_topic.go kern.critical "A critical kernel error"
4242

4343
[Tutorial six: RPC](http://www.rabbitmq.com/tutorial-six-python.html)
4444

go/emit_log.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"strings"
78

89
"github.com/streadway/amqp"
910
)
@@ -51,11 +52,11 @@ func main() {
5152
}
5253

5354
func bodyFrom(args []string) string {
54-
var body string
55+
var s string
5556
if (len(args) < 2) || os.Args[1] == "" {
56-
body = "hello"
57+
s = "hello"
5758
} else {
58-
body = os.Args[1]
59+
s = strings.Join(args[1:], " ")
5960
}
60-
return body
61+
return s
6162
}

go/new_task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"log"
6+
"os"
67
"strings"
78

89
"github.com/streadway/amqp"

go/receive_logs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ func main() {
2323
failOnError(err, "Failed to open a channel")
2424
defer ch.Close()
2525

26+
err = ch.ExchangeDeclare(
27+
"logs", // name
28+
"fanout", // type
29+
true, // durable
30+
false, // auto-deleted
31+
false, // internal
32+
false, // no-wait
33+
nil, // arguments
34+
)
35+
failOnError(err, "Failed to declare an exchange")
36+
2637
q, err := ch.QueueDeclare(
2738
"", // name
2839
false, // durable

go/receive_logs_direct.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ func main() {
4545
)
4646
failOnError(err, "Failed to declare a queue")
4747

48-
for _, s := range os.Args {
48+
if len(os.Args) < 2 {
49+
log.Printf("Usage: %s [info] [warning] [error]", os.Args[0])
50+
os.Exit(0)
51+
}
52+
for _, s := range os.Args[1:] {
4953
log.Printf("Binding queue %s to exchange %s with routing key %s", q.Name, "logs_direct", s)
5054
err = ch.QueueBind(
5155
q.Name, // queue name
@@ -67,18 +71,14 @@ func main() {
6771
)
6872
failOnError(err, "Failed to register a consumer")
6973

70-
done := make(chan bool)
74+
forever := make(chan bool)
7175

7276
go func() {
7377
for d := range msgs {
7478
log.Printf(" [x] %s", d.Body)
75-
76-
done <- true
7779
}
7880
}()
7981

8082
log.Printf(" [*] Waiting for logs. To exit press CTRL+C")
81-
82-
<-done
83-
log.Printf("Done")
83+
<-forever
8484
}

go/receive_logs_topic.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ func main() {
4545
)
4646
failOnError(err, "Failed to declare a queue")
4747

48-
for _, s := range os.Args {
48+
if len(os.Args) < 2 {
49+
log.Printf("Usage: %s [binding_key]...", os.Args[0])
50+
os.Exit(0)
51+
}
52+
for _, s := range os.Args[1:] {
4953
log.Printf("Binding queue %s to exchange %s with routing key %s", q.Name, "logs_topic", s)
5054
err = ch.QueueBind(
5155
q.Name, // queue name

go/worker.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
5-
"log"
6-
76
"github.com/streadway/amqp"
7+
"log"
8+
"time"
89
)
910

1011
func failOnError(err error, msg string) {
@@ -57,6 +58,9 @@ func main() {
5758
for d := range msgs {
5859
log.Printf("Received a message: %s", d.Body)
5960
d.Ack(false)
61+
dot_count := bytes.Count(d.Body, []byte("."))
62+
t := time.Duration(dot_count)
63+
time.Sleep(t * time.Second)
6064
}
6165
}()
6266

javascript-nodejs/send.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var amqp_hacks = require('./amqp-hacks');
44
var connection = amqp.createConnection({host: 'localhost'});
55

66
connection.on('ready', function(){
7-
connection.publish('hello', 'Hello World!');
7+
connection.publish('task_queue', 'Hello World!');
88
console.log(" [x] Sent 'Hello World!'");
99

1010
amqp_hacks.safeEndConnection(connection);

0 commit comments

Comments
 (0)
0