forked from feixiaobo/go-xxl-job-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxxl_client.go
99 lines (86 loc) · 2.53 KB
/
xxl_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package xxl
import (
"context"
"github.com/apache/dubbo-go-hessian2"
"github.com/tangjun2012/go-xxl-job-client/handler"
"github.com/tangjun2012/go-xxl-job-client/logger"
"github.com/tangjun2012/go-xxl-job-client/option"
"github.com/tangjun2012/go-xxl-job-client/transport"
)
type XxlClient struct {
executor *executor
gettyClient *GettyClient
requestHandler *handler.RequestHandler
}
type executor struct {
appName string
port int
}
func NewXxlClient(opts ...option.Option) *XxlClient {
hessian.RegisterPOJO(&transport.XxlRpcRequest{})
hessian.RegisterPOJO(&transport.TriggerParam{})
hessian.RegisterPOJO(&transport.Beat{})
hessian.RegisterPOJO(&transport.XxlRpcResponse{})
hessian.RegisterPOJO(&transport.ReturnT{})
hessian.RegisterPOJO(&transport.HandleCallbackParam{})
hessian.RegisterPOJO(&logger.LogResult{})
hessian.RegisterPOJO(&transport.RegistryParam{})
clientOps := option.NewClientOptions(opts...)
requestHandler := handler.NewRequestHandler(clientOps)
gettyClient := &GettyClient{
PkgHandler: handler.NewPackageHandler(),
EventListener: &handler.MessageHandler{
GettyClient: &transport.GettyRPCClient{},
MsgHandle: requestHandler.RequestHandler,
},
}
return &XxlClient{
requestHandler: requestHandler,
executor: &executor{
appName: clientOps.AppName,
port: clientOps.Port,
},
gettyClient: gettyClient,
}
}
func (c *XxlClient) ExitApplication() {
c.requestHandler.RemoveRegisterExecutor()
}
func GetParam(ctx context.Context, key string) (val string, has bool) {
jobMap := ctx.Value("jobParam")
if jobMap != nil {
inputParam, ok := jobMap.(map[string]map[string]interface{})["inputParam"]
if ok {
val, vok := inputParam[key]
if vok {
return val.(string), true
}
}
}
return "", false
}
func GetSharding(ctx context.Context) (shardingIdx, shardingTotal int32) {
jobMap := ctx.Value("jobParam")
if jobMap != nil {
shardingParam, ok := jobMap.(map[string]map[string]interface{})["sharding"]
if ok {
idx, vok := shardingParam["shardingIdx"]
if vok {
shardingIdx = idx.(int32)
}
total, ok := shardingParam["shardingTotal"]
if ok {
shardingTotal = total.(int32)
}
}
}
return shardingIdx, shardingTotal
}
func (c *XxlClient) Run() {
c.requestHandler.RegisterExecutor(c.executor.appName, c.executor.port)
go logger.InitLogPath()
c.gettyClient.Run(c.executor.port, c.requestHandler.JobHandler.BeanJobLength())
}
func (c *XxlClient) RegisterJob(jobName string, function handler.JobHandlerFunc) {
c.requestHandler.JobHandler.RegisterJob(jobName, function)
}