示例#1
0
// 执行单个 Command
func (self *CommandProcessor) executeCommand(command Command, commandId uuid.UUID) {
	commandName := util.FromPtrTypeOf(command).Name()
	var handler CommandHandler
	var handlerName string

	for key, val := range self.handlers {
		if key == commandName {
			handler = val
			handlerName = util.FromPtrTypeOf(val).String()
			break
		}
	}

	if handler != nil {
		context := &commandHandlerContext{commandName: commandName, handlerName: handlerName, isStop: false, isSuccess: false}
		middlewares := self.createMiddlewares()
		self.handleBefore(middlewares, command, ExecuteBeforeContext(context))
		if !context.isStop {
			startTime := time.Now().UnixNano()
			handler.Execute(command)
			endTime := time.Now().UnixNano()
			if ddd.DEBUG {
				consoleLog.Printf("Command '%s_%s' execute in %vms.\n", commandName, commandId, float64(endTime-startTime)/float64(time.Millisecond))
			}
			context.isSuccess = true
			self.handleAfter(middlewares, command, ExecuteAfterContext(context))
		} else {
			consoleLog.Printf("Command '%s_%s' is stopped by %s.\n", commandName, commandId, util.FromPtrTypeOf(context.stopByMiddleware).String())
		}
	} else {
		consoleLog.Panicf("[panic] CommandHandler for Command %s not found!", commandName)
	}
}
示例#2
0
// 注册 CommandHandler
func (self *CommandProcessor) RegisterHandler(handler CommandHandler) {
	handlerName := util.FromPtrTypeOf(handler).String()
	for _, commandType := range handler.CommandTypes() {
		commandName := util.FromPtrType(commandType).Name()
		self.handlers[commandName] = handler
		consoleLog.Printf("Register handler:%s for %s.\n", handlerName, commandName)
	}
}
示例#3
0
文件: command.go 项目: Berkaroad/ddd
// 获取 Command 的 CommandType
func CommandTypeOf(c Command) CommandType {
	return CommandType(util.FromPtrTypeOf(c))
}
示例#4
0
// 注册 CommandMiddleware
func (self *CommandProcessor) RegisterMiddleware(middleware CommandMiddleware) {
	middlewareType := util.FromPtrTypeOf(middleware)
	middlewareName := middlewareType.String()
	self.middlewareTypes = append(self.middlewareTypes, middlewareType)
	consoleLog.Printf("Register middleware:%s.\n", middlewareName)
}