@@ -17,6 +17,7 @@ func checkError(err error) {
17
17
18
18
func main () {
19
19
20
+ // see also: https://www.rabbitmq.com/blog/2024/12/13/amqp-filter-expressions
20
21
rmq .Info ("Golang AMQP 1.0 Streams example with filtering" )
21
22
queueStream := "stream-go-queue-filtering-" + time .Now ().String ()
22
23
env := rmq .NewEnvironment ([]string {"amqp://" }, nil )
@@ -35,7 +36,7 @@ func main() {
35
36
36
37
// create a stream publisher. In this case we use the QueueAddress to make the example
37
38
// simple. So we use the default exchange here.
38
- publisher , err := amqpConnection .NewPublisher (context .TODO (), & rmq.QueueAddress {Queue : queueStream }, "stream-publisher" )
39
+ publisher , err := amqpConnection .NewPublisher (context .TODO (), & rmq.QueueAddress {Queue : queueStream }, nil )
39
40
checkError (err )
40
41
41
42
filters := []string {"MyFilter1" , "MyFilter2" , "MyFilter3" , "MyFilter4" }
@@ -50,17 +51,14 @@ func main() {
50
51
switch publishResult .Outcome .(type ) {
51
52
case * rmq.StateAccepted :
52
53
rmq .Info ("[Publisher]" , "Message accepted" , publishResult .Message .Data [0 ])
53
- break
54
54
case * rmq.StateReleased :
55
55
rmq .Warn ("[Publisher]" , "Message was not routed" , publishResult .Message .Data [0 ])
56
- break
57
56
case * rmq.StateRejected :
58
57
rmq .Warn ("[Publisher]" , "Message rejected" , publishResult .Message .Data [0 ])
59
58
stateType := publishResult .Outcome .(* rmq.StateRejected )
60
59
if stateType .Error != nil {
61
60
rmq .Warn ("[Publisher]" , "Message rejected with error: %v" , stateType .Error )
62
61
}
63
- break
64
62
default :
65
63
// these status are not supported. Leave it for AMQP 1.0 compatibility
66
64
// see: https://www.rabbitmq.com/docs/next/amqp#outcomes
@@ -76,7 +74,25 @@ func main() {
76
74
77
75
// add a filter to the consumer, in this case we use only the filter values
78
76
// MyFilter1 and MyFilter2. So all other messages won't be received
79
- Filters : []string {"MyFilter1" , "MyFilter2" },
77
+ StreamFilterOptions : & rmq.StreamFilterOptions {
78
+ Values : []string {"MyFilter1" , "MyFilter2" },
79
+ // it is also possible to filter by application properties or message properties
80
+ // you can create filters like:
81
+ // msg.ApplicationProperties = map[string]interface{}{"key3": "value3"}
82
+ // during the publish you can do something like:
83
+ // msg.ApplicationProperties = map[string]interface{}{"key1": "value1"}
84
+ // publisher.Publish(context.Background(), msg)
85
+ //ApplicationProperties: nil,
86
+
87
+ // or here you can filter by message properties
88
+ // like:
89
+ // msg.Properties = &amqp.MessageProperties{Subject: "MySubject"}
90
+ // during the publish you can do something like:
91
+ // msg.Properties = &amqp.MessageProperties{Subject: "MySubject"}
92
+ // publisher.Publish(context.Background(), msg)
93
+ //Properties: nil,
94
+ // see amqp_consumer_stream_test.go for more examples
95
+ },
80
96
})
81
97
checkError (err )
82
98
0 commit comments