Example #1
0
func (p *BaseComponent) sendMessage(address MessageAddress, msg ComponentMessage) {
	var sender MessageSender
	var err error
	if sender, err = senderFactory.NewSender(address.Type); err != nil {
		logs.Error(err)
		return
	}

	if err = sender.Send(address.Url, msg); err != nil {
		logs.Error(err)
		return
	}
}
Example #2
0
func (p *BaseComponent) hookMessagesAfter(inPortName string, message *ComponentMessage) (metadatas []MessageHookMetadata, err error) {
	var hooks []string
	var exist bool

	if hooks, exist = p.inPortHooks[inPortName]; !exist {
		return
	}

	newMetadatas := []MessageHookMetadata{}
	for index, hookName := range hooks {
		var hook MessageHook
		if hook, err = hookFactory.Get(hookName); err != nil {
			return
		}

		if ignored, matadata, e := hook.HookAfter(message.hooksMetaData, newMetadatas, &message.payload); e != nil {
			if !errors.IsErrCode(e) {
				e = ERR_MESSAGE_HOOK_ERROR.New(errors.Params{"err": e, "name": p.name, "port": inPortName, "index": index, "count": len(hooks), "hookName": hook.Name(), "event": "after"})
			}
			logs.Error(e)

			if !ignored {
				err = e
				return
			}
		} else if !ignored {
			matadata.HookName = hookName
			newMetadatas = append(newMetadatas, matadata)
		}
	}
	metadatas = newMetadatas
	return
}
Example #3
0
func (p *BaseComponent) hookMessagesBefore(inPortName string, message *ComponentMessage) (metadatas []MessageHookMetadata, err error) {
	preMetadata := message.hooksMetaData
	if preMetadata == nil || len(preMetadata) == 0 {
		return
	}

	var hooks []string
	var exist bool

	if hooks, exist = p.inPortHooks[inPortName]; !exist {
		err = ERR_INPORT_NOT_BIND_HOOK.New(errors.Params{"inPortName": inPortName})
		return
	}

	newMetadatas := []MessageHookMetadata{}
	for index, metadata := range preMetadata {
		var hook MessageHook
		if hook, err = hookFactory.Get(metadata.HookName); err != nil {
			return
		}

		if ignored, matadata, e := hook.HookBefore(metadata, message.hooksMetaData, newMetadatas, &message.payload); e != nil {
			if !errors.IsErrCode(e) {
				e = ERR_MESSAGE_HOOK_ERROR.New(errors.Params{"err": e, "name": p.name, "port": inPortName, "index": index, "count": len(hooks), "hookName": hook.Name(), "event": "before"})
			}
			logs.Error(e)

			if !ignored {
				err = e
				return
			}
		} else if !ignored {
			newMetadatas = append(newMetadatas, matadata)
		}
	}
	metadatas = newMetadatas
	return
}
Example #4
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)
	}

}
Example #5
0
func (p *BaseComponent) handleRecevierError(inportName string, err error) {
	atomic.AddInt64(&p.inboxError, 1)
	defer atomic.AddInt64(&p.inboxError, -1)

	logs.Error(err)
}
Example #6
0
func (p *BaseComponent) handleComponentMessage(inPortName string, context interface{}, message ComponentMessage, callbacks ...OnReceiverMessageProcessed) {
	atomic.AddInt64(&p.inboxMessage, 1)
	defer atomic.AddInt64(&p.inboxMessage, -1)

	defer func() {
		if callbacks != nil {
			for _, callback := range callbacks {
				callback(context)
			}
		}
	}()

	var handler ComponentHandler
	var err error
	var exist bool

	if message.graph == nil {
		logs.Error(ERR_MESSAGE_GRAPH_IS_NIL.New())
		return
	}

	if handler, exist = p.inPortHandler[inPortName]; !exist {
		logs.Error(fmt.Sprintf("in port of %s not exist", inPortName))
	}

	var address MessageAddress
	var nextGraphIndex int32 = 0
	var content interface{}

	var newMetadatas []MessageHookMetadata
	if _, err = p.hookMessages(inPortName, HookEventBeforeCallHandler, &message); err != nil {
		if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
			errCode := err.(errors.ErrCode)
			message.payload.err.AddressId = message.currentGraphIndex
			message.payload.err.Id = errCode.Id()
			message.payload.err.Namespace = errCode.Namespace()
			message.payload.err.Code = errCode.Code()
			message.payload.err.Message = errCode.Error()

			nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
		} else {
			return
		}
	} else {
		if content, err = p.callHandlerWithRecover(handler, &message.payload); err != nil {
			if !errors.IsErrCode(err) {
				err = ERR_COMPONENT_HANDLER_RETURN_ERROR.New(errors.Params{"err": err, "name": p.name, "port": inPortName})
			}

			logs.Error(err)

			if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
				errCode := err.(errors.ErrCode)
				message.payload.err.AddressId = message.currentGraphIndex
				message.payload.err.Id = errCode.Id()
				message.payload.err.Namespace = errCode.Namespace()
				message.payload.err.Code = errCode.Code()
				message.payload.err.Message = errCode.Error()

				nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
			} else {
				return
			}
		} else {
			message.payload.SetContent(content)

			if newMetadatas, err = p.hookMessages(inPortName, HookEventAfterCallHandler, &message); err != nil {
				if address, exist = message.graph[ERROR_MSG_ADDR]; exist {
					errCode := err.(errors.ErrCode)
					message.payload.err.AddressId = message.currentGraphIndex
					message.payload.err.Id = errCode.Id()
					message.payload.err.Namespace = errCode.Namespace()
					message.payload.err.Code = errCode.Code()
					message.payload.err.Message = errCode.Error()

					nextGraphIndex = ERROR_MSG_ADDR_INT //forword to the error port
				} else {
					return
				}
			}

			if address, exist = message.graph[strconv.Itoa(int(message.currentGraphIndex)+1)]; exist {
				nextGraphIndex = message.currentGraphIndex + 1 //forword the next component
			} else {
				return
			}
		}
	}

	message.hooksMetaData = newMetadatas
	message.currentGraphIndex = nextGraphIndex

	if nextInPortName, exist := p.inPortUrlIndex[address.Type+"|"+address.Url]; exist {
		p.handleComponentMessage(nextInPortName, nil, message)
		EventCenter.PushEvent(EVENT_BASE_COMPONENT_INTERNAL_MESSAGE, p.name, address, message)
	} else {
		p.sendMessage(address, message)
	}

	return
}