et-go 20240919重建
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

49 lines
1.2 KiB

package data_source
import (
"log"
"time"
)
// IMessageHandler 是 kafka 消息处理者接口
type IMessageHandler interface {
HandleMessage(key string, values []string) bool
GetDataChannel() chan *RPCPayload
}
type RPCPayload struct {
Id string
Messages []string
}
type RawDataHandler struct {
key string
topic string
partitionID int
dataChannel chan *RPCPayload
}
// 创建一个新的 RawDataHandler 实例
func NewRawDataHandler(key, topic string, partitionID int) *RawDataHandler {
handler := &RawDataHandler{
key: key,
topic: topic,
partitionID: partitionID,
dataChannel: make(chan *RPCPayload, 10),
}
return handler
}
// 在 kafka_dataSource.go 的 Producer() 中被使用
func (h *RawDataHandler) HandleMessage(thingId string, values []string) bool {
h.dataChannel <- &RPCPayload{Id: thingId, Messages: values}
log.Printf("--> RawDataHandler%d ,h.dataChannel【%p】通道数据量: %d/%d", h.partitionID, h.dataChannel, len(h.dataChannel), cap(h.dataChannel))
time.Sleep(50 * time.Millisecond)
return true
}
// GetDataChannel 返回 dataChannel
func (h *RawDataHandler) GetDataChannel() chan *RPCPayload {
return h.dataChannel
}