|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + amqp "github.com/rabbitmq/amqp091-go" |
| 9 | + |
| 10 | + "github.com/bxcodec/goqueue" |
| 11 | + "github.com/bxcodec/goqueue/consumer" |
| 12 | + "github.com/bxcodec/goqueue/interfaces" |
| 13 | + "github.com/bxcodec/goqueue/middleware" |
| 14 | + "github.com/bxcodec/goqueue/options" |
| 15 | + consumerOpts "github.com/bxcodec/goqueue/options/consumer" |
| 16 | + publisherOpts "github.com/bxcodec/goqueue/options/publisher" |
| 17 | + "github.com/bxcodec/goqueue/publisher" |
| 18 | +) |
| 19 | + |
| 20 | +func initExchange(ch *amqp.Channel, exchangeName string) error { |
| 21 | + return ch.ExchangeDeclare( |
| 22 | + exchangeName, |
| 23 | + "topic", |
| 24 | + true, |
| 25 | + false, |
| 26 | + false, |
| 27 | + false, |
| 28 | + nil, |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +func main() { |
| 33 | + rmqDSN := "amqp://rabbitmq:rabbitmq@localhost:5672/" |
| 34 | + rmqConn, err := amqp.Dial(rmqDSN) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + |
| 39 | + rmqPub := publisher.NewPublisher( |
| 40 | + publisherOpts.PublisherPlatformRabbitMQ, |
| 41 | + publisherOpts.WithRabbitMQPublisherConfig(&publisherOpts.RabbitMQPublisherConfig{ |
| 42 | + Conn: rmqConn, |
| 43 | + PublisherChannelPoolSize: 5, |
| 44 | + }), |
| 45 | + publisherOpts.WithPublisherID("publisher_id"), |
| 46 | + publisherOpts.WithMiddlewares( |
| 47 | + middleware.HelloWorldMiddlewareExecuteBeforePublisher(), |
| 48 | + middleware.HelloWorldMiddlewareExecuteAfterPublisher(), |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + requeueChannel, err := rmqConn.Channel() |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + defer requeueChannel.Close() |
| 58 | + initExchange(requeueChannel, "goqueue") |
| 59 | + |
| 60 | + consumerChannel, err := rmqConn.Channel() |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + defer consumerChannel.Close() |
| 65 | + rmqConsumer := consumer.NewConsumer( |
| 66 | + consumerOpts.ConsumerPlatformRabbitMQ, |
| 67 | + consumerOpts.WithRabbitMQConsumerConfig(consumerOpts.RabbitMQConfigWithDefaultTopicFanOutPattern( |
| 68 | + consumerChannel, |
| 69 | + requeueChannel, |
| 70 | + "goqueue", // exchange name |
| 71 | + []string{"goqueue.payments.#"}, // routing keys pattern |
| 72 | + )), |
| 73 | + consumerOpts.WithConsumerID("consumer_id"), |
| 74 | + consumerOpts.WithMiddlewares( |
| 75 | + middleware.HelloWorldMiddlewareExecuteAfterInboundMessageHandler(), |
| 76 | + middleware.HelloWorldMiddlewareExecuteBeforeInboundMessageHandler(), |
| 77 | + ), |
| 78 | + consumerOpts.WithMaxRetryFailedMessage(5), |
| 79 | + consumerOpts.WithBatchMessageSize(1), |
| 80 | + consumerOpts.WithQueueName("consumer_queue"), |
| 81 | + ) |
| 82 | + |
| 83 | + queueSvc := goqueue.NewQueueService( |
| 84 | + options.WithConsumer(rmqConsumer), |
| 85 | + options.WithPublisher(rmqPub), |
| 86 | + options.WithMessageHandler(handler()), |
| 87 | + ) |
| 88 | + go func() { |
| 89 | + for i := 0; i < 10; i++ { |
| 90 | + data := map[string]interface{}{ |
| 91 | + "message": fmt.Sprintf("Hello World %d", i), |
| 92 | + } |
| 93 | + jbyt, _ := json.Marshal(data) |
| 94 | + err := queueSvc.Publish(context.Background(), interfaces.Message{ |
| 95 | + Data: data, |
| 96 | + Action: "goqueue.payments.create", |
| 97 | + Topic: "goqueue", |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + panic(err) |
| 101 | + } |
| 102 | + fmt.Println("Message Sent: ", string(jbyt)) |
| 103 | + } |
| 104 | + }() |
| 105 | + |
| 106 | + // change to context.Background() if you want to run it forever |
| 107 | + // ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 108 | + // defer cancel() |
| 109 | + err = queueSvc.Start(context.Background()) |
| 110 | + if err != nil { |
| 111 | + panic(err) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func handler() interfaces.InboundMessageHandlerFunc { |
| 116 | + return func(ctx context.Context, m interfaces.InboundMessage) (err error) { |
| 117 | + fmt.Printf("Message: %+v\n", m) |
| 118 | + // something happend, we need to requeue the message |
| 119 | + return m.RetryWithDelayFn(ctx, interfaces.ExponentialBackoffDelayFn) |
| 120 | + } |
| 121 | +} |
0 commit comments