加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
yanghua 提交于 2020-05-22 13:57 . add dev
package main
/**
* Copyright 2016 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"./global"
"./models"
"fmt"
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
func onDayTimer(t *time.Timer) {
now := time.Now()
next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
if global.Config["runmode"] == "dev" {
t.Reset(5 * time.Second)
} else {
t.Reset(next.Sub(now))
}
}
func main() {
global.InitConfig("./config.cnf")
models.Init()
broker, ok := global.Config["broker"]
group, ok1 := global.Config["group"]
alltopics, ok2 := global.Config["topics"]
if !ok || !ok1 || !ok2 {
fmt.Println("broker=", broker, " group=", group, " topics=", alltopics)
return
}
fmt.Println("broker=", string(broker), " group=", string(group), " topics=", alltopics)
topics := strings.Split(alltopics, ",")
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": broker,
"group.id": group,
"session.timeout.ms": 6000,
"go.events.channel.enable": true,
"go.application.rebalance.enable": true,
// Enable generation of PartitionEOF when the
// end of a partition is reached.
"enable.partition.eof": true,
"auto.offset.reset": "earliest"})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create consumer: %s\n", err)
os.Exit(1)
}
dayTimer := time.NewTimer(time.Second * 10)
onDayTimer(dayTimer)
fmt.Printf("Created Consumer %v\n", c)
err = c.SubscribeTopics(topics, nil)
run := true
for run == true {
select {
case sig := <-sigchan:
fmt.Printf("Caught signal %v: terminating\n", sig)
run = false
case <-dayTimer.C:
fmt.Println("dayTimer ", time.Now().Format("2006-01-02 15:04:05"))
models.CheckNewDay()
onDayTimer(dayTimer)
case ev := <-c.Events():
switch e := ev.(type) {
case kafka.AssignedPartitions:
fmt.Fprintf(os.Stderr, "%% %v\n", e)
c.Assign(e.Partitions)
case kafka.RevokedPartitions:
fmt.Fprintf(os.Stderr, "%% %v\n", e)
c.Unassign()
case *kafka.Message:
if global.Config["runmode"] == "dev" {
fmt.Printf("%% Message on %s:\n%s\n", e.TopicPartition, string(e.Value))
}
models.CallActInsert(string(e.Value))
case kafka.PartitionEOF:
fmt.Printf("%% Reached %v\n", e)
case kafka.Error:
// Errors should generally be considered as informational, the client will try to automatically recover
fmt.Fprintf(os.Stderr, "%% Error: %v\n", e)
}
}
}
fmt.Printf("Closing consumer\n")
c.Close()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化