// Connects to the message queue, opens a channel, declares a queue func (amqpBroker *AMQPBroker) open() (*amqp.Connection, *amqp.Channel, amqp.Queue, <-chan amqp.Confirmation, error) { var err error var channel *amqp.Channel var queue amqp.Queue if conn == nil { once.Do(amqpBroker.connect) } if conn == nil { return conn, channel, queue, nil, fmt.Errorf("Can't connect to the server") } channel, err = conn.Channel() if err != nil { fmt.Printf("Channel: %s\n", err) } if err := channel.ExchangeDeclare( amqpBroker.config.Exchange, // name of the exchange amqpBroker.config.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ); err != nil { fmt.Printf("Exchange: %s\n", err) } queue, err = channel.QueueDeclare( amqpBroker.config.DefaultQueue, // name true, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { fmt.Printf("Queue Declare: %s\n", err) } if err := channel.QueueBind( queue.Name, // name of the queue amqpBroker.config.BindingKey, // binding key amqpBroker.config.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { fmt.Printf("Queue Bind: %s\n", err) } confirmsChan := make(chan amqp.Confirmation, 1) // Enable publish confirmations if err := channel.Confirm(false); err != nil { close(confirmsChan) fmt.Printf("Channel could not be put into confirm mode: %s\n", err) } return conn, channel, queue, channel.NotifyPublish(confirmsChan), nil }
func QueueBind(q *amqp.Queue, exchange string, ch *amqp.Channel) error { return ch.QueueBind( q.Name, // queue name "", // routing key exchange, // exchange false, nil) }
// Bind the queue to an exchange func (r Rabbit) Bind(ch *amqp.Channel, queueName string, rk string) error { log.Infof("Binding queue %s to exchange %s with routing key %s with %s exchange ", queueName, r.Exchange, rk, r.ExchangeType) err := ch.QueueBind( queueName, rk, r.Exchange, r.NoWait, r.Arguments) return err }
// Ensures the given queue exists and is bound to the exchange func (r *Relay) declareQueue(ch *amqp.Channel, name string) error { // Declare the queue if _, err := ch.QueueDeclare(name, true, false, false, false, nil); err != nil { return fmt.Errorf("Failed to declare queue '%s'! Got: %s", name, err) } // Bind the queue to the exchange if err := ch.QueueBind(name, name, r.conf.Exchange, false, nil); err != nil { return fmt.Errorf("Failed to bind queue '%s'! Got: %s", name, err) } return nil }
// bindReceiveQueue binds the amqpReceiveExchange name to a queue. func bindReceiveQueue(ch *amqp.Channel, q amqp.Queue) { err := ch.QueueBind( q.Name, // queue name "", // routing key amqpReceiveExchange, // exchange false, nil) if err != nil { log.Fatalf("Failed to bind a queue: %s", err) } }
// Connects to the message queue, opens a channel, declares a queue func open(cnf *config.Config) (*amqp.Connection, *amqp.Channel, amqp.Queue, error) { var conn *amqp.Connection var channel *amqp.Channel var queue amqp.Queue var err error conn, err = amqp.Dial(cnf.Broker) if err != nil { return conn, channel, queue, fmt.Errorf("Dial: %s", err) } channel, err = conn.Channel() if err != nil { return conn, channel, queue, fmt.Errorf("Channel: %s", err) } if err := channel.ExchangeDeclare( cnf.Exchange, // name of the exchange cnf.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, fmt.Errorf("Exchange: %s", err) } queue, err = channel.QueueDeclare( cnf.DefaultQueue, // name true, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { return conn, channel, queue, fmt.Errorf("Queue Declare: %s", err) } if err := channel.QueueBind( queue.Name, // name of the queue cnf.BindingKey, // binding key cnf.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, fmt.Errorf("Queue Bind: %s", err) } return conn, channel, queue, nil }
func sineMain(ch *amqp.Channel) { go sniffy() q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name "", // routing key "bpm", // exchange false, nil) failOnError(err, "Failed to bind a queue") msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "Failed to register a consumer") portaudio.Initialize() defer portaudio.Terminate() //s := newStereoSine(256, 320, sampleRate) s := newStereoSine(220, 220, sampleRate) defer s.Close() chk(s.Start()) defer s.Stop() for m := range msgs { var bm BpmMsg err := json.Unmarshal(m.Body, &bm) if err != nil { fmt.Println("blah", err) } bpm = bm.Bpm } }
func Consume(ch *amqp.Channel, b *bind) (<-chan amqp.Delivery, error) { var err error q, err := ch.QueueDeclare(b.name, true, false, false, false, nil) if err != nil { Log.Error("queue declare err: %v", err) } err = ch.QueueBind(b.name, b.key, b.exchange, false, nil) if err != nil { Log.Error("queue bind err: %v", err) } tasks, err := ch.Consume(q.Name, b.tag, false, false, false, false, nil) return tasks, err }
func (s *Server) consumer() { var ( ch *amqp.Channel err error q amqp.Queue consumer <-chan amqp.Delivery ) qArgs := amqp.Table{} if *ttl > 0 { qArgs["x-message-ttl"] = int64(*ttl) } if s.Ttl > 0 { qArgs["x-message-ttl"] = int64(s.Ttl) } for s.alive { if err != nil { ch = nil log.Println(err) err = nil time.Sleep(*backoff) continue } if ch == nil { ch, err = s.dial() } if ch != nil { q, err = ch.QueueDeclare(*queue, false, true, true, false, qArgs) if err != nil { continue } err = ch.QueueBind(q.Name, s.Source, s.Source, false, nil) if err != nil { continue } consumer, err = ch.Consume(q.Name, "mqslice", true, true, false, false, nil) if err != nil { continue } for elt := range consumer { s.ch <- elt.Body } ch.Close() ch = nil } } }
// Ensures the given queue exists and is bound to the exchange func (r *Relay) declareQueue(ch *amqp.Channel, name string, routingKey string) error { var args amqp.Table if r.conf.QueueTTL > 0 { args = make(map[string]interface{}) msec := int32(r.conf.QueueTTL / time.Millisecond) args["x-expires"] = msec } // Declare the queue if _, err := ch.QueueDeclare(name, true, false, false, false, args); err != nil { return fmt.Errorf("Failed to declare queue '%s'! Got: %s", name, err) } // Bind the queue to the exchange if err := ch.QueueBind(name, routingKey, r.conf.Exchange, false, nil); err != nil { return fmt.Errorf("Failed to bind queue '%s'! Got: %s", name, err) } return nil }
func msgListener(ch *amqp.Channel) { q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name "", // routing key "updateMsgs", // exchange false, nil) failOnError(err, "Failed to bind a queue") msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "Failed to register a consumer") for d := range msgs { var m UpdateMsg err := json.Unmarshal(d.Body, &m) if err != nil { fmt.Println("blah", err) } if m.Name == "bpm" { fmt.Println("Setting BPM to", m.Value) bpm = m.Value } } }
func createQueue(channel *amqp.Channel, queueName string) { err := channel.ExchangeDeclare( queueName, // name of the exchange "direct", // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ) if err != nil { log.Fatal("Couldn't declare exchange: %s", err) } queue, err := channel.QueueDeclare( queueName, // name of the queue true, // durable false, // delete when usused false, // exclusive false, // noWait nil, // arguments ) if err != nil { log.Fatal("Couldn't declare queue: %s", err) } err = channel.QueueBind( queue.Name, // name of the queue queueName, // bindingKey queueName, // sourceExchange false, // noWait nil, // arguments ) if err != nil { log.Fatal("Couldn't bind queue: %s", err) } }
func amqpCreateAndBindQueue(ch *amqp.Channel, filter string) (amqp.Queue, error) { q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, // no-wait nil, // arguments ) if err != nil { return q, err } err = ch.QueueBind( q.Name, // queue name filter, // routing key "events", // exchange false, nil, ) if err != nil { return q, err } return q, err }
// Connects to the message queue, opens a channel, declares a queue func open(taskUUID string, cnf *config.Config) (*amqp.Connection, *amqp.Channel, amqp.Queue, error) { var conn *amqp.Connection var channel *amqp.Channel var queue amqp.Queue var err error conn, err = amqp.Dial(cnf.Broker) if err != nil { return conn, channel, queue, fmt.Errorf("Dial: %s", err) } channel, err = conn.Channel() if err != nil { return conn, channel, queue, fmt.Errorf("Channel: %s", err) } err = channel.ExchangeDeclare( cnf.Exchange, // name of the exchange cnf.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ) if err != nil { return conn, channel, queue, fmt.Errorf("Exchange: %s", err) } resultsExpireIn := cnf.ResultsExpireIn * 1000 if resultsExpireIn == 0 { // // expire results after 1 hour by default resultsExpireIn = 3600 * 1000 } arguments := amqp.Table{ "x-message-ttl": int32(resultsExpireIn), } queue, err = channel.QueueDeclare( taskUUID, // name false, // durable true, // delete when unused false, // exclusive false, // no-wait arguments, ) if err != nil { return conn, channel, queue, fmt.Errorf("Queue Declare: %s", err) } if err := channel.QueueBind( queue.Name, // name of the queue taskUUID, // binding key cnf.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, fmt.Errorf("Queue Bind: %s", err) } return conn, channel, queue, nil }
func NewConsumer(consumerName string, queueName string, exchange Exchange, config *Config) (consumer *Consumer, err error) { if consumerName == "" { err = errors.New("Must give the consumer a name") return } if queueName == "" { err = errors.New("Must give the queue name.") return } if len(queueName) > 255 { err = errors.New("Must give a queue name that contains 1-255 characters.") return } if err = exchange.Validate(); err != nil { return } if config == nil { config = NewConfig() } conn, err := amqp.Dial(config.GetConnectionString()) if err != nil { return } //setup the channel var channel *amqp.Channel if channel, err = conn.Channel(); err != nil { return } //setup the exchange if err = channel.ExchangeDeclare( exchange.Name, //exchange name exchange.Type, //exchange type true, //durable false, //remove when complete false, //internal false, //noWait nil, //arguments ); err != nil { return } //setup the queue var queue amqp.Queue if queue, err = channel.QueueDeclare( queueName, // name of the queue true, // durable false, // delete when usused false, // exclusive false, // noWait nil, // arguments ); err != nil { return } if err = channel.QueueBind( queue.Name, //queue name exchange.RoutingKey, //routing key ("binding key") exchange.Name, //exchange (source) false, //noWait nil, //arguments ); err != nil { return } //setup the deliverables var messages <-chan amqp.Delivery if messages, err = channel.Consume( queue.Name, //queue name consumerName, //consumer name false, //auto acknowledge false, //exclusive false, //not local false, //no wait nil, //arguments ); err != nil { return } consumer = new(Consumer) consumer.Name = consumerName consumer.config = config consumer.conn = conn consumer.channel = channel consumer.Exchange = exchange consumer.incomingMessages = messages return }
func (d *Topic) Consume(ch *amqp.Channel, cfg config.EndpointConfig) (<-chan amqp.Delivery, error) { queueName, ok := cfg.QueueConfig["queuename"].(string) if !ok { return nil, fmt.Errorf("unable to parse queuename from config") } topicName, ok := cfg.QueueConfig["topicname"].(string) if !ok { return nil, fmt.Errorf("unable to parse topicname from config") } exchangeName, ok := cfg.QueueConfig["exchangename"].(string) if !ok { return nil, fmt.Errorf("unable to parse exchangename from config") } prefetch, ok := cfg.QueueConfig["prefetch"].(int) if !ok || prefetch < 1 { prefetch = 1 } err := ch.ExchangeDeclare( exchangeName, // name "topic", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments ) if err != nil { return nil, err } q, err := ch.QueueDeclare( queueName, // name true, // durable false, // delete when usused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { return nil, err } err = ch.Qos( prefetch, // prefetch count 0, // prefetch size false, // global ) if err != nil { return nil, err } err = ch.QueueBind( q.Name, // queue name topicName, // routing key exchangeName, // exchange false, nil, ) if err != nil { return nil, err } msgs, err := ch.Consume( q.Name, // queue "", // consumer false, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) if err != nil { return nil, err } return msgs, nil }
// connects to an AMQP topic and emits each message into streamtools. func (b *FromAMQP) Run() { var err error var conn *amqp.Connection var amqp_chan *amqp.Channel toOut := make(blocks.MsgChan) toError := make(chan error) host := "localhost" port := "5672" username := "******" password := "******" routingkey := "#" exchange := "amq.topic" exchange_type := "topic" for { select { case msg := <-toOut: b.out <- msg case err := <-toError: b.Error(err) case ruleI := <-b.inrule: rule := ruleI.(map[string]interface{}) routingkey, err = util.ParseString(rule, "RoutingKey") if err != nil { b.Error(err) continue } exchange, err = util.ParseString(rule, "Exchange") if err != nil { b.Error(err) continue } exchange_type, err = util.ParseString(rule, "ExchangeType") if err != nil { b.Error(err) continue } host, err = util.ParseString(rule, "Host") if err != nil { b.Error(err) continue } port, err = util.ParseString(rule, "Port") if err != nil { b.Error(err) continue } username, err = util.ParseString(rule, "Username") if err != nil { b.Error(err) continue } password, err = util.ParseString(rule, "Password") if err != nil { b.Error(err) continue } conn, err = amqp.Dial("amqp://" + username + ":" + password + "@" + host + ":" + port + "/") if err != nil { b.Error(err) continue } amqp_chan, err = conn.Channel() if err != nil { b.Error(err) continue } err = amqp_chan.ExchangeDeclare( exchange, // name exchange_type, // type true, // durable false, // auto-deleted false, // internal false, // noWait nil, // arguments ) if err != nil { b.Error(err) continue } queue, err := amqp_chan.QueueDeclare( "", // name false, // durable true, // delete when unused false, // exclusive false, // noWait nil, // arguments ) if err != nil { b.Error(err) continue } err = amqp_chan.QueueBind( queue.Name, // queue name routingkey, // routing key exchange, // exchange false, nil, ) if err != nil { b.Error(err) continue } deliveries, err := amqp_chan.Consume( queue.Name, // name "", // consumerTag true, // noAck false, // exclusive false, // noLocal false, // noWait nil, // arguments ) if err != nil { b.Error(err) continue } h := readWriteAMQPHandler{toOut, toError} go h.handle(deliveries) case <-b.quit: if amqp_chan != nil { amqp_chan.Close() } if conn != nil { conn.Close() } return case c := <-b.queryrule: c <- map[string]interface{}{ "Host": host, "Port": port, "Username": username, "Password": password, "Exchange": exchange, "ExchangeType": exchange_type, "RoutingKey": routingkey, } } } }
// Connects to the message queue, opens a channel, declares a queue func (amqpBackend *AMQPBackend) open(taskUUID string) (*amqp.Connection, *amqp.Channel, amqp.Queue, <-chan amqp.Confirmation, error) { var conn *amqp.Connection var channel *amqp.Channel var queue amqp.Queue var err error conn, err = amqp.Dial(amqpBackend.config.ResultBackend) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Dial: %s", err) } channel, err = conn.Channel() if err != nil { return conn, channel, queue, nil, fmt.Errorf("Channel: %s", err) } err = channel.ExchangeDeclare( amqpBackend.config.Exchange, // name of the exchange amqpBackend.config.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Exchange: %s", err) } arguments := amqp.Table{ "x-message-ttl": int32(amqpBackend.getExpiresIn()), } queue, err = channel.QueueDeclare( taskUUID, // name false, // durable true, // delete when unused false, // exclusive false, // no-wait arguments, ) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Declare: %s", err) } if err := channel.QueueBind( queue.Name, // name of the queue taskUUID, // binding key amqpBackend.config.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Bind: %s", err) } confirmsChan := make(chan amqp.Confirmation, 1) // Enable publish confirmations if err := channel.Confirm(false); err != nil { close(confirmsChan) return conn, channel, queue, nil, fmt.Errorf("Channel could not be put into confirm mode: %s", err) } return conn, channel, queue, channel.NotifyPublish(confirmsChan), nil }
// Connects to the message queue, opens a channel, declares a queue func (amqpBackend *AMQPBackend) open(taskUUID string) (*amqp.Connection, *amqp.Channel, amqp.Queue, <-chan amqp.Confirmation, error) { var ( conn *amqp.Connection channel *amqp.Channel queue amqp.Queue err error ) // Connect // From amqp docs: DialTLS will use the provided tls.Config when it encounters an amqps:// scheme // and will dial a plain connection when it encounters an amqp:// scheme. conn, err = amqp.DialTLS(amqpBackend.config.Broker, amqpBackend.config.TLSConfig) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Dial: %s", err) } // Open a channel channel, err = conn.Channel() if err != nil { return conn, channel, queue, nil, fmt.Errorf("Channel: %s", err) } // Declare an exchange err = channel.ExchangeDeclare( amqpBackend.config.Exchange, // name of the exchange amqpBackend.config.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Exchange Declare: %s", err) } // Declare a queue arguments := amqp.Table{ "x-message-ttl": int32(amqpBackend.getExpiresIn()), } queue, err = channel.QueueDeclare( taskUUID, // name false, // durable true, // delete when unused false, // exclusive false, // no-wait arguments, ) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Declare: %s", err) } // Bind the queue if err := channel.QueueBind( queue.Name, // name of the queue taskUUID, // binding key amqpBackend.config.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Bind: %s", err) } // Enable publish confirmations if err := channel.Confirm(false); err != nil { return conn, channel, queue, nil, fmt.Errorf("Channel could not be put into confirm mode: %s", err) } return conn, channel, queue, channel.NotifyPublish(make(chan amqp.Confirmation, 1)), nil }
func clientMain(ch *amqp.Channel) { q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name "", // routing key "bpm", // exchange false, nil) failOnError(err, "Failed to bind a queue") msgs, err := ch.Consume( q.Name, // queue "", // consumer true, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "Failed to register a consumer") for d := range msgs { var bm BpmMsg err := json.Unmarshal(d.Body, &bm) if err != nil { fmt.Println("blah", err) } fmt.Println(bm) // if bm.TickCounter%4 == 1 { if bm.MicroTick == 1 { fmt.Println("KICK Got one Modulo 4!", bm.TickCounter) //go playrrr(kick, Soxfilter{}) //if rand.Intn(10) > 2 { go playrrr(kick, Soxfilter{Effect: "pitch", Val: strconv.Itoa(int(math.Pow(float64(bm.TickCounter%1000), 3.0)) % 1000)}) //} else { // fmt.Println("RANDY MISSS!") //} } //if bm.TickCounter%4 == 0 { // fmt.Println("SNARE Got one Modulo 3!", bm.TickCounter) // //go playrrr(snare, Soxfilter{}) // if rand.Intn(10) > 1 { // //go playrrr(snare, Soxfilter{Effect: "pitch", Val: strconv.Itoa(int(math.Pow(float64(bm.TickCounter%1000), 3.0)) % 1000)}) // go playrrr(snare, Soxfilter{}) // } //} //if bm.TickCounter%2 == 0 { // fmt.Println("HAT Got one Modulo 1!", bm.TickCounter) // // go playrrr(hat + " pitch " + strconv.Itoa(bm.TickCounter%1000)) // if rand.Intn(10) > 4 { // go playrrr(hat, Soxfilter{Effect: "pitch", Val: strconv.Itoa(int(math.Pow(float64(bm.TickCounter%1000), 3.0)) % 1000)}) // } //} } }
// Connects to the message queue, opens a channel, declares a queue func (amqpBroker *AMQPBroker) open() (*amqp.Connection, *amqp.Channel, amqp.Queue, <-chan amqp.Confirmation, error) { var ( conn *amqp.Connection channel *amqp.Channel queue amqp.Queue err error ) // Connect conn, err = amqp.Dial(amqpBroker.config.Broker) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Dial: %s", err) } // Open a channel channel, err = conn.Channel() if err != nil { return conn, channel, queue, nil, fmt.Errorf("Channel: %s", err) } // Declare an exchange if err := channel.ExchangeDeclare( amqpBroker.config.Exchange, // name of the exchange amqpBroker.config.ExchangeType, // type true, // durable false, // delete when complete false, // internal false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, nil, fmt.Errorf("Exchange Declare: %s", err) } // Declare a queue queue, err = channel.QueueDeclare( amqpBroker.config.DefaultQueue, // name true, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Declare: %s", err) } // Bind the queue if err := channel.QueueBind( queue.Name, // name of the queue amqpBroker.config.BindingKey, // binding key amqpBroker.config.Exchange, // source exchange false, // noWait nil, // arguments ); err != nil { return conn, channel, queue, nil, fmt.Errorf("Queue Bind: %s", err) } // Enable publish confirmations if err := channel.Confirm(false); err != nil { return conn, channel, queue, nil, fmt.Errorf("Channel could not be put into confirm mode: %s", err) } return conn, channel, queue, channel.NotifyPublish(make(chan amqp.Confirmation, 1)), nil }