File tree Expand file tree Collapse file tree 1 file changed +10
-8
lines changed Expand file tree Collapse file tree 1 file changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -109,13 +109,13 @@ Third to create queue and initialize multiple worker, receive all job message:
109
109
q.Wait ()
110
110
```
111
111
112
- Full example code as below or [ try it in playground] ( https://play.golang.org/p/DlhCQgZZ1Bb ) .
112
+ Full example code as below or [ try it in playground] ( https://play.golang.org/p/xuR4WhcFdoQ ) .
113
113
114
114
``` go
115
115
package main
116
116
117
117
import (
118
- " errors "
118
+ " encoding/json "
119
119
" fmt"
120
120
" log"
121
121
" time"
@@ -125,11 +125,11 @@ import (
125
125
)
126
126
127
127
type job struct {
128
- message string
128
+ Message string
129
129
}
130
130
131
131
func (j *job ) Bytes () []byte {
132
- return []byte (j.message )
132
+ return []byte (j.Message )
133
133
}
134
134
135
135
func main () {
@@ -140,12 +140,14 @@ func main() {
140
140
w := simple.NewWorker (
141
141
simple.WithQueueNum (taskN),
142
142
simple.WithRunFunc (func (m queue.QueuedMessage ) error {
143
- j , ok := m.(*job)
143
+ v , ok := m.(*job)
144
144
if !ok {
145
- return errors.New (" message is not job type" )
145
+ if err := json.Unmarshal (m.Bytes (), &v); err != nil {
146
+ return err
147
+ }
146
148
}
147
149
148
- rets <- j. message
150
+ rets <- v. Message
149
151
return nil
150
152
}),
151
153
)
@@ -166,7 +168,7 @@ func main() {
166
168
for i := 0 ; i < taskN; i++ {
167
169
go func (i int ) {
168
170
q.Queue (&job{
169
- message : fmt.Sprintf (" handle the job: %d " , i+1 ),
171
+ Message : fmt.Sprintf (" handle the job: %d " , i+1 ),
170
172
})
171
173
}(i)
172
174
}
You can’t perform that action at this time.
0 commit comments