go使用beanstalk

生产代码

package main

import (
	"github.com/iwanbk/gobeanstalk"
	"log"
	"time"
)

func main() {
	conn, err := gobeanstalk.Dial("111.229.100.91:11300")
	if err != nil {
		log.Fatal(err)
	}

	//管道名称
	conn.Use("cs")
	id, err := conn.Put([]byte("hello"), 0, 10*time.Second, 30*time.Second)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Job id %d inserted\n", id)
}

消费代码

package main
import (
	"github.com/iwanbk/gobeanstalk"
	"log"
)

func main() {
	conn, err := gobeanstalk.Dial("111.229.100.91:11300")
	if err != nil {
		log.Fatal(err)
	}
	conn.Watch("cs")
	for {

		j, err := conn.Reserve()
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("id:%d, body:%s\n", j.ID, string(j.Body))
		err = conn.Delete(j.ID)
		if err != nil {
			log.Fatal(err)
		}
	}
}

循环
请先登录后发表评论
  • latest comments
  • 总共0条评论