代码拉取完成,页面将自动刷新
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()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。