golang连接activemq,发送接收数据
介绍
使用golang连接activemq发送数据的话,需要使用一个叫做stomp的包,直接go get github.com/go-stomp/stomp即可
代码
生产者
package main
import (
"fmt"
"github.com/go-stomp/stomp"
"time"
)
func main(){
// 调用Dial方法,第一个参数是"tcp",第二个参数则是ip:port
// 返回conn(连接)和err(错误)
conn,err:=stomp.Dial("tcp", "47.adsasaads89:61613")
// 错误判断
if err!=nil{
fmt.Println("err =", err)
return
}
//发送十条数据
for i:=0;i<10;i++ {
// 调用conn下的send方法,接收三个参数
//参数一:队列的名字
//参数二:数据类型,一般是文本类型,直接写text/plain即可
//参数三:内容,记住要转化成byte数组的格式
//返回一个error
err := conn.Send("testQ", "text/plain",[]byte(fmt.Sprintf("message:%d", i)))
if err!=nil{
fmt.Println("err =", err)
}
}
/*
这里为什么要sleep一下,那就是conn.Send这个过程是不阻塞的
相当于Send把数据放到了一个channel里面
另一个goroutine从channel里面去取数据再放到消息队列里面
但是还没等到另一个goroutine放入数据,此时循环已经结束了
因此最好要sleep一下,根据测试,如果不sleep,那么发送1000条数据,
最终进入队列的大概是980条数据,这说明了什么
说明了当程序把1000条数据放到channel里面的时候,另一个goroutine只往队列里面放了980条
剩余的20条还没有来得及放入,程序就结束了
*/
time.Sleep(time.Second * 1)
}
消费者
package main
import (
"fmt"
"github.com/go-stomp/stomp"
"time"
)
func recv_data(ch chan *stomp.Message) {
//不断地循环,从channel里面获取数据
for {
v := <-ch
//这里是打印当然还可以做其他的操作,比如写入hdfs平台
//v是*stomp.Message类型,属性都在这里面
/*
type Message struct {
// Indicates whether an error was received on the subscription.
// The error will contain details of the error. If the server
// sent an ERROR frame, then the Body, ContentType and Header fields
// will be populated according to the contents of the ERROR frame.
Err error
// Destination the message has been sent to.
Destination string
// MIME content type.
ContentType string // MIME content
// Connection that the message was received on.
Conn *Conn
// Subscription associated with the message.
Subscription *Subscription
// Optional header entries. When received from the server,
// these are the header entries received with the message.
Header *frame.Header
// The ContentType indicates the format of this body.
Body []byte // Content of message
}
*/
fmt.Println(string(v.Body))
}
}
func main() {
//创建一个channel,存放的是*stomp.Message类型
ch := make(chan *stomp.Message)
//将管道传入函数中
go recv_data(ch)
//和生产者一样,调用Dial方法,返回conn和err
conn, err := stomp.Dial("tcp", "47.dsdsadsa9:61613")
if err != nil {
fmt.Println("err =", err)
}
//消费者订阅这个队列
//参数一:队列名
//参数二:确认信息,直接填默认地即可
sub, err := conn.Subscribe("testQ", stomp.AckMode(stomp.AckAuto))
for { //无限循环
select {
//sub.C是一个channel,如果订阅的队列有数据就读取
case v := <-sub.C:
//读取的数据是一个*stomp.Message类型
ch <- v
//如果30秒还没有人发数据的话,就结束
case <-time.After(time.Second * 30):
return
}
}
}
message:0
message:1
message:2
message:3
message:4
message:5
message:6
message:7
message:8
message:9
golang连接activemq,发送接收数据的更多相关文章
- 安卓Socket连接实现连接实现发送接收数据,openwrt wifi转串口连接单片机实现控制
安卓Socket连接实现连接实现发送接收数据,openwrt wifi转串口连接单片机实现控制 socket 连接采用流的方式进行发送接收数据,采用thread线程的方式. 什么是线程? 详细代码介 ...
- Unary模式下客户端从开始连接到发送接收数据的主要流程
(原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient grpc Unary模式下客户端从开始连接到发送数据的主要流程 graph TD; ...
- 网络编程--使用TCP协议发送接收数据
package com.zhangxueliang.tcp; import java.io.IOException; import java.io.OutputStream; import java. ...
- 网络编程--使用UDP发送接收数据
package com.zhangxueliang.udp; import java.io.IOException; import java.net.DatagramPacket; import ja ...
- c# 串口发送接收数据
/********************** 串口数据接收事件 *****************************/ private void SerialPort_DataReceived ...
- STM32 串口USART DMA方式发送接收数据
硬件:stm32f103cbt6 软件:STM32F10x_StdPeriph_Lib_V3.5.0 文章目录 头文件 USART3_DR的地址 DMA的通道 DMA的中断 USART接收回调函数 头 ...
- java 基于tcp客户端服务端发送接收数据
客户端: package demo03; import java.io.IOException; import java.io.InputStream; import java.io.OutputSt ...
- socket 异步 发送 接收 数据
Socket socketClints = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); / ...
- Golang 连接ActiveMQ
使用ActiveMQ库:github.com/go-stomp/stomp 示例代码 package main import ( "net" "fmt" &qu ...
随机推荐
- python3将汉字转换为大写拼音首字母
利用pypinyin包 实现 import pypinyin a = pypinyin.pinyin('你好世界', style=pypinyin.FIRST_LETTER) b = [] for i ...
- CnPack开发包基础库
unit CnCommon; {* |<PRE> ===================================================================== ...
- C# 跨线程更新 UI
Winforms 跨线程更新 UI 在 Winforms 中, 所有的控件都包含 InvokeRequired 属性, 如果我们要更新UI,通过它我们可以判断是否需要调用 [Begin]Invoke. ...
- ubuntu install themes && use it
one step: I am going to show you the installation of a theme with Numix theme and Unity Tweak Tool. ...
- 【MapReduce】二、MapReduce编程模型
通过前面的实例,可以基本了解MapReduce对于少量输入数据是如何工作的,但是MapReduce主要用于面向大规模数据集的并行计算.所以,还需要重点了解MapReduce的并行编程模型和运行机制 ...
- 目前最新u盘启动快捷热键一览表
现在重装系统已不再是件难事了,一个普通的u盘就可以帮你搞定,但是对于一些新手来说在使用u盘启动盘安装系统是也许会遇到这样的小问题,面对一台新电脑时不知道该如何让电脑优先访问u盘从而进入PE系统下进行装 ...
- USACO4.4 Frame Up【拓扑排序】
题意居然还读了好久... 读完题目之后大概就知道拓扑排序了.用拓扑可以求出一些字母之间的关系,谁先,谁后.但是这个关系不是唯一确定的,所以就会产生多种方案(题目还要求按字典序输出所有的方案) 输出方案 ...
- 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法
使用blas做矩阵乘法 #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <st ...
- NIO 编程模型
NIO 编程模型 Doug Lea 在 Scalable IO in Java 的 PPT 中描述了 Reactor 编程模型的思想,大部分 NIO 框架和一些中间件的 NIO 编程都与它一样或是它的 ...
- gulp时发生错误---------const { Math, Object } = primordials;
[问题描述] 执行完npm install后,对前台页面进行gulp操作时,报如下错误: const { Math, Object } = primordials; [错误日志] ***@**** M ...