/* If ch is nil, a new Connection and Channel will be created, and this publisher will 'own' the connection. A call to close() will close both channel and connection. If ch is provided, then this publisher will reuse the channel and calls to close() will do nothing. */ func newPublisher(serverURI string, cfg *ChannelConfig, ch *amqp.Channel) *Publisher { var conn *amqp.Connection var err error if ch == nil { conn, err = amqp.Dial(serverURI) if err != nil { panic(fmt.Errorf("Failed to connect to RabbitMQ: %v", err)) } ch, err = conn.Channel() if err != nil { panic(fmt.Errorf("Failed to open a channel: %v", err)) } } _, err = ch.QueueDeclare(*cfg.Name, *cfg.Durable, *cfg.AutoDelete, *cfg.Exclusive, false, *cfg.Args) if err != nil { panic(fmt.Errorf("Failed to declare queue %s: %v", cfg.Name, err)) } ch.QueuePurge(*cfg.Name, true) return &Publisher{exch: "", routingKey: *cfg.Name, conn: conn, ch: ch, typeTag: *cfg.TypeTag} }
func MakeQueue(c *amqp.Channel) amqp.Queue { q, err := c.QueueDeclare("stress-test-exchange", true, false, false, false, nil) if err != nil { log.Fatal(err) } return q }
func MakeQueue(c *amqp.Channel) amqp.Queue { q, err2 := c.QueueDeclare("stress-test-queue", true, false, false, false, nil) if err2 != nil { panic(err2) } return q }
func SendEmail(req *http.Request, mail Email, r render.Render, ch *amqp.Channel) { msg, _ := json.Marshal(mail) q, _ := ch.QueueDeclare( "email", // name true, // durable false, // delete when usused false, // exclusive false, // no-wait nil, // arguments ) err := ch.Publish( "", // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "json/plain", Body: []byte(msg), }) if err == nil { r.JSON(http.StatusOK, map[string]interface{}{"Email": mail}) } else { r.JSON(http.StatusInternalServerError, err) } }
func MakeQueue(c *amqp.Channel, queueName string, queueDurability bool) amqp.Queue { q, err2 := c.QueueDeclare(queueName, queueDurability, false, false, false, nil) if err2 != nil { panic(err2) } return q }
// 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 declareQueue(ch *amqp.Channel, name string) (amqp.Queue, error) { durable := false autoDelete := true exclusive := false noWait := false var args amqp.Table return ch.QueueDeclare(name, durable, autoDelete, exclusive, noWait, args) }
// DeclareQueue returns a decleared queue func (r Rabbit) DeclareQueue(ch *amqp.Channel, queueName string) (amqp.Queue, error) { qd, err := ch.QueueDeclare( queueName, r.Durable, r.Delete, r.Exclusive, r.NoWait, r.Arguments, ) return qd, err }
func Queue(name string, ch *amqp.Channel) (*amqp.Queue, error) { q, err := ch.QueueDeclare( name, // name true, // durable false, // delete when usused false, // exclusive false, // no-wait nil, // arguments ) return &q, 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 }
func ensureNonZeroMessageRate(ch *amqp.Channel) { for i := 0; i < 2000; i++ { q, _ := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, nil) ch.Publish("", q.Name, false, false, amqp.Publishing{Body: []byte("")}) } }
func GetQueue(name string, ch *amqp.Channel) *amqp.Queue { q, err := ch.QueueDeclare( name, // q name false, // durable false, // autoDelete false, // exclusive false, // noWait nil, // args ) failOnError(err, "Failed to declare a queue") return &q }
// 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 publish(ch *amqp.Channel, q amqp.Queue, c <-chan string) { for { connectionStatus := <-c if connectionStatus == "lost" { for i := 0; i < 40; i++ { conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/") if err != nil { fmt.Println("Failed to connect to RabbitMQ") time.Sleep(1 * time.Second) continue } else { defer conn.Close() } } } if connectionStatus == "ok" { conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/") if err != nil { fmt.Println("Failed to connect to RabbitMQ") time.Sleep(1 * time.Second) continue } else { defer conn.Close() } ch, _ := conn.Channel() defer ch.Close() body := bodyFrom(os.Args) q, err := ch.QueueDeclare( "task_queue", // name true, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) err = ch.Publish( "", // exchange q.Name, // routing key false, // mandatory false, amqp.Publishing{ DeliveryMode: amqp.Persistent, ContentType: "text/plain", Body: []byte(body), }) if err == nil { log.Printf(" [x] Sent %s", body) } } time.Sleep(1 * time.Second) } }
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 }
// declareQueue declares a queue - a buffer for messages. func declareQueue(name string, ch *amqp.Channel) amqp.Queue { q, err := ch.QueueDeclare( name, // name false, // durable false, // delete when usused false, // exclusive false, // noWait nil, // arguments ) if err != nil { log.Fatalf("Failed to declare a queue: %s", err) } return q }
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 } } }
func (d *DefaultWorker) 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") } prefetch, ok := cfg.QueueConfig["prefetch"].(int) if !ok || prefetch < 1 { prefetch = 1 } q, err := ch.QueueDeclare( queueName, // name true, // durable false, // delete when unused 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 } 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 }
func GetQueue(name string, ch *amqp.Channel, autoDelete bool) *amqp.Queue { q, err := ch.QueueDeclare( name, // name string, false, // durable bool, autoDelete, // autoDelete bool false, // exclusive bool, false, // noWait bool, nil) // args amqp.Table if err != nil { failOnError(err, "Failed to declare queue") } else { log.Printf("Declared [%s] queue successfully", q.Name) } return &q }
// 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 (rb *AmqpBeat) consumeIntoStream(stream chan<- *AmqpEvent, ch *amqp.Channel, c *ChannelConfig, wg *sync.WaitGroup) { defer wg.Done() err := ch.Qos(*c.QosPrefetchCount, 0, false) if err != nil { logp.Err("Failed to set QoS on queue '%s': %v", *c.Name, err) } _, err = ch.QueueDeclare(*c.Name, *c.Durable, *c.AutoDelete, false, false, *c.Args) if err != nil { logp.Err("Failed to declare queue '%s': %v", *c.Name, err) return } q, err := ch.Consume(*c.Name, "", false, false, false, false, *c.Args) if err != nil { logp.Err("Failed to consume queue %s: %v", *c.Name, err) return } mName := fmt.Sprintf("consumer.%s", *c.Name) var msgCount int64 for { select { case d := <-q: evt, err := rb.newAmqpEvent(&d, c.TypeTag, c.TsField, c.TsFormat) msgCount++ logp.Debug("flow", "Consumed %d into %s", msgCount, *c.Name) if err != nil { logp.Warn("failed to build event for delivery, will be Nacked. (delivery = %v) (error = %v)", d, err) d.Nack(false, true) } stream <- evt rb.metrics <- &metric{name: mName, value: msgCount} case <-rb.stop: logp.Info("Consumer '%s' is stopping...", *c.Name) return } } logp.Info("Consumer '%s' has stopped.") }
func NewQueue(ch *amqp.Channel, name string, options ...func(*Queue) error) (*Queue, error) { q := Queue{Name: name, readPump: make(chan Message), writePump: make(chan Message)} for _, option := range options { option(&q) } mq, err := ch.QueueDeclare( q.Name, // name q.durable, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) q.Key = mq.Name return &q, err }
func ExportEmail(req *http.Request, mail MongoExport, r render.Render, ch *amqp.Channel, db *mgo.Database) { appid := req.Header.Get("Application-Id") if appid == "" { r.JSON(handlers.HttpErr(http.StatusNotFound, "insert to Application-Id")) return } mail.Collection = handlers.CollectionTable(mail.Collection, appid) if count, err := db.C(mail.Collection).Find(nil).Count(); err != nil || count == 0 { r.JSON(http.StatusNotFound, map[string]interface{}{"Export": "Not Found Collection"}) return } msg, _ := json.Marshal(mail) q, _ := ch.QueueDeclare( config.NAMESPACE+":"+"export", // name true, // durable false, // delete when usused false, // exclusive false, // no-wait nil, // arguments ) err := ch.Publish( //RabbitMQ에 큐가 생성되어있어야 들어감. "", // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "json/plain", Body: []byte(msg), }) if err == nil { r.JSON(http.StatusOK, map[string]interface{}{"Export": mail}) } else { r.JSON(http.StatusInternalServerError, err) } }
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 (que *TSQueue) Channel(conn *amqp.Connection) *amqp.Channel { var err error var ch *amqp.Channel ch, err = conn.Channel() if que.Acknowledge { ch.Confirm(false) que.Ack, que.NAck = ch.NotifyConfirm(make(chan uint64, 1), make(chan uint64, 1)) } Fail(err, "Failed to open a channel") _, err = ch.QueueDeclare( que.Name, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) Fail(err, "Failed to declare a queue") return ch }
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 (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 }
// 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, } } } }