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 }
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 }
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) } }
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 }