Golang 连接ActiveMQ

使用ActiveMQ库:github.com/go-stomp/stomp

示例代码

package main

import (
    "net"
    "fmt"
    "os"
    "github.com/gpmgo/gopm/modules/goconfig"
    "github.com/go-stomp/stomp"
    "strconv"
)


// 读取配置文件 func getConfigFile(filePath string) (configFile *goconfig.ConfigFile){ configFile, err := goconfig.LoadConfigFile(filePath) if err != nil { fmt.Println("load config file error: " + err.Error()) os.Exit(1) } return configFile } // 使用IP和端口连接到ActiveMQ服务器 // 返回ActiveMQ连接对象 func connActiveMq(host, port string) (stompConn *stomp.Conn){// @todo 实现断开重连 stompConn, err := stomp.Dial("tcp", net.JoinHostPort(host, port)) if err != nil { fmt.Println("connect to active_mq server service, error: " + err.Error()) os.Exit(1) } return stompConn }
// 将消息发送到ActiveMQ中 func activeMqProducer(c chan string, queue string, conn *stomp.Conn){ for{ err := conn.Send(queue, "text/plain", []byte(<-c)) fmt.Println("send active mq..." + queue) if err != nil { fmt.Println("active mq message send erorr: " + err.Error()) } } } func main() { configPath := "./config.ini" configFile := getConfigFile(configPath) host, err := configFile.GetValue("active_mq", "host") if err != nil { fmt.Println("get active_mq host error: " + err.Error()) os.Exit(1) } port, err:= configFile.GetValue("active_mq", "port") if err != nil { fmt.Println("get active_mq port error: " + err.Error()) os.Exit(1) } queue, err := configFile.GetValue("active_mq", "queue") if err != nil { fmt.Println("get active_mq queue error: " + err.Error()) os.Exit(1) } activeMq := connActiveMq(host, port) defer activeMq.Disconnect() c := make(chan string)
  // 启动Go routine发送消息 go activeMqProducer(c, queue, activeMq) for i := 0; i < 10000; i ++{
// 发送1万个消息 c <- "hello world" + strconv.Itoa(i) } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值