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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package main
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/*
Copied from https://github.com/apache/rocketmq-clients/blob/rocketmq-client-golang-5.0.0/golang/example/consumer/simple_consumer/main.go by lqx at 2023-11-27T15:34:51+08:00.
*/
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/apache/rocketmq-clients/golang"
"github.com/apache/rocketmq-clients/golang/credentials"
)
var (
// maximum waiting time for receive func
awaitDuration = time.Second * 5
// maximum number of messages received at one time
maxMessageNum int32 = 16
// invisibleDuration should > 20s
invisibleDuration = time.Second * 20
// receive messages in a loop
)
func main() {
topic := flag.String("topic", "", "rocketmq 5.x topic")
groupName := flag.String("group_name", "", "rocketmq 5.x group")
endpoint := flag.String("endpoint", "", "rocketmq 5.x endpoint")
accessKey := flag.String("access_key", "", "rocketmq 5.x access key")
secretKey := flag.String("secret_key", "", "rocketmq 5.x secret key")
flag.Parse()
// log to console
// os.Setenv("mq.consoleAppender.enabled", "true")
os.Setenv("rocketmq.client.logRoot", "./logs")
os.Setenv("rocketmq.client.logLevel", "warning")
golang.ResetLogger()
// new simpleConsumer instance
simpleConsumer, err := golang.NewSimpleConsumer(&golang.Config{
Endpoint: *endpoint,
ConsumerGroup: *groupName,
Credentials: &credentials.SessionCredentials{
AccessKey: *accessKey,
AccessSecret: *secretKey,
},
},
golang.WithAwaitDuration(awaitDuration),
golang.WithSubscriptionExpressions(map[string]*golang.FilterExpression{
*topic: golang.SUB_ALL,
}),
)
if err != nil {
log.Fatal(err)
}
// start simpleConsumer
err = simpleConsumer.Start()
if err != nil {
log.Fatal(err)
}
// gracefule stop simpleConsumer
defer simpleConsumer.GracefulStop()
go func() {
for {
fmt.Println("start recevie message")
mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration)
if err != nil {
fmt.Println(err)
}
// ack message
for _, mv := range mvs {
tag := mv.GetTag()
keys := mv.GetKeys()
// ensure it's json like
body := mv.GetBody()
var data map[string]string
err := json.Unmarshal(body, &data)
if err != nil {
fmt.Println(err)
break
}
fmt.Println("got message tag ", *tag)
fmt.Println("got message keys ", keys)
fmt.Println("got message data ", data)
// only close_open like tag will ack
if matched, _ := regexp.Match("close_open", []byte(*tag)); matched == true {
callback, ok := data["url"]
if ok {
fmt.Println("will request with url", data["url"])
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
jenkins_username := os.Getenv("JENKINS_USERNAME")
jenkins_password := os.Getenv("JENKINS_PASSWORD")
// will send jenkins stop env requests
req, err := http.NewRequestWithContext(ctx, http.MethodPost, callback, nil)
req.SetBasicAuth(jenkins_username, jenkins_password)
// make the POST request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Println("Send jenkins request failed:", err)
simpleConsumer.Ack(context.TODO(), mv)
break
} else {
// Check the response status code
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
fmt.Println("Send jenkins request successfully")
} else {
fmt.Println("Send jenkins request failed status:", res.Status)
}
}
defer res.Body.Close()
} else {
fmt.Println("does not have url, ignore")
}
} else {
fmt.Println("ignore this message, but ack")
}
simpleConsumer.Ack(context.TODO(), mv)
}
time.Sleep(time.Second * 10)
}
}()
// Keep the application running
done := make(chan struct{})
<-done
}
|