func connect(conn *amqp.Connection, exchange string, wg *sync.WaitGroup) { ch, err := conn.Channel() utils.FailOnError(err, "Failed to open a channel") defer conn.Close() err = ch.ExchangeDeclare( exchange, // name "direct", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments ) utils.FailOnError(err, "Failed to declare an exchange") keys := []string{"create", "update"} for { for _, key := range keys { go connectKey(ch, key, exchange) } log.Printf("Break \n \n") time.Sleep(40 * time.Second) } }
func receive(conn *amqp.Connection, exchange string, key string, wg *sync.WaitGroup) { ch, err := conn.Channel() utils.FailOnError(err, "Failed to open a channel") defer ch.Close() err = ch.ExchangeDeclare( exchange, // name "direct", // type true, // durable false, // auto-deleted false, // internal false, // no-wait nil, // arguments ) utils.FailOnError(err, "Failed to declare an exchange") q, err := ch.QueueDeclare( "", // name false, // durable false, // delete when usused true, // exclusive false, // no-wait nil, // arguments ) utils.FailOnError(err, "Failed to declare a queue") err = ch.QueueBind( q.Name, // queue name key, // routing key exchange, // exchange false, nil) utils.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 ) utils.FailOnError(err, "Failed to register a consumer") forever := make(chan bool) go func() { for msg := range msgs { log.Printf(" [i] %s", key) go processMessage(msg.Body) } }() log.Printf(" [*] Waiting for logs. To exit press CTRL+C") <-forever }
func Getstuff(message models.TimelineEvent) { db, err := utils.GetPostgreSqlDB() utils.FailOnError(err, "Failed to open sql connection") defer db.Close() // values := []interface{"134d91d8-66ee-1cec-9e08-09b6ef0398af", "1", "2015-11-20 00:00:00", "1", "FourthEvent", "Youre the man", "ABABABABABB"} err = StoreTimelineEvent(db, message) if err != nil { fmt.Fprintf(os.Stdout, "Error:%s\n", err) } else { fmt.Fprintf(os.Stdout, "Timeline Event Added\n") } }
func connectKey(ch *amqp.Channel, key string, exchange string) { body := utils.RandomTimelineEvent() bodyJSON, err := json.Marshal(body) err = ch.Publish( exchange, // exchange key, // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "application/json", Body: []byte(bodyJSON), }) utils.FailOnError(err, "failed to publish a message") log.Printf(" [x] Sent %s", body) }
func main() { conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/") utils.FailOnError(err, "Failed to connect to RabbitMQ") defer conn.Close() var wg sync.WaitGroup exchanges := []string{ "communication", "deal", "task", "contact", "account", "address", } for _, exchange := range exchanges { wg.Add(1) go connect(conn, exchange, &wg) } wg.Wait() }
func main() { conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/") utils.FailOnError(err, "Failed to connect to RabbitMQ") defer conn.Close() var wg sync.WaitGroup exchanges := make(map[string][]string) exchanges["communication"] = append(exchanges["communication"], "create", "update") exchanges["deal"] = append(exchanges["deal"], "create", "update") exchanges["task"] = append(exchanges["task"], "create", "update") exchanges["contact"] = append(exchanges["contact"], "create", "update") exchanges["account"] = append(exchanges["account"], "create", "update") exchanges["address"] = append(exchanges["address"], "create", "update") for exchange, keys := range exchanges { for _, key := range keys { wg.Add(1) go receive(conn, exchange, key, &wg) } } wg.Wait() }