Exemplo n.º 1
0
func Consume(number int, uri string) {
	for {
		log.Printf("Consumer %d, connecting.\n", number)
		connection := queue.GetConnection(uri)
		defer connection.Close()
		channel, err := connection.Channel()
		if err != nil {
			println(err.Error())
			panic(err.Error())
		}
		// defer channel.Close()

		q := queue.MakeQueue(channel)

		msgs, err := channel.Consume(q.Name, "", true, false, false, false, nil)
		if err != nil {
			panic(err)
		}

		d := <-msgs
		var thisMessage queue.MqMessage
		err = json.Unmarshal(d.Body, &thisMessage)
		if err != nil {
			log.Printf("Error unmarshalling! %s", err.Error())
		}
		log.Printf("Consumer(%d) - Receiver message, age: %s", number, time.Since(thisMessage.TimeNow))
		connection.Close()
	}
	log.Println("done recieving")

}
Exemplo n.º 2
0
func Produce(number int, config ProducerConfig, tasks chan int, messageCount int) {
	for {
		log.Printf("Producer %d, connecting.", number)
		connection := queue.GetConnection(config.Uri)

		channel, err1 := connection.Channel()
		if err1 != nil {
			println(err1.Error())
			panic(err1.Error())
		}
		defer channel.Close()

		if config.WaitForAck {
			channel.Confirm(false)
		}

		ack, nack := channel.NotifyConfirm(make(chan uint64, 1), make(chan uint64, 1))

		q := queue.MakeQueue(channel)

		//
		sequenceNumber, alive := <-tasks

		if !alive {
			log.Println("No more messages to publish. Publisher shutting down!")
			return
		}

		start := time.Now()

		message := &queue.MqMessage{start, sequenceNumber, makeString(config.Bytes)}
		messageJson, _ := json.Marshal(message)
		log.Printf("Producer(%d) - Publishing message: %d", number, sequenceNumber)
		channel.Publish("", q.Name, true, false, amqp.Publishing{
			Headers:         amqp.Table{},
			ContentType:     "text/plain",
			ContentEncoding: "UTF-8",
			Body:            messageJson,
			DeliveryMode:    amqp.Transient,
			Priority:        0,
		},
		)

		confirmOne(ack, nack, config.Quiet, config.WaitForAck)

		if !config.Quiet {
			log.Printf("Producer(%d) - Publish took: %s.\n", number, time.Since(start))
		}
		//
		connection.Close()
	}
}