From 6aa00d8ab983c56af013ffad71c4ee601b2aeddf Mon Sep 17 00:00:00 2001 From: Danlock Date: Fri, 13 Oct 2023 22:02:51 -0400 Subject: [PATCH] added another example. Removed trivial ImportJSONTopology function to avoid unneccessary encoding/json dependency. --- README.md | 24 ++++++++++++++++++++++-- topology.go | 9 --------- topology_int_test.go | 16 ---------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 3a27474..81e9446 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ cfg := rmq.Args{Log: slog.Log} rmqConn := rmq.ConnectWithAMQPConfig(ctx, rmq.ConnectArgs{Args: cfg}, os.Getenv("AMQP_URL"), amqp.Config{}) -consCfg := rmq.ConsumerArgs{ +consCfg := rmq.ConsumerArgs{ Args: cfg, Queue: rmq.Queue{Name: "q2d2", AutoDelete: true}, Qos: rmq.Qos{PrefetchCount: 1000}, @@ -60,7 +60,27 @@ rmq.NewConsumer(rmqConn, consCfg).ConsumeConcurrently(ctx, 100, func(ctx context }) ``` -Take a look at healthcheck_int_test.go for a more complete example. +Creating an AMQP topology that is automatically applied on reconnections as seen in the Java and C# RabbitMQ client drivers. + +``` +ctx, := context.TODO() +cfg := rmq.Args{Log: slog.Log} + +topology := rmq.Topology{ + Args: cfg, + Exchanges: []rmq.Exchange{{Name: "xchg", Kind: amqp.ExchangeDirect, AutoDelete: true}}, + Queues: []rmq.Queue{{Name: "huehue", Durable: true, AutoDelete: true}}, + QueueBindings: []rmq.QueueBinding{{QueueName: "huehue", ExchangeName: "xchg"}}, +} + +// It may be desired to read your AMQP topology from disk as JSON or some other config format. rmq.Topology is a simple struct so it can be done like so. +// err := json.NewDecoder(topologyFile).Decode(&topology) +// topology.Args = cfg + +rmqConn := rmq.ConnectWithURLs(ctx, rmq.ConnectArgs{Args: cfg, Topology: topology}, os.Getenv("AMQP_URL")) +``` + +Take a look at healthcheck_int_test.go for a more complete example of using all of danlock/rmq together. # Logging diff --git a/topology.go b/topology.go index d4ddb5c..d067119 100644 --- a/topology.go +++ b/topology.go @@ -2,10 +2,8 @@ package rmq import ( "context" - "encoding/json" "errors" "fmt" - "io" "log/slog" "time" @@ -87,13 +85,6 @@ func DeclareTopology(ctx context.Context, amqpConn AMQPConnection, topology Topo } } -// ImportJSONTopology reads in a Topology from a file. Useful for setting Exchanges, ExchangeBindings, Queues and QueueBindings from a config, -// although rabbitmqctl is probably a better candidate for this since it can also export your cuurrent RabbitMQ schema. -// Decoding files to structs is easy in Golang, so feel free to write your own as desired for XML, YAML, TOML or any other desired config format. -func ImportJSONTopology(topologyReader io.Reader) (top Topology, _ error) { - return top, json.NewDecoder(topologyReader).Decode(&top) -} - // Topology contains all the exchange, queue and binding information needed for your application to use RabbitMQ. type Topology struct { Args diff --git a/topology_int_test.go b/topology_int_test.go index efa9fe7..55392fd 100644 --- a/topology_int_test.go +++ b/topology_int_test.go @@ -7,7 +7,6 @@ import ( "fmt" "log/slog" "os" - "strings" "testing" "time" @@ -61,15 +60,6 @@ func TestDeclareTopology(t *testing.T) { }}, } - jsonTopologyString := `{ - "Exchanges": [{"Name":"jsonXchg", "Kind": "topic", "AutoDelete": true}], - "Queues": [{"Name": "jsonQueue", "AutoDelete": true}] - }` - jsonTopology, err := rmq.ImportJSONTopology(strings.NewReader(jsonTopologyString)) - if err != nil { - t.Fatalf("failed to ImportJSONTopology %v", err) - } - amqpConn, err := rmqConn.CurrentConnection(ctx) if err != nil { t.Fatalf("failed to CurrentConnection %v", err) @@ -99,12 +89,6 @@ func TestDeclareTopology(t *testing.T) { baseTopology, true, }, - { - "json success", - time.Minute, - jsonTopology, - false, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {