func main() { conn, ch := qutils.GetChannel(url) defer conn.Close() defer ch.Close() msgs, err := ch.Consume( qutils.PersistReadingsQueue, //queue string, "", //consumer string, false, //autoAck bool, true, //exclusive bool, false, //noLocal bool, false, //noWait bool, nil) //args amqp.Table) if err != nil { log.Fatalln("Failed to get access to messages") } for msg := range msgs { buf := bytes.NewReader(msg.Body) dec := gob.NewDecoder(buf) sd := &dto.SensorMessage{} dec.Decode(sd) err := datamanager.SaveReading(sd) if err != nil { log.Printf("Failed to save reading from sensor %v. Error: %s", sd.Name, err.Error()) } else { msg.Ack(false) } } }
func main() { flag.Parse() conn, ch := qutils.GetChannel(url) defer conn.Close() defer ch.Close() dataQueue := qutils.GetQueue(*name, ch, false) publishQueueName(ch) discoveryQueue := qutils.GetQueue("", ch, true) ch.QueueBind( discoveryQueue.Name, "", qutils.SensorDiscoveryExchange, false, nil) go listenForDiscoveryRequests(discoveryQueue.Name, ch) dur, _ := time.ParseDuration(strconv.Itoa(1000/int(*freq)) + "ms") signal := time.Tick(dur) buf := new(bytes.Buffer) enc := gob.NewEncoder(buf) for range signal { calcValue() reading := dto.SensorMessage{ Name: *name, Value: value, Timestamp: time.Now(), } buf.Reset() enc = gob.NewEncoder(buf) enc.Encode(reading) msg := amqp.Publishing{ Body: buf.Bytes(), } ch.Publish( "", // exchange name dataQueue.Name, // key string false, // mandatory bool false, // immediate bool msg) // msg amqp.Publish log.Printf("Reading sent. Value: %v\n", value) } }
func NewQueueListener(ea *EventAggregator) *QueueListener { ql := QueueListener{ sources: make(map[string]<-chan amqp.Delivery), ea: ea, } ql.conn, ql.ch = qutils.GetChannel(url) return &ql }
func NewDatabaseConsumer(er EventRaiser) *DatabaseConsumer { dc := DatabaseConsumer{ er: er, } dc.conn, dc.ch = qutils.GetChannel(url) dc.queue = qutils.GetQueue(qutils.PersistReadingsQueue, dc.ch, false) dc.er.AddListener("DataSourceDiscovered", func(eventData interface{}) { dc.SubscribeToDataEvent(eventData.(string)) }) return &dc }