|
1 | 1 | # rabbitmq
|
2 | 2 |
|
3 | 3 | [RabbitMQ](https://www.rabbitmq.com/) as backend for Queue Package. See the [Go RabbitMQ Client Library](https://github.com/rabbitmq/amqp091-go) maintained by the [RabbitMQ core team](https://github.com/rabbitmq). It was [originally developed by Sean Treadway](https://github.com/streadway/amqp).
|
| 4 | + |
| 5 | +## Exchanges and Exchange Types |
| 6 | + |
| 7 | +See the [Exchanges and Exchange Types][11] section of [AMQP 0-9-1 Model Explained][12]. |
| 8 | + |
| 9 | +[11]:https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges |
| 10 | +[12]:https://www.rabbitmq.com/tutorials/amqp-concepts.html |
| 11 | + |
| 12 | +### Direct Exchange |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +See the consumer code: |
| 17 | + |
| 18 | +```go |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "flag" |
| 25 | + "fmt" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/appleboy/graceful" |
| 29 | + "github.com/golang-queue/queue" |
| 30 | + "github.com/golang-queue/queue/core" |
| 31 | + "github.com/golang-queue/rabbitmq" |
| 32 | +) |
| 33 | + |
| 34 | +type job struct { |
| 35 | + Message string |
| 36 | +} |
| 37 | + |
| 38 | +func (j *job) Bytes() []byte { |
| 39 | + b, err := json.Marshal(j) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + return b |
| 44 | +} |
| 45 | + |
| 46 | +var ( |
| 47 | + uri = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI") |
| 48 | + exchange = flag.String("exchange", "test-exchange", "Durable, non-auto-deleted AMQP exchange name") |
| 49 | + exchangeType = flag.String("exchange-type", "direct", "Exchange type - direct|fanout|topic|x-custom") |
| 50 | + q = flag.String("queue", "test-queue", "Ephemeral AMQP queue name") |
| 51 | + bindingKey = flag.String("key", "test-key", "AMQP binding key") |
| 52 | +) |
| 53 | + |
| 54 | +func init() { |
| 55 | + flag.Parse() |
| 56 | +} |
| 57 | + |
| 58 | +func main() { |
| 59 | + taskN := 10000 |
| 60 | + rets := make(chan string, taskN) |
| 61 | + |
| 62 | + m := graceful.NewManager() |
| 63 | + |
| 64 | + // define the worker |
| 65 | + w := rabbitmq.NewWorker( |
| 66 | + rabbitmq.WithAddr(*uri), |
| 67 | + rabbitmq.WithQueue(*q), |
| 68 | + rabbitmq.WithExchangeName(*exchange), |
| 69 | + rabbitmq.WithExchangeType(*exchangeType), |
| 70 | + rabbitmq.WithRoutingKey(*bindingKey), |
| 71 | + rabbitmq.WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error { |
| 72 | + var v *job |
| 73 | + if err := json.Unmarshal(m.Bytes(), &v); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + rets <- v.Message |
| 77 | + time.Sleep(500 * time.Millisecond) |
| 78 | + return nil |
| 79 | + }), |
| 80 | + ) |
| 81 | + // define the queue |
| 82 | + q := queue.NewPool( |
| 83 | + 2, |
| 84 | + queue.WithWorker(w), |
| 85 | + ) |
| 86 | + |
| 87 | + m.AddRunningJob(func(ctx context.Context) error { |
| 88 | + for { |
| 89 | + select { |
| 90 | + case <-ctx.Done(): |
| 91 | + select { |
| 92 | + case m := <-rets: |
| 93 | + fmt.Println("message:", m) |
| 94 | + default: |
| 95 | + } |
| 96 | + return nil |
| 97 | + case m := <-rets: |
| 98 | + fmt.Println("message:", m) |
| 99 | + time.Sleep(50 * time.Millisecond) |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + m.AddShutdownJob(func() error { |
| 105 | + // shutdown the service and notify all the worker |
| 106 | + q.Release() |
| 107 | + return nil |
| 108 | + }) |
| 109 | + |
| 110 | + <-m.Done() |
| 111 | +} |
| 112 | +``` |
0 commit comments