Use of BEANSTALKD (Golang)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Recently, a new message queue has to be introduced, preferably a professional, simple, message-not-lost feature, without introducing too much complexity.
Especially in the present single-handedly case. Then I found out that BEANSTALKD seems to be what I need.
The BEANSTALKD supports task priorities (priority), latency (delay), timeout (time-to-run) and reservation (buried),
Support Binlog at the same time. The final speed is also possible.
Read the next source, C language code is small and clear. The author maintained from 07 to 14, star is also very high, the quality should be guaranteed.
The queue author provides the go client, which can be seen from the list of author projects that has written a lot of go-related things, and it seems that go is popular with background development.

BEANSTALKD home in this: HTTP://KR.GITHUB.IO/BEANSTALKD

Write an example of the invocation as follows.

/* XCL (2015-8-15) tubename Multi-consumer */package mainimport ("FMT" "Github.com/kr/beanstalk" "Runtime" "Strings" "Time") Var ( TubeName1 string = "Channel1" TubeName2 string = "Channel2") func Producer (fname, tubename string) {if fname = = "" | | Tubename = = "" {return}c, err: = Beanstalk. Dial ("TCP", "127.0.0.1:11300") if err! = Nil {panic (err)}defer c.close () C.tube.name = Tubenamec.tubeset.name[tubename] = Truefmt. Println (fname, "[Producer] Tubename:", Tubename, "C.tube.name:", c.tube.name) for I: = 0; I < 5; i++ {msg: = FMT. Sprintf ("For%s%d", Tubename, I) C.put ([]byte (msg), 0, 120*time. Second) fmt. Println (fname, "[Producer] Beanstalk put body:", msg)//time. Sleep (1 * time. Second)}c.close () fmt. Println ("Producer () end.")} Func Consumer (fname, tubename string) {if fname = = "" | | Tubename = = "" {return}c, err: = Beanstalk. Dial ("TCP", "127.0.0.1:11300") if err! = Nil {panic (err)}defer c.close () C.tube.name = Tubenamec.tubeset.name[tubename] = Truefmt. Println (fname, "[Consumer] Tubename:", Tubename, "C.tube.name:", c.tube.name) substr: = "Timeout" for {fmt. Println (fname, "[consumer]/////////////////////////")//Remove the ID from the queue, body, err: = C.reserve (1 * time. Second) If err! = Nil {if!strings. Contains (Err. Error (), substr) {fmt. Println (fname, "[Consumer] [", C.tube.name, "] err:", err, "ID:", id)}continue}fmt. Println (fname, "[Consumer] [", C.tube.name, "] job:", ID, "Body:", string (body))//clear from Queue err = C.delete (ID) if err! = Nil {FMT. Println (fname, "[Consumer] [", C.tube.name, "] Delete err:", err, "ID:", id)} else {fmt. Println (fname, "[Consumer] [", C.tube.name, "] successfully deleted. ID: ", id)}fmt. Println (fname, "[consumer]/////////////////////////")//time. Sleep (1 * time. Second)}fmt. Println ("Consumer () end.")} Func Main () {runtime. Gomaxprocs (runtime. NUMCPU ()) Go Producer ("PA", TubeName1) Go Producer ("PB", TubeName2) Go Consumer ("CA", TubeName1) Go Consumer ("CB", TubeName2) time. Sleep (Ten * time. Second)}/* Run Result: xcldeimac:src xcl$ clearxcldeimac:src xcl$ go run TESTMQ.GOCB [Consumer]Tubename:channel2 C.TUBE.NAME:CHANNEL2CA [Consumer] Tubename:channel1 C.TUBE.NAME:CHANNEL1CB [Consumer]/////////// CA [CONSUMER]/////////////////////////PB [Producer] Tubename:channel2 C.tube.name:channel2pa [Produce R] Tubename:channel1 C.TUBE.NAME:CHANNEL1PB [Producer] Beanstalk put body:for channel2 0PA [Producer] Beanstalk put Body:for channel1 0CA [Consumer] [Channel1] job:47027 body:for channel1 0CB [Consumer] [Channel2] job:47026 bo Dy:for Channel2 0PB [Producer] Beanstalk put body:for channel2 1PA [Producer] Beanstalk put body:for channel1 1CB [C Onsumer] [Channel2] successfully deleted. ID:47026CB [CONSUMER]/////////////////////////CB [CONSUMER]/////////////////////////CA [Consumer] [Channel1] Succes sfully deleted. ID:47027CA [CONSUMER]/////////////////////////CA [CONSUMER]/////////////////////////CA [Consumer] [Channel1] job:47 028 body:for channel1 1PA [Producer] Beanstalk put body:for channel1 2CB [Consumer] [Channel2] job:47029 body:for channel2 1PB [Producer] Beanstalk put body:for channel2 2PA [Producer] Beanstalk PU T body:for channel1 3CA [Consumer] [Channel1] successfully deleted. ID:47028CA [CONSUMER]/////////////////////////CA [CONSUMER]/////////////////////////CB [Consumer] [Channel2] Succes sfully deleted. ID:47029PB [Producer] Beanstalk put body:for channel2 3CB [CONSUMER]/////////////////////////CB [consumer]///////////] PB [Producer] Beanstalk put body:for channel2 4CB [Consumer] [Channel2] job:47030 body:for Channel2 2CA [Consumer] [Channel1] job:47031 body:for channel1 2PA [Producer] Beanstalk put body:for channel1 4Producer () E nd. Producer () end.ca [Consumer] [Channel1] successfully deleted. ID:47031CA [CONSUMER]/////////////////////////CA [CONSUMER]/////////////////////////CB [Consumer] [Channel2] Succes sfully deleted. ID:47030CB [CONSUMER]/////////////////////////CB [CONSUMER]/////////////////////////CB [ConsumER] [channel2] job:47033 body:for channel2 3CA [Consumer] [Channel1] job:47032 body:for channel1 3CB [Consumer ] [Channel2] successfully deleted. ID:47033CA [Consumer] [Channel1] successfully deleted. ID:47032CB [CONSUMER]/////////////////////////CB [CONSUMER]/////////////////////////CA [Consumer]////////////////  CA [CONSUMER]/////////////////////////CA [Consumer] [Channel1] job:47034 body:for channel1 4CB [Consumer] [Channel2] job:47035 body:for channel2 4CB [Consumer] [Channel2] successfully deleted. ID:47035CB [CONSUMER]/////////////////////////CA [Consumer] [Channel1] successfully deleted. ID:47034CB [Consumer]/////////////////////////xcldeimac:src xcl$*/
Use Beanstool to view queue status

can also refer to I write the following two paragraphs, to check.

AR, er: = C.listtubes () if er! = nil {fmt. Println ("[Example]  er:", er)} else {for I, V: = Range ar {fmt. Println ("[Example] listtubes  i:", I, "V:", v) c.tube.name = vid, body, err: = C.reserve (5 * time). Second) If err! = Nil {fmt. Println ("[Example] Err:", err, "Name:", c.tube.name) continue} else {FMT. Println ("[Example] Job:", id) fmt. Println ("[Example] Body:", string (body))}}}func Tubestatus (c *beanstalk. Conn) {fmt. Println ("[tubestatus]/////////////////////////") fmt. Println ("Tube (", C.tube.name, ") Stats:") m, er: = c.tube.stats () if er! = nil {fmt. Println ("[Tubestatus] Err:", er)} else {for k, V: = range m {fmt. Println (k, ":", v)}}fmt. Println ("[tubestatus]/////////////////////////")}
From the test, beanstalkd enough to meet my current needs.

blog:http://blog.csdn.net/xcl168


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.