コード例 #1
0
ファイル: sqsway.go プロジェクト: LeeQY/sqsway
func (q *QueueInfo) ReceiveMessage(h ReceiveHandle) {
	var input sqs.ReceiveMessageInput
	input.QueueUrl = q.Queue
	input.WaitTimeSeconds = &waitTime
	input.MessageAttributeNames = []*string{&valueName}
	input.MaxNumberOfMessages = &maxMsg

	ro, _ := thisSQS.ReceiveMessage(&input)
	l := len(ro.Messages)
	if l > 0 {
		needDele := set.New()
		wg := new(sync.WaitGroup)
		for i := 0; i < l; i++ {
			msg := ro.Messages[i]
			atts := msg.MessageAttributes
			wg.Add(1)
			go h(wg, atts["value"].BinaryValue, needDele, i)
		}
		wg.Wait()

		indexes := set.IntSlice(needDele)
		var entries []*sqs.DeleteMessageBatchRequestEntry
		for _, index := range indexes {
			msg := ro.Messages[index]

			var entry sqs.DeleteMessageBatchRequestEntry
			entry.Id = msg.MessageId
			entry.ReceiptHandle = msg.ReceiptHandle
			entries = append(entries, &entry)
		}

		q.deleteMessage(&entries)
	}
	q.ReceiveMessage(h)
}
コード例 #2
0
// GetMessages returns the parsed messages from SQS if any. If an error
// occurs that error will be returned.
func (q *Queue) GetMessages(waitTimeout int64) ([]Message, error) {
	params := sqs.ReceiveMessageInput{
		QueueUrl: aws.String(q.URL),
	}
	if waitTimeout > 0 {
		params.WaitTimeSeconds = aws.Int64(waitTimeout)
	}
	resp, err := q.Client.ReceiveMessage(&params)
	if err != nil {
		return nil, fmt.Errorf("failed to get messages, %v", err)
	}

	msgs := make([]Message, len(resp.Messages))
	for i, msg := range resp.Messages {
		parsedMsg := Message{}
		if err := json.Unmarshal([]byte(aws.StringValue(msg.Body)), &parsedMsg); err != nil {
			return nil, fmt.Errorf("failed to unmarshal message, %v", err)
		}

		msgs[i] = parsedMsg
	}

	return msgs, nil
}