| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package main |
| 21 |
|
| 22 |
import ( |
| 23 |
"flag" |
| 24 |
"fmt" |
| 25 |
"log" |
| 26 |
"os" |
| 27 |
"strings" |
| 28 |
|
| 29 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 30 |
"github.com/apache/qpid-proton/go/pkg/electron" |
| 31 |
) |
| 32 |
|
| 33 |
// NOTE: this example requires a broker thta supports the APACHE.ORG:SELECTOR filter. |
| 34 |
|
| 35 |
// Usage and command-line flags |
| 36 |
func usage() { |
| 37 |
fmt.Fprintf(os.Stderr, `Usage: %s |
| 38 |
Receive messages that match a query. |
| 39 |
`, os.Args[0]) |
| 40 |
flag.PrintDefaults() |
| 41 |
} |
| 42 |
|
| 43 |
var count = flag.Int("count", 5, "Number of messages to send, will only receive half") |
| 44 |
var url = flag.String("url", "/examples", "Receive messages from this URL") |
| 45 |
|
| 46 |
// Example function to create an AMQP filter-set containing an APACHE.ORG:SELECTOR filter. |
| 47 |
// This filters messages using an SQL-like query expression. |
| 48 |
// See http://www.amqp.org/specification/1.0/filters for more |
| 49 |
func selectorFilter(query string) map[amqp.Symbol]interface{} { |
| 50 |
filter := amqp.Described{ |
| 51 |
Descriptor: amqp.Symbol("apache.org:selector-filter:string"), // Identify the type of filter |
| 52 |
Value: query, // Query to evaluate |
| 53 |
} |
| 54 |
// Return a filter set (map) with a single selector filter. |
| 55 |
// Note "selector" is an arbitrary name to identify the filter, it can be any non-empty string. |
| 56 |
return map[amqp.Symbol]interface{}{"selector": filter} |
| 57 |
} |
| 58 |
|
| 59 |
func main() { |
| 60 |
flag.Usage = usage |
| 61 |
flag.Parse() |
| 62 |
u, err := amqp.ParseURL(*url) |
| 63 |
fatalIf(err) |
| 64 |
container := electron.NewContainer(fmt.Sprintf("%s[%v]", os.Args[0], os.Getpid())) |
| 65 |
c, err := container.Dial("tcp", u.Host) // NOTE: Dial takes just the Host part of the U |
| 66 |
fatalIf(err) |
| 67 |
defer c.Close(nil) |
| 68 |
addr := strings.TrimPrefix(u.Path, "/") |
| 69 |
s, err := c.Sender(electron.Target(addr)) |
| 70 |
fatalIf(err) |
| 71 |
r, err := c.Receiver(electron.Source(addr), electron.Filter(selectorFilter("colour = 'green'"))) |
| 72 |
fatalIf(err) |
| 73 |
|
| 74 |
send(s, *count) |
| 75 |
receive(r, *count/2) |
| 76 |
} |
| 77 |
|
| 78 |
func send(s electron.Sender, n int) { |
| 79 |
defer s.Close(nil) |
| 80 |
for i := 0; i < n; i++ { |
| 81 |
m := amqp.NewMessageWith(i) |
| 82 |
if i%2 == 0 { |
| 83 |
m.ApplicationProperties()["colour"] = "red" |
| 84 |
} else { |
| 85 |
m.ApplicationProperties()["colour"] = "green" |
| 86 |
} |
| 87 |
s.SendSync(m) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func receive(r electron.Receiver, n int) { |
| 92 |
defer r.Close(nil) |
| 93 |
for i := 0; i < n; i++ { |
| 94 |
switch rm, err := r.Receive(); err { |
| 95 |
case nil: |
| 96 |
rm.Accept() |
| 97 |
log.Print(rm.Message) |
| 98 |
case electron.Closed: |
| 99 |
return |
| 100 |
default: |
| 101 |
log.Fatalf("receive error: %v", err) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func fatalIf(err error) { |
| 107 |
if err != nil { |
| 108 |
log.Fatal(err) |
| 109 |
} |
| 110 |
} |