当前位置 : 主页 > 编程语言 > java >

golang与activeMQ

来源:互联网 收集:自由互联 发布时间:2022-06-23
package main import stomp "github.com/go-stomp/stomp" import "fmt" //Connect to ActiveMQ and listen for messages func main() { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { fmt.Println(err) } sub, err := conn.Subscribe("
package main

import stomp "github.com/go-stomp/stomp"
import "fmt"

//Connect to ActiveMQ and listen for messages
func main() {
conn, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
fmt.Println(err)
}

sub, err := conn.Subscribe("/queue/test-1", stomp.AckAuto)
if err != nil {
fmt.Println(err)
}
for {
msg := <-sub.C
fmt.Println(msg)
}

err = sub.Unsubscribe()
if err != nil {
fmt.Println(err)
}
defer conn.Disconnect()
}
package main

import stomp "github.com/go-stomp/stomp"
import "fmt"

//Connect to ActiveMQ and produce messages
func main() {
conn, err := stomp.Dial("tcp", "localhost:61613")
if err != nil {
fmt.Println(err)
}

c := make(chan string)
quit := make(chan string)
go Producer(c, quit, conn)

for {
fmt.Println(<-c)
}
quit<-"read"
}

func Producer(c, quit chan string, conn *stomp.Conn) {
for {
select {
case c <- "msg sent":
err := conn.Send(
"/queue/test-1", // destination
"text/plain", // content-type
[]byte("Test message #1")) // body
if err != nil {
fmt.Println(err)
return;
}
case <-quit:
fmt.Println("finish")
return;
}
}
}

go run main.go
Run Consumer:

go run reader.go

golang与activeMQ_github
通过网页可以查看到,消息有生产,也有消费。

​​推荐​​



【文章原创作者:防ddos攻击 http://www.558idc.com/shsgf.html 复制请保留原URL】
上一篇:export对环境变量进行设置
下一篇:没有了
网友评论