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)
} }
Golang 连接ActiveMQ的更多相关文章
- golang连接activemq,发送接收数据
介绍 使用golang连接activemq发送数据的话,需要使用一个叫做stomp的包,直接go get github.com/go-stomp/stomp即可 代码 生产者 package main ...
- windows下用golang连接mssql
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 安装Microsoft SQL Server Native Client 安装golang的mssql驱动 写测试代码 ...
- 消费者端的Spring JMS 连接ActiveMQ接收生产者Oozie Server发送的Oozie作业执行结果
一,介绍 Oozie是一个Hadoop工作流服务器,接收Client提交的作业(MapReduce作业)请求,并把该作业提交给MapReduce执行.同时,Oozie还可以实现消息通知功能,只要配置好 ...
- Golang连接Oracle数据库
Golang连接Oracle的库有很多,比较常见的如下: 不过,oralce 只提供了 oci8 的接口,必须通过它来调用,所以下面方案都逃不过相关设置. 1.go-db-oracle 地址: htt ...
- python使用stomp连接activemq
一.安装ActiveMQ服务 1. 当使用windows时,安装参考:https://blog.csdn.net/WuLex/article/details/78323811 启动:运行activem ...
- php 通过stomp协议连接ActiveMQ
一.安装php的stomp扩展 http://pecl.php.net/package/stomp 如:stomp-2.0.0.tgz > tar xf stomp-1.0.9.tgz > ...
- golang连接达梦数据库的一个坑
golang连接达梦数据库的一个坑 有一次项目中用到了达梦数据库,后端语言使用的golang,达梦官方并未适配专门的golang连接方式,正一筹莫展的时候发现达梦提供了odbc的连接,这样可以使用类似 ...
- eclipse 搭建连接 activemq
今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学. 1.下载activemq压缩包,并解压(如果需要下载请看文章尾部附录) 2.进入bin文件夹,(64位电脑就 ...
- golang连接orcale
使用glang有一段时间了,最开始其实并不太喜欢他的语法,但是后来熟悉之后发现用起来还挺爽的.之前数据库一直使用mysql,连接起来没有什么问题,github上有很多完善的驱动,所以以为连接其他数据库 ...
随机推荐
- javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.qingmu.Customer
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity pas ...
- git分布式版本控制系统的概述和安装
Git历史 同生活中的许多伟大赛事一样,Git诞生于一个极富纷争大举创新的年代.Linux内核开源项目有着为数众广的参与者.绝大多数的Linux内核维护工作都花在了提交补丁和保存归档的繁琐事务上(19 ...
- python应用-craps赌博游戏
from random import randint face1=randint(1,6) face2=randint(1,6) first_point=face1+face2 print('玩家摇出 ...
- Spring Boot 2.x(十六):玩转vue文件上传
为什么使用Vue-Simple-Uploader 最近用到了Vue + Spring Boot来完成文件上传的操作,踩了一些坑,对比了一些Vue的组件,发现了一个很好用的组件--Vue-Simple- ...
- python 定时任务 from apscheduler.schedulers.blocking import BlockingScheduler
说明:使用python内置的模块来实现,本篇博客只是以循环定时来示范,其他的可以结合crontab的风格自己设定 一.导包 from apscheduler.schedulers.blocking i ...
- ent 基本使用十八 查询谓词
ent 生成的代码包含了比较完整的查询谓词 字段谓词 Bool: =, != Numeric: =, !=, >, <, >=, <=, IN, NOT IN Time: =, ...
- LOJ#6229. 这是一道简单的数学题(莫比乌斯反演+杜教筛)
题目链接 \(Description\) 求\[\sum_{i=1}^n\sum_{j=1}^i\frac{lcm(i,j)}{gcd(i,j)}\] 答案对\(10^9+7\)取模. \(n< ...
- .NET总结--ASP.NET工作原理
前言 前前后后写了不少关于某些技术啥的博客,一直在追新求深,而真正使用上的时候才发现了解的太少太少了,从事.net开发三年有余了不是它不行了而是我坚持不住了,如今不得不向生活低头,这个系列作为三年技术 ...
- nginx架构分析之 事件驱动模型
事件驱动模型 事件驱动模型是实现异步非阻塞的一个手段.事件驱动模型中,一个进程(线程)就可以了. 对于web服务器来说,客户端A的请求连接到服务端时,服务端的某个进程(Nginx worker pro ...
- MVC框架模式和Javaweb经典三层架构
一.MVC设计模式 1.MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(Vie ...