| 1 |
// This example declares a durable Exchange, and publishes a single message to |
| 2 |
// that Exchange with a given routing key. |
| 3 |
// |
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
|
| 11 |
"github.com/streadway/amqp" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
uri = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI") |
| 16 |
exchangeName = flag.String("exchange", "test-exchange", "Durable AMQP exchange name") |
| 17 |
exchangeType = flag.String("exchange-type", "direct", "Exchange type - direct|fanout|topic|x-custom") |
| 18 |
routingKey = flag.String("key", "test-key", "AMQP routing key") |
| 19 |
body = flag.String("body", "foobar", "Body of message") |
| 20 |
reliable = flag.Bool("reliable", true, "Wait for the publisher confirmation before exiting") |
| 21 |
) |
| 22 |
|
| 23 |
func init() { |
| 24 |
flag.Parse() |
| 25 |
} |
| 26 |
|
| 27 |
func main() { |
| 28 |
if err := publish(*uri, *exchangeName, *exchangeType, *routingKey, *body, *reliable); err != nil { |
| 29 |
log.Fatalf("%s", err) |
| 30 |
} |
| 31 |
log.Printf("published %dB OK", len(*body)) |
| 32 |
} |
| 33 |
|
| 34 |
func publish(amqpURI, exchange, exchangeType, routingKey, body string, reliable bool) error { |
| 35 |
|
| 36 |
// This function dials, connects, declares, publishes, and tears down, |
| 37 |
// all in one go. In a real service, you probably want to maintain a |
| 38 |
// long-lived connection as state, and publish against that. |
| 39 |
|
| 40 |
log.Printf("dialing %q", amqpURI) |
| 41 |
connection, err := amqp.Dial(amqpURI) |
| 42 |
if err != nil { |
| 43 |
return fmt.Errorf("Dial: %s", err) |
| 44 |
} |
| 45 |
defer connection.Close() |
| 46 |
|
| 47 |
log.Printf("got Connection, getting Channel") |
| 48 |
channel, err := connection.Channel() |
| 49 |
if err != nil { |
| 50 |
return fmt.Errorf("Channel: %s", err) |
| 51 |
} |
| 52 |
|
| 53 |
log.Printf("got Channel, declaring %q Exchange (%q)", exchangeType, exchange) |
| 54 |
if err := channel.ExchangeDeclare( |
| 55 |
exchange, // name |
| 56 |
exchangeType, // type |
| 57 |
true, // durable |
| 58 |
false, // auto-deleted |
| 59 |
false, // internal |
| 60 |
false, // noWait |
| 61 |
nil, // arguments |
| 62 |
); err != nil { |
| 63 |
return fmt.Errorf("Exchange Declare: %s", err) |
| 64 |
} |
| 65 |
|
| 66 |
// Reliable publisher confirms require confirm.select support from the |
| 67 |
// connection. |
| 68 |
if reliable { |
| 69 |
log.Printf("enabling publishing confirms.") |
| 70 |
if err := channel.Confirm(false); err != nil { |
| 71 |
return fmt.Errorf("Channel could not be put into confirm mode: %s", err) |
| 72 |
} |
| 73 |
|
| 74 |
confirms := channel.NotifyPublish(make(chan amqp.Confirmation, 1)) |
| 75 |
|
| 76 |
defer confirmOne(confirms) |
| 77 |
} |
| 78 |
|
| 79 |
log.Printf("declared Exchange, publishing %dB body (%q)", len(body), body) |
| 80 |
if err = channel.Publish( |
| 81 |
exchange, // publish to an exchange |
| 82 |
routingKey, // routing to 0 or more queues |
| 83 |
false, // mandatory |
| 84 |
false, // immediate |
| 85 |
amqp.Publishing{ |
| 86 |
Headers: amqp.Table{}, |
| 87 |
ContentType: "text/plain", |
| 88 |
ContentEncoding: "", |
| 89 |
Body: []byte(body), |
| 90 |
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent |
| 91 |
Priority: 0, // 0-9 |
| 92 |
// a bunch of application/implementation-specific fields |
| 93 |
}, |
| 94 |
); err != nil { |
| 95 |
return fmt.Errorf("Exchange Publish: %s", err) |
| 96 |
} |
| 97 |
|
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
// One would typically keep a channel of publishings, a sequence number, and a |
| 102 |
// set of unacknowledged sequence numbers and loop until the publishing channel |
| 103 |
// is closed. |
| 104 |
func confirmOne(confirms <-chan amqp.Confirmation) { |
| 105 |
log.Printf("waiting for confirmation of one publishing") |
| 106 |
|
| 107 |
if confirmed := <-confirms; confirmed.Ack { |
| 108 |
log.Printf("confirmed delivery with delivery tag: %d", confirmed.DeliveryTag) |
| 109 |
} else { |
| 110 |
log.Printf("failed delivery of delivery tag: %d", confirmed.DeliveryTag) |
| 111 |
} |
| 112 |
} |