Ejemplo n.º 1
0
func (p *MessageReceiverMNS) newAliMNSQueue() (queue ali_mns.AliMNSQueue, err error) {

	hostId := ""
	accessKeyId := ""
	accessKeySecret := ""
	queueName := ""

	regUrl := regexp.MustCompile("http://(.*):(.*)@(.*)/(.*)")
	regMatched := regUrl.FindAllStringSubmatch(p.url, -1)

	if len(regMatched) == 1 &&
		len(regMatched[0]) == 5 {
		accessKeyId = regMatched[0][1]
		accessKeySecret = regMatched[0][2]
		hostId = regMatched[0][3]
		queueName = regMatched[0][4]
	}

	client := ali_mns.NewAliMNSClient("http://"+hostId,
		accessKeyId,
		accessKeySecret)

	if client == nil {
		err = ERR_RECEIVER_MNS_CLIENT_IS_NIL.New(errors.Params{"type": p.Type(), "url": p.url})
		return
	}

	queue = ali_mns.NewMNSQueue(queueName, client, p.qpsLimit)

	return
}
Ejemplo n.º 2
0
func NewMNSWriter(config spirit.Map) (w io.WriteCloser, err error) {
	conf := MNSWriterConfig{}
	if err = config.ToObject(&conf); err != nil {
		return
	}

	if conf.Queue == "" {
		err = ErrAliMNSQueueIsEmpty
		return
	}

	if conf.URL == "" {
		err = ErrAliURLIsEmpty
		return
	}

	if conf.AccessKeyId == "" {
		err = ErrAliAccessKeyIdIsEmpty
		return
	}

	if conf.AccessKeySecret == "" {
		err = ErrAliAccessKeySecretIsEmpty
		return
	}

	if conf.Priority == 0 {
		conf.Priority = 8
	}

	if conf.DelaySeconds > 604800 {
		conf.DelaySeconds = 604800
	}

	if conf.DelaySeconds < 0 {
		conf.DelaySeconds = 0
	}

	client := ali_mns.NewAliMNSClient(conf.URL, conf.AccessKeyId, conf.AccessKeySecret)

	if conf.ProxyAddress != "" {
		client.SetProxy(conf.ProxyAddress)
	}

	w = &MNSWriter{conf: conf, client: client}

	return
}
Ejemplo n.º 3
0
func NewMNSReader(config spirit.Map) (r io.ReadCloser, err error) {
	conf := MNSReaderConfig{}
	if err = config.ToObject(&conf); err != nil {
		return
	}

	if conf.Queue == "" {
		err = ErrAliMNSQueueIsEmpty
		return
	}

	if conf.URL == "" {
		err = ErrAliURLIsEmpty
		return
	}

	if conf.AccessKeyId == "" {
		err = ErrAliAccessKeyIdIsEmpty
		return
	}

	if conf.AccessKeySecret == "" {
		err = ErrAliAccessKeySecretIsEmpty
		return
	}

	if conf.MaxReadCount <= 0 || conf.MaxReadCount > 16 {
		conf.MaxReadCount = int(ali_mns.DefaultNumOfMessages)
	}

	if conf.PollingWaitSeconds <= 0 || conf.PollingWaitSeconds > 30 {
		conf.PollingWaitSeconds = int(ali_mns.DefaultTimeout)
	}

	client := ali_mns.NewAliMNSClient(conf.URL, conf.AccessKeyId, conf.AccessKeySecret)

	if conf.ProxyAddress != "" {
		client.SetProxy(conf.ProxyAddress)
	}

	r = &MNSReader{conf: conf, client: client}

	return
}
Ejemplo n.º 4
0
func (p *MNSSender) getMNSClient(queue string) ali_mns.AliMNSQueue {
	p.clientLocker.Lock()
	defer p.clientLocker.Unlock()

	if q, exist := p.mnsClient[queue]; exist {
		return q
	}

	cli := ali_mns.NewAliMNSClient(p.conf.URL, p.conf.AccessKeyId, p.conf.AccessKeySecret)
	if p.conf.ProxyAddress != "" {
		cli.SetProxy(p.conf.ProxyAddress)
	}

	q := ali_mns.NewMNSQueue(queue, cli)

	p.mnsClient[queue] = q

	return q
}
Ejemplo n.º 5
0
func main() {
	conf := appConf{}

	if bFile, e := ioutil.ReadFile("app.conf"); e != nil {
		panic(e)
	} else {
		if e := json.Unmarshal(bFile, &conf); e != nil {
			panic(e)
		}
	}

	client := ali_mns.NewAliMNSClient(conf.Url,
		conf.AccessKeyId,
		conf.AccessKeySecret)

	msg := ali_mns.MessageSendRequest{
		MessageBody:  []byte("hello gogap/ali_mns"),
		DelaySeconds: 0,
		Priority:     8}

	queue := ali_mns.NewMNSQueue("test", client)
	ret, err := queue.SendMessage(msg)

	if err != nil {
		logs.Error(err)
	} else {
		logs.Pretty("response:", ret)
	}

	respChan := make(chan ali_mns.MessageReceiveResponse)
	errChan := make(chan error)
	go func() {
		for {
			select {
			case resp := <-respChan:
				{
					logs.Pretty("response:", resp)
					logs.Debug("change the visibility: ", resp.ReceiptHandle)
					if ret, e := queue.ChangeMessageVisibility(resp.ReceiptHandle, 5); e != nil {
						logs.Error(e)
					} else {
						logs.Pretty("visibility changed", ret)
					}

					logs.Debug("delete it now: ", resp.ReceiptHandle)
					if e := queue.DeleteMessage(resp.ReceiptHandle); e != nil {
						logs.Error(e)
					}
				}
			case err := <-errChan:
				{
					logs.Error(err)
				}
			}
		}

	}()

	queue.ReceiveMessage(respChan, errChan)
	for {
		time.Sleep(time.Second * 1)
	}

}
Ejemplo n.º 6
0
func (p *MessageSenderMNS) Send(url string, message ComponentMessage) (err error) {

	EventCenter.PushEvent(EVENT_BEFORE_MESSAGE_SEND, url, message)

	if url == "" {
		err = ERR_MESSAGE_ADDRESS_IS_EMPTY.New()
		return
	}

	var msgData []byte

	if msgData, err = message.Serialize(); err != nil {
		return
	}

	var client ali_mns.AliMNSQueue
	if c, exist := p.clientCache[url]; exist {
		client = c
	} else {
		hostId := ""
		accessKeyId := ""
		accessKeySecret := ""
		queueName := ""

		regUrl := regexp.MustCompile("http://(.*):(.*)@(.*)/(.*)")
		regMatched := regUrl.FindAllStringSubmatch(url, -1)

		if len(regMatched) == 1 &&
			len(regMatched[0]) == 5 {
			accessKeyId = regMatched[0][1]
			accessKeySecret = regMatched[0][2]
			hostId = regMatched[0][3]
			queueName = regMatched[0][4]
		}

		cli := ali_mns.NewAliMNSClient("http://"+hostId,
			accessKeyId,
			accessKeySecret)

		if cli == nil {
			err = ERR_SENDER_MNS_CLIENT_IS_NIL.New(errors.Params{"type": p.Type(), "url": url})
			return
		}

		client = ali_mns.NewMNSQueue(queueName, cli)

		p.clientCache[url] = client
	}

	msg := ali_mns.MessageSendRequest{
		MessageBody:  msgData,
		DelaySeconds: 0,
		Priority:     8}

	defer EventCenter.PushEvent(EVENT_AFTER_MESSAGE_SEND, url, msg)

	if _, e := client.SendMessage(msg); e != nil {
		err = ERR_SENDER_SEND_FAILED.New(errors.Params{"type": p.Type(), "url": url, "err": e})
		return
	}

	return
}