Skip to content

Commit

Permalink
added another example. Removed trivial ImportJSONTopology function to…
Browse files Browse the repository at this point in the history
… avoid unneccessary encoding/json dependency.
  • Loading branch information
Danlock committed Oct 14, 2023
1 parent 4c0f1e3 commit 6aa00d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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

Expand Down
9 changes: 0 additions & 9 deletions topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package rmq

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"time"

Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions topology_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"log/slog"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6aa00d8

Please sign in to comment.