|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp" |
| 7 | + "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/ha" |
| 8 | + "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + fmt.Printf("Getting started with Streaming client for RabbitMQ\n") |
| 13 | + |
| 14 | + // Create the environment. You can set the log level to DEBUG for more information |
| 15 | + // stream.SetLevelInfo(logs.DEBUG) |
| 16 | + // the environment is the connection to the broker(s) |
| 17 | + env, err := stream.NewEnvironment(stream.NewEnvironmentOptions(). |
| 18 | + SetHost("localhost"). |
| 19 | + SetPort(5552)) |
| 20 | + if err != nil { |
| 21 | + fmt.Printf("Error creating environment: %v\n", err) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + // Create a stream |
| 26 | + streamName := "my-stream" |
| 27 | + // It is highly recommended to define the stream retention policy |
| 28 | + err = env.DeclareStream(streamName, stream.NewStreamOptions(). |
| 29 | + SetMaxLengthBytes(stream.ByteCapacity{}.GB(2))) |
| 30 | + |
| 31 | + // ignore the error if the stream already exists |
| 32 | + if err != nil && !errors.Is(err, stream.StreamAlreadyExists) { |
| 33 | + fmt.Printf("Error declaring stream: %v\n", err) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // declare the reliable consumer using the package ha |
| 38 | + consumer, err := ha.NewReliableConsumer(env, streamName, |
| 39 | + // start from the beginning of the stream |
| 40 | + stream.NewConsumerOptions(). |
| 41 | + SetOffset(stream.OffsetSpecification{}.First()), |
| 42 | + // handler where the messages will be processed |
| 43 | + func(consumerContext stream.ConsumerContext, message *amqp.Message) { |
| 44 | + fmt.Printf("Message received: %s\n", message.GetData()) |
| 45 | + }) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + fmt.Printf("Error creating consumer: %v\n", err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // Create the reliable producer using the package ha |
| 53 | + producer, err := ha.NewReliableProducer(env, streamName, |
| 54 | + // we leave the default options |
| 55 | + stream.NewProducerOptions(), |
| 56 | + // handler for the confirmation of the messages |
| 57 | + func(messageConfirm []*stream.ConfirmationStatus) { |
| 58 | + for _, msg := range messageConfirm { |
| 59 | + if msg.IsConfirmed() { |
| 60 | + fmt.Printf("message %s confirmed \n", msg.GetMessage().GetData()) |
| 61 | + } else { |
| 62 | + fmt.Printf("message %s failed \n", msg.GetMessage().GetData()) |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + fmt.Printf("Error creating producer: %v\n", err) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Send a message |
| 73 | + for i := 0; i < 10; i++ { |
| 74 | + err = producer.Send(amqp.NewMessage([]byte(fmt.Sprintf("Hello stream:%d", i)))) |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Error sending message: %v\n", err) |
| 77 | + return |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // press any key to exit |
| 82 | + fmt.Printf("Press any close the producer, consumer and environment\n") |
| 83 | + _, _ = fmt.Scanln() |
| 84 | + |
| 85 | + //// Close the producer |
| 86 | + err = producer.Close() |
| 87 | + if err != nil { |
| 88 | + fmt.Printf("Error closing producer: %v\n", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Close the consumer |
| 92 | + err = consumer.Close() |
| 93 | + if err != nil { |
| 94 | + fmt.Printf("Error closing consumer: %v\n", err) |
| 95 | + } |
| 96 | + |
| 97 | + err = env.DeleteStream(streamName) |
| 98 | + if err != nil { |
| 99 | + fmt.Printf("Error deleting stream: %v\n", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Close the environment |
| 103 | + err = env.Close() |
| 104 | + if err != nil { |
| 105 | + fmt.Printf("Error closing environment: %s\n", err) |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments