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 ...
随机推荐
- CSS display的几个常用的属性值,inline , block, inline-block
1.解释一下display的几个常用的属性值,inline , block, inline-block inline(行内元素): 使元素变成行内元素,拥有行内元素的特性,即可以与其他行内元素共享一行 ...
- JSON+如何处理JSON字符串
JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读.尽管JSON是在Javascript的一个子集,但JSON是独立于语言的文本格 ...
- 网站集成Paypal
国际化Paypal是一个不错的选择,现在很多的app都是H5,所以网站集成即可操作了. 最方便快捷的集成方式,目前Paypal的网站收款需要企业账号,不过它最开始的老版本是可以个人账号收款的.如下是个 ...
- 关于Yii的ocracle链接问题
1. http://www.yiiframework.com/extension/oci8pdo/ 2.下载extension包,根据配置可解决.
- PHPBase64格式编码图片
base64_encode编码图片 /** * 获取图片的Base64编码(不支持url) * @date 2017-02-20 19:41:22 * * @param $img_file 传入本地图 ...
- ASP.NET Core 入门笔记 1,项目概览
(1)新建项目选择ASP.NET Core Web应用程序 (2)程序会自动安装相应的包组件,此时依赖项会有感叹号,等待安装完毕感叹号消失 (3)在项目的文件夹下建立其他文件,都会在项目资源视图中显示 ...
- Spring MVC 源码 分析
spring web 源码 @HandlesTypes(WebApplicationInitializer.class) public class SpringServletContainerInit ...
- 【Spring 基础】通过注解注入Bean
原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...
- 西安邀请赛-D(带权并查集+背包)
题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...
- Spring IOC 和Aspectj AOP
1.Aspectj AOP 是一套独立的AOP 解决方案,不仅限于java应用,不依赖其他方案,属于编译时增强,有自己单独的编译器.Spring AOP 是基于Spring 容器的的AOP解决方式,属 ...