|
1 | 1 | # goqueue |
2 | 2 |
|
3 | | -GoQueue - Golang Queue Wrapper for all queue platforms |
| 3 | +GoQueue - one library to rule them all. A golang wrapper that handles all the complexity of every Queue platforms. Extensible and easy to learn |
| 4 | + |
| 5 | +## Index |
| 6 | + |
| 7 | +- [Support](#support) |
| 8 | +- [Getting Started](#getting-started) |
| 9 | +- [Example](#example) |
| 10 | +- [Contribution](#contribution) |
| 11 | + |
| 12 | +## Support |
| 13 | + |
| 14 | +You can file an [Issue](https://github.com/bxcodec/goqueue/issues/new). |
| 15 | +See documentation in [Go.Dev](https://pkg.go.dev/github.com/bxcodec/goqueue?tab=doc) |
| 16 | + |
| 17 | +## Getting Started |
| 18 | + |
| 19 | +#### Install |
| 20 | + |
| 21 | +```shell |
| 22 | +go get -u github.com/bxcodec/goqueue |
| 23 | +``` |
| 24 | + |
| 25 | +# Example |
| 26 | + |
| 27 | +```go |
| 28 | +package main |
| 29 | + |
| 30 | +import ( |
| 31 | + "context" |
| 32 | + "encoding/json" |
| 33 | + "fmt" |
| 34 | + "time" |
| 35 | + |
| 36 | + amqp "github.com/rabbitmq/amqp091-go" |
| 37 | + |
| 38 | + "github.com/bxcodec/goqueue" |
| 39 | + "github.com/bxcodec/goqueue/consumer" |
| 40 | + rmqConsumer "github.com/bxcodec/goqueue/consumer/rabbitmq" |
| 41 | + "github.com/bxcodec/goqueue/middleware" |
| 42 | + "github.com/bxcodec/goqueue/publisher" |
| 43 | + rmqPublisher "github.com/bxcodec/goqueue/publisher/rabbitmq" |
| 44 | +) |
| 45 | + |
| 46 | +func initExchange(ch *amqp.Channel, exchangeName string) error { |
| 47 | + return ch.ExchangeDeclare( |
| 48 | + exchangeName, |
| 49 | + "topic", |
| 50 | + true, |
| 51 | + false, |
| 52 | + false, |
| 53 | + false, |
| 54 | + nil, |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +func main() { |
| 59 | + rmqDSN := "amqp://rabbitmq:rabbitmq@localhost:5672/" |
| 60 | + rmqConn, err := amqp.Dial(rmqDSN) |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + |
| 65 | + rmqPub := rmqPublisher.NewPublisher(rmqConn, |
| 66 | + publisher.WithPublisherID("publisher_id"), |
| 67 | + publisher.WithMiddlewares( |
| 68 | + middleware.HelloWorldMiddlewareExecuteBeforePublisher(), |
| 69 | + middleware.HelloWorldMiddlewareExecuteAfterPublisher()), |
| 70 | + ) |
| 71 | + |
| 72 | + publisherChannel, err := rmqConn.Channel() |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + defer publisherChannel.Close() |
| 78 | + initExchange(publisherChannel, "goqueue") |
| 79 | + |
| 80 | + consumerChannel, err := rmqConn.Channel() |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + defer consumerChannel.Close() |
| 85 | + |
| 86 | + rmqConsumer := rmqConsumer.NewConsumer( |
| 87 | + publisherChannel, |
| 88 | + consumerChannel, |
| 89 | + consumer.WithMiddlewares( |
| 90 | + middleware.HelloWorldMiddlewareExecuteAfterInboundMessageHandler(), |
| 91 | + middleware.HelloWorldMiddlewareExecuteBeforeInboundMessageHandler()), |
| 92 | + consumer.WithQueueName("consumer_queue"), |
| 93 | + consumer.WithConsumerID("consumer_id"), |
| 94 | + consumer.WithBatchMessageSize(1), |
| 95 | + consumer.WithMaxRetryFailedMessage(3), |
| 96 | + consumer.WithActionsPatternSubscribed("goqueue.payments.#", "goqueue.users.#"), |
| 97 | + consumer.WithTopicName("goqueue"), |
| 98 | + ) |
| 99 | + |
| 100 | + queueSvc := goqueue.NewQueueService( |
| 101 | + goqueue.WithPublisher(rmqPub), |
| 102 | + goqueue.WithConsumer(rmqConsumer), |
| 103 | + goqueue.WithMessageHandler(handler()), |
| 104 | + ) |
| 105 | + |
| 106 | + go func() { |
| 107 | + for i := 0; i < 10; i++ { |
| 108 | + data := map[string]interface{}{ |
| 109 | + "message": fmt.Sprintf("Hello World %d", i), |
| 110 | + } |
| 111 | + jbyt, _ := json.Marshal(data) |
| 112 | + err := queueSvc.Publish(context.Background(), goqueue.Message{ |
| 113 | + Data: data, |
| 114 | + Action: "goqueue.payments.create", |
| 115 | + Topic: "goqueue", |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + fmt.Println("Message Sent: ", string(jbyt)) |
| 121 | + } |
| 122 | + }() |
| 123 | + |
| 124 | + // change to context.Background() if you want to run it forever |
| 125 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 126 | + defer cancel() |
| 127 | + err = queueSvc.Start(ctx) |
| 128 | + if err != nil { |
| 129 | + panic(err) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func handler() goqueue.InboundMessageHandlerFunc { |
| 134 | + return func(ctx context.Context, m goqueue.InboundMessage) (err error) { |
| 135 | + data := m.Data |
| 136 | + jbyt, _ := json.Marshal(data) |
| 137 | + fmt.Println("Message Received: ", string(jbyt)) |
| 138 | + return m.Ack(ctx) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +``` |
| 143 | + |
| 144 | +## Contribution |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +To contrib to this project, you can open a PR or an issue. |
0 commit comments