Пример #1
0
func main() {
	EventCenter := event_center.NewClassicEventCenter("GoGapEventCenter")

	EventCenter.RegisterEvent(event_center.ConcurrencyMode,
		EVENT_CMD_STOP,
		EVENT_RECEIVER_STOPPED,
		EVENT_TRANS_MSG,
	)

	//EVENT_RECEIVER_STOPPED 事件响应
	stopSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {
		fmt.Println("::stopSubscriber")
		fmt.Println(values)
		fmt.Println()
	})
	EventCenter.Subscribe(EVENT_CMD_STOP, stopSubscriber)

	//EVENT_TRANS_MSG 事件响应
	transMsgSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {
		fmt.Println("::trans msg")
		fmt.Println(values)
		if reflect.TypeOf(values[0]).Kind() == reflect.Func {
			fmt.Println("params is function")
			callBack := reflect.ValueOf(values[0])
			in := make([]reflect.Value, 0)
			go callBack.Call(in)
		}
		fmt.Println()
	})
	EventCenter.Subscribe(EVENT_TRANS_MSG, transMsgSubscriber)

	for {
		time.Sleep(time.Second)

		EventCenter.PushEvent(EVENT_CMD_STOP, "SSSS")

		callBackFunc := func() interface{} {
			fmt.Println("call back function")
			return true
		}
		EventCenter.PushEvent(EVENT_TRANS_MSG, callBackFunc, "hahaha", "hello", "word")
		fmt.Println("In Main Program")

		time.Sleep(time.Second)
		fmt.Println()
		fmt.Println()
		fmt.Println("---------^---------")
	}
}
Пример #2
0
func (p *ClassicSpirit) buildEventInspectSubscribers() (err error) {
	if !p.inspectMode {
		return
	}

	events := EventCenter.ListEvents()

	loggerSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {

		str := ""
		for i, v := range values {
			str = str + fmt.Sprintf("v_%d: %v\n", i, v)
		}

		logs.Info(fmt.Sprintf("SPIRIT-EVENT-INSPECT EVENT_NAME: %s\nValues:\n%s\n", eventName, str))

	})

	for _, event := range events {
		if err = EventCenter.Subscribe(event, loggerSubscriber); err != nil {
			return
		}
	}

	return
}
Пример #3
0
func (p *ClassicSpirit) startHeartbeat() (err error) {
	if p.metadata.RunConfig.Heartbeat == nil || len(p.metadata.RunConfig.Heartbeat) == 0 {
		return
	}

	heartbeatMessage := p.getHeartbeatMessage()

	for _, heartbeatConf := range p.metadata.RunConfig.Heartbeat {
		if heartbeater, exist := heartbeaters[heartbeatConf.Heart]; exist {
			if err = heartbeater.Start(heartbeatConf.Options); err != nil {
				return
			}

			go func(beater Heartbeater, msg HeartbeatMessage, sleepTime time.Duration) {
				isStopped := false
				stopSubScriber := event_center.NewSubscriber(
					func(eventName string, values ...interface{}) {
						isStopped = true
					})

				EventCenter.Subscribe(EVENT_CMD_STOP, stopSubScriber)
				defer EventCenter.Unsubscribe(EVENT_CMD_STOP, stopSubScriber.Id())

				for !isStopped {
					time.Sleep(sleepTime)
					msg.CurrentTime = time.Now()
					msg.HeartbeatCount += 1
					beater.Heartbeat(msg)
				}
			}(heartbeater, heartbeatMessage, time.Duration(heartbeatConf.Interval)*time.Millisecond)
		} else {
			err = ERR_HEARTBEAT_NOT_EXIST.New(errors.Params{"name": heartbeatConf.Heart})
			return
		}
	}

	return
}