func main() { flag.Parse() showUsageAndStatus() // Construct new client with the flag url // and default backoff policy cli := cony.NewClient( cony.URL(*url), cony.Backoff(cony.DefaultBackoff), ) // Declarations // The queue name will be supplied by the AMQP server que := &cony.Queue{ AutoDelete: true, Name: "myQueue", } exc := cony.Exchange{ Name: "myExc", Kind: "fanout", AutoDelete: true, } bnd := cony.Binding{ Queue: que, Exchange: exc, Key: "pubSub", } cli.Declare([]cony.Declaration{ cony.DeclareQueue(que), cony.DeclareExchange(exc), cony.DeclareBinding(bnd), }) // Declare and register a consumer cns := cony.NewConsumer( que, cony.AutoAck(), // Auto sign the deliveries ) cli.Consume(cns) for cli.Loop() { select { case msg := <-cns.Deliveries(): log.Printf("Received body: %q\n", msg.Body) // If when we built the consumer we didn't use // the "cony.AutoAck()" option this is where we'd // have to call the "amqp.Deliveries" methods "Ack", // "Nack", "Reject" // // msg.Ack(false) // msg.Nack(false) // msg.Reject(false) case err := <-cns.Errors(): fmt.Printf("Consumer error: %v\n", err) case err := <-cli.Errors(): fmt.Printf("Client error: %v\n", err) } } }
func Example() { client := cony.NewClient(cony.URL(os.Getenv("AMQP_URL")), cony.Backoff(cony.DefaultBackoff)) q := &cony.Queue{ Name: "", // autogenerated queue name AutoDelete: true, } exchange := cony.Exchange{ Name: "amq.topic", Durable: true, } b := cony.Binding{ Queue: q, Exchange: exchange, Key: "something.#", } // wrap all declarations and save into slice declarations := []cony.Declaration{ cony.DeclareQueue(q), cony.DeclareExchange(exchange), cony.DeclareBinding(b), } // declare consumer consumer := cony.NewConsumer(q, cony.Qos(10), cony.AutoTag(), cony.AutoAck(), ) // declare publisher publisher := cony.NewPublisher(exchange.Name, "ololo.key", cony.PublishingTemplate(amqp.Publishing{ ContentType: "application/json", AppId: "app1", }), // template amqp.Publising ) // let client know about declarations client.Declare(declarations) // let client know about consumers/publishers client.Consume(consumer) client.Publish(publisher) clientErrs := client.Errors() deliveries := consumer.Deliveries() consumerErrs := consumer.Errors() // connect, reconnect, or exit loop // run network operations such as: // queue, exchange, bidning, consumers declarations for client.Loop() { select { case msg := <-deliveries: log.Println(msg) msg.Ack(false) publisher.Write([]byte("ololo reply")) case err := <-consumerErrs: log.Println("CONSUMER ERROR: ", err) case err := <-clientErrs: log.Println("CLIENT ERROR: ", err) client.Close() } } }