使用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)
} }

最新文章

  1. Best Time to Buy and Sell Stock1,2,3,4
  2. 三、jQuery--jQuery基础--jQuery基础课程--第9章 jQuery 常用插件
  3. 贪心 HDOJ 4726 Kia&#39;s Calculation
  4. python爬虫之Scrapy 使用代理配置
  5. Hadoop常见错误解决
  6. Emule使用Upnp,解决Lowid和port not reachable的问题
  7. 类型转换操作符static_cast、const_cast、dynamic_cast、reinterpret_cast
  8. Python基础(十三)-进程&amp;线程
  9. 关于input 的选中,自定义input[type=&quot;checkbox&quot;]样式
  10. Django使用models建表的一些另类功能
  11. web项目如何使用Material Icons
  12. Vs2017 typescript 开发小问题
  13. Java中的hashCode() 和 equals()的若干问题解答
  14. vue自定義指令
  15. Luogu P4705 玩游戏
  16. Oracle EBS CST 成本请求报错
  17. 进程间通信IPC -- 管道, 队列
  18. block本质探寻七之内存管理
  19. 更新ubuntu的源
  20. WiFi无线连接真机进行Appium自动化测试方法

热门文章

  1. jmeter压测学习1-window环境准备与案例
  2. 用于Python扩展包的非官方Windows二进制文件
  3. django常用命令行和一些笔记
  4. Tomcat热部署和热加载
  5. 聊聊CMSIS-RTOS是什么东东
  6. 一些开源的dashboard 解决方案
  7. 2019蓝桥杯Java第十题大学生B组——最短路径思想
  8. linux 如何改变文件属性与权限1
  9. 区间dp专题练习
  10. java基础之 final