gobeanstalk - go語言beanstalk客戶端

項目地址: https://github.com/liuzhengyang/gobeanstalk

go-beanstalk 是beanstalkd 的GO語言的一個客戶端.

項目還在開發中,歡迎你們提意見git

介紹

beanstalkd是一個快速的、有各類用途的延遲隊列 和定時任務的不一樣點: 定時任務以必定的週期或者在某個特定的時間運行。beanstalk能夠在延遲一段時間執行。 一些使用場景:github

  • 用戶下單5分鐘後檢查用戶是否完成了支付
  • 一分鐘後開始一個新的程序

如何使用

Mac&Linux

安裝並啓動beantalkd服務器

git clone https://github.com/kr/beanstalkd
cd beanstalkd
make
./beanstalkd

使用示例

go get github.com/liuzhengyang/gobeanstalk

create a test.go file服務器

package main

import (
	"fmt"
	"github.com/liuzhengyang/gobeanstalk"
)

func main() {
	addr := "localhost:11300"  // define server address
	newConn := gobeanstalk.NewConnection(addr)   // create new connection
	channel := make(chan int)   // create int channel
	putFunc := func() {
		// define a function which put some message to one tube
		id, _ := newConn.PutWithTube("hello", "test2", 1)
		channel <- id
	}
	go putFunc()   // run previous function in a go-routine
	id := <-channel  // wait until we finish putting
	fmt.Printf("Receive from channel message of another goroutine %d\n", id)
	listenChannel := make(chan string)  // make a listen channel for receiving results
	dealFunc := func(body string) bool {
		// define a function to deal with tube messages
		fmt.Printf("receive %s\n", body)
		listenChannel <- body
		return true
	}
	go newConn.Listen("test2", dealFunc)  // run deal function in a specified go-routing
	body := <-listenChannel     // wait our message
	fmt.Printf("Listen once %s\n", body)
	newConn.Close()   // Close connection
}

And run thisthis

go run test.go
相關文章
相關標籤/搜索