#github.com/xulang-org/packages/rabbitmq
Xu 0.16.0 API
导入方式: rabbitmq #github.com/xulang-org/packages/rabbitmq
公开 API: 20 函数, 27 方法, 5 类型, 10 常量, 0 方法集
扩展包 `rabbitmq #github.com/xulang-org/packages/rabbitmq`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
示例代码 · API 声明
示例代码
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
37_standalone_rabbitmq/main.xu
所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码
@main
rabbitmq #github.com/xulang-org/packages/rabbitmq
#errors
fn verify_rabbitmq_basic() {
println("--- 1. Testing RabbitMQ Client AMQP 0-9-1 Basic Operations ---")
$client := rabbitmq.open("127.0.0.1", 5672, "guest", "guest", "/") >>> (err) {
println(" [FAIL] RabbitMQ connect failed: " + err.msg())
<<
}
println(" [PASS] RabbitMQ AMQP Handshake & Channel Open succeeded.")
// 1. Declare Queue
queue_name := "xu_test_queue"
client.declare_queue(queue_name + "", true) >>> (err) {
println(" [FAIL] declare_queue failed: " + err.msg())
client.close()
<<
}
println(" [PASS] Queue '" + queue_name + "' declared.")
// 2. Publish Message
msg := "Antigravity RabbitMQ AMQP test message from Xu Language!"
println(" Publishing message to queue '" + queue_name + "'...")
client.publish("", queue_name + "", msg + "") >>> (err) {
println(" [FAIL] publish failed: " + err.msg())
client.close()
<<
}
println(" [PASS] Message published.")
// 3. Get / Consume Message
println(" Consuming message via Basic.Get...")
got := client.get(queue_name + "", true) >>> (err) {
println(" [FAIL] get failed: " + err.msg())
client.close()
<<
}
println(" Retrieved message: " + got)
if got == msg {
println(" [PASS] Retrieved message matches published content exactly!")
} else {
println(" [FAIL] Message mismatch: " + got)
}
client.close()
println(" [PASS] Client closed cleanly.")
}
fn verify_rabbitmq_pool() {
println("\n--- 2. Testing RabbitMQ Connection Pool ---")
$p := rabbitmq.open_pool("127.0.0.1", 5672, "guest", "guest", "/", 5)
// 1. Connection Reuse Test
$c1 := p.get() >>> (err) {
println(" [FAIL] Failed to get c1: " + err.msg())
<<
}
fd1 := c1.fd
println(" Borrow 1 fd: " + str(fd1))
p.put(c1)
$c2 := p.get() >>> (err) {
println(" [FAIL] Failed to get c2: " + err.msg())
<<
}
fd2 := c2.fd
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
}
// 2. Multi-connection concurrent borrow
$c3 := p.get() >>> (err) {
println(" [FAIL] Failed to get c3: " + err.msg())
<<
}
fd3 := c3.fd
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put(c2)
p.put(c3)
// 3. Delegated operations via pool
println(" Testing delegated operations via pool...")
pool_queue := "xu_pool_queue"
p.declare_queue(pool_queue + "", true) >>> (err) {
println(" [FAIL] Pool declare_queue failed: " + err.msg())
<<
}
println(" [PASS] Pool declared queue: " + pool_queue)
pool_msg := "Pooled rabbitmq message payload from Xu"
p.publish("", pool_queue + "", pool_msg + "") >>> (err) {
println(" [FAIL] Pool publish failed: " + err.msg())
<<
}
println(" [PASS] Pool published message.")
got_pool := p.get_message(pool_queue + "", true) >>> (err) {
println(" [FAIL] Pool get_message failed: " + err.msg())
<<
}
println(" Retrieved via pool: " + got_pool)
if got_pool == pool_msg {
println(" [PASS] Pool message matches published content exactly!")
} else {
println(" [FAIL] Pool message mismatch!")
}
p.close()
println(" [PASS] RabbitMQ Pool closed cleanly.")
}
fn main() {
println("=== Xu RabbitMQ AMQP 0-9-1 Native Driver Verification ===\n")
verify_rabbitmq_basic()
verify_rabbitmq_pool()
println("\n[RABBITMQ VERIFICATION 100% COMPLETED]")
}
API 声明
open
pub fn open(host string, port int, user string, password string, vhost string) Client
用途
打开资源或建立网络连接。
参数
host string- 主机(string)
port int- 端口(int)
user string- user(string)
password string- password(string)
vhost string- vhost(string)
返回值
Client — 该类型的值
查看用法示例
open_default
pub fn open_default() Client
用途
执行 open default 操作。
参数
无需传入参数。
返回值
Client — 该类型的值
struct Message
pub struct Message {
pub exchange string
pub routing_key string
pub body string
pub message_count int
}
用途
承载 exchange、routing_key、body、message_count 等属性的结构体。
公开字段
exchange string- 字段(string)
routing_key string- 字段(string)
body string- 正文
message_count int- 字段(int)
struct Client
pub struct Client {
pub fd int
pub host string
pub port int
pub user string
pub password string
pub vhost string
pub channel_id int
}
用途
Rabbitmq 客户端会话实例,维护与服务端的连接并提供操作接口。
公开字段
fd int- 文件描述符
host string- 主机
port int- 端口
user string- 字段(string)
password string- 字段(string)
vhost string- 字段(string)
channel_id int- 字段(int)
($&Client) write_exact
pub fn (c $&Client) write_exact(data string) int
用途
$&Client 上的操作:执行 write exact 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string- 数据(string)
返回值
int — 该类型的值
($&Client) read_exact
pub fn (c $&Client) read_exact(n int) string
用途
$&Client 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
n int- 数量、长度或上限(int)
返回值
string — 结果字符串
($&Client) send_frame
pub fn (c $&Client) send_frame(frame_type int, channel int, payload string) int
用途
$&Client 上的操作:执行 send frame 操作。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
frame_type int- frame_type(int)
channel int- channel(int)
payload string- payload(string)
返回值
int — 该类型的值
($&Client) read_frame
pub fn (c $&Client) read_frame() Frame
用途
$&Client 上的操作:执行 read frame 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
Frame — 该类型的值
connect
pub fn connect(host string, port int, user string, password string, vhost string) Client
用途
执行 connect 处理。失败时抛出错误,调用方需处理或继续传播。
参数
host string- 主机(string)
port int- 端口(int)
user string- user(string)
password string- password(string)
vhost string- vhost(string)
返回值
Client — 该类型的值
($&Client) declare_queue
pub fn (c $&Client) declare_queue(queue_name string, durable bool) int
用途
$&Client 上的操作:执行 declare queue 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
queue_name string- queue_name(string)
durable bool- durable(bool)
返回值
int — 该类型的值
($&Client) declare_exchange
pub fn (c $&Client) declare_exchange(exchange_name string, exchange_type string, durable bool) int
用途
$&Client 上的操作:执行 declare exchange 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
exchange_name string- exchange_name(string)
exchange_type string- exchange_type(string)
durable bool- durable(bool)
返回值
int — 该类型的值
($&Client) bind_queue
pub fn (c $&Client) bind_queue(queue_name string, exchange_name string, routing_key string) int
用途
$&Client 上的操作:执行 bind queue 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
queue_name string- queue_name(string)
exchange_name string- exchange_name(string)
routing_key string- routing_key(string)
返回值
int — 该类型的值
($&Client) publish
pub fn (c $&Client) publish(exchange_name string, routing_key string, message_body string) int
用途
向指定的消息主题或频道广播发布消息。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
exchange_name string- exchange_name(string)
routing_key string- routing_key(string)
message_body string- message_body(string)
返回值
int — 该类型的值
($&Client) get
pub fn (c $&Client) get(queue_name string, auto_ack bool) string
用途
在 $&Client 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
queue_name string- queue_name(string)
auto_ack bool- auto_ack(bool)
返回值
string — 结果字符串
($&Client) ping
pub fn (c $&Client) ping() bool
用途
发送心跳保活探测请求,检测连接活性与服务可用性。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
bool — 是否成立
($&Client) close
pub fn (c $&Client) close()
用途
$&Client 上的操作:关闭资源。
参数
c $&Client- 本次调用使用的
Client 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
不返回值。
struct Pool
pub struct Pool {
pub host string
pub port int
pub user string
pub password string
pub vhost string
pub idle chan int
pub max_idle int
}
用途
RabbitMQ AMQP 连接池管理器。
公开字段
host string- 主机
port int- 端口
user string- 字段(string)
password string- 字段(string)
vhost string- 字段(string)
idle chan int- 字段(通道;类型 chan int)
max_idle int- 字段(int)
new_pool
pub fn new_pool(host string, port int, user string, password string, vhost string, max_idle int) Pool
用途
构造 pool。
参数
host string- 主机(string)
port int- 端口(int)
user string- user(string)
password string- password(string)
vhost string- vhost(string)
max_idle int- max_idle(int)
返回值
Pool — 该类型的值
open_pool
pub fn open_pool(host string, port int, user string, password string, vhost string, max_idle int) Pool
用途
执行 open pool 操作。
参数
host string- 主机(string)
port int- 端口(int)
user string- user(string)
password string- password(string)
vhost string- vhost(string)
max_idle int- max_idle(int)
返回值
Pool — 该类型的值
查看用法示例
(&Pool) get
pub fn (p &Pool) get() Client
用途
在 &Pool 上执行 get 操作。失败时抛出错误,调用方需处理或继续传播。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
返回值
Client — 该类型的值
(&Pool) get_conn
pub fn (p &Pool) get_conn() int
用途
&Pool 上的操作:读取 conn。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
返回值
int — 该类型的值
(&Pool) put_conn
pub fn (p &Pool) put_conn(fd int)
用途
&Pool 上的操作:执行 put conn 操作。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
fd int- 文件描述符(int)
返回值
不返回值。
(&Pool) put
pub fn (p &Pool) put(c Client)
用途
写入或更新指定键对应的值数据。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
c Client- 字节或字符码(Client)
返回值
不返回值。
(&Pool) declare_queue
pub fn (p &Pool) declare_queue(queue_name string, durable bool) int
用途
&Pool 上的操作:执行 declare queue 操作。失败时抛出错误,调用方需处理或继续传播。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
queue_name string- queue_name(string)
durable bool- durable(bool)
返回值
int — 该类型的值
(&Pool) publish
pub fn (p &Pool) publish(exchange_name string, routing_key string, body string) int
用途
向指定的消息主题或频道广播发布消息。失败时抛出错误,调用方需处理或继续传播。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
exchange_name string- exchange_name(string)
routing_key string- routing_key(string)
body string- 正文(string)
返回值
int — 该类型的值
(&Pool) get_message
pub fn (p &Pool) get_message(queue_name string, auto_ack bool) string
用途
&Pool 上的操作:读取 message。失败时抛出错误,调用方需处理或继续传播。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
queue_name string- queue_name(string)
auto_ack bool- auto_ack(bool)
返回值
string — 结果字符串
(&Pool) ping
pub fn (p &Pool) ping() bool
用途
发送心跳保活探测请求,检测连接活性与服务可用性。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
返回值
bool — 是否成立
(&Pool) close
用途
&Pool 上的操作:关闭资源。
参数
p &Pool- 本次调用使用的
Pool 实例;以只读借用传入,调用方仍持有实例。
返回值
不返回值。
const FRAME_METHOD
pub const FRAME_METHOD = 1
用途
常量 FRAME_METHOD,定义了相关的协议或算法配置参数。
const FRAME_BODY
用途
常量 FRAME_BODY,定义了相关的协议或算法配置参数。
const FRAME_HEARTBEAT
pub const FRAME_HEARTBEAT = 8
用途
常量 FRAME_HEARTBEAT,定义了相关的协议或算法配置参数。
const FRAME_END
pub const FRAME_END = 206
用途
常量 FRAME_END,定义了相关的协议或算法配置参数。
const CLASS_CONNECTION
pub const CLASS_CONNECTION = 10
用途
常量 CLASS_CONNECTION,定义了相关的协议或算法配置参数。
const CLASS_CHANNEL
pub const CLASS_CHANNEL = 20
用途
常量 CLASS_CHANNEL,定义了相关的协议或算法配置参数。
const CLASS_EXCHANGE
pub const CLASS_EXCHANGE = 40
用途
常量 CLASS_EXCHANGE,定义了相关的协议或算法配置参数。
const CLASS_QUEUE
pub const CLASS_QUEUE = 50
用途
常量 CLASS_QUEUE,定义了相关的协议或算法配置参数。
const CLASS_BASIC
pub const CLASS_BASIC = 60
用途
常量 CLASS_BASIC,定义了相关的协议或算法配置参数。
struct Frame
pub struct Frame {
pub frame_type int
pub channel int
pub payload string
}
用途
Rabbitmq 协议数据帧结构封装。
公开字段
frame_type int- 字段(int)
channel int- 字段(int)
payload string- 字段(string)
encode_i8
pub fn encode_i8(v int) string
用途
编码 i8 数据。
参数
v int- 值(int)
返回值
string — 结果字符串
encode_i16
pub fn encode_i16(v int) string
用途
编码 i16 数据。
参数
v int- 值(int)
返回值
string — 结果字符串
encode_i32
pub fn encode_i32(v int) string
用途
编码 i32 数据。
参数
v int- 值(int)
返回值
string — 结果字符串
encode_i64
pub fn encode_i64(v int) string
用途
编码 i64 数据。
参数
v int- 值(int)
返回值
string — 结果字符串
decode_i8
pub fn decode_i8(s string, off int) int
用途
解码 i8 数据。
参数
s string- 源字符串(string)
off int- 偏移(int)
返回值
int — 该类型的值
decode_i16
pub fn decode_i16(s string, off int) int
用途
解码 i16 数据。
参数
s string- 源字符串(string)
off int- 偏移(int)
返回值
int — 该类型的值
decode_i32
pub fn decode_i32(s string, off int) int
用途
解码 i32 数据。
参数
s string- 源字符串(string)
off int- 偏移(int)
返回值
int — 该类型的值
decode_i64
pub fn decode_i64(s string, off int) int
用途
解码 i64 数据。
参数
s string- 源字符串(string)
off int- 偏移(int)
返回值
int — 该类型的值
encode_shortstr
pub fn encode_shortstr(s string) string
用途
编码 shortstr 数据。
参数
s string- 源字符串(string)
返回值
string — 结果字符串
encode_longstr
pub fn encode_longstr(s string) string
用途
编码 longstr 数据。
参数
s string- 源字符串(string)
返回值
string — 结果字符串
encode_empty_table
pub fn encode_empty_table() string
用途
编码 empty table 数据。
参数
无需传入参数。
返回值
string — 结果字符串
build_frame
pub fn build_frame(frame_type int, channel int, payload string) string
用途
执行 build frame 操作。
参数
frame_type int- frame_type(int)
channel int- channel(int)
payload string- payload(string)
返回值
string — 结果字符串
build_method_payload
pub fn build_method_payload(class_id int, method_id int, arguments string) string
用途
执行 build method payload 操作。
参数
class_id int- class_id(int)
method_id int- method_id(int)
arguments string- arguments(string)
返回值
string — 结果字符串
struct TlsClient
pub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}
用途
Rabbitmq 客户端会话实例,维护与服务端的连接并提供操作接口。
公开字段
conn transport.Conn- 字段(transport.Conn)
host string- 主机
port int- 端口
tls_connect
pub fn tls_connect(endpoint string, default_port int) TlsClient
用途
执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
参数
endpoint string- endpoint(string)
default_port int- default_port(int)
返回值
TlsClient — 该类型的值
tls_connect_config
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient
用途
执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
参数
endpoint string- endpoint(string)
default_port int- default_port(int)
cfg tls.Config- cfg(tls.Config)
返回值
TlsClient — 该类型的值
($&TlsClient) write_all
pub fn (c $&TlsClient) write_all(data string) int
用途
$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string- 数据(string)
返回值
int — 该类型的值
($&TlsClient) read
pub fn (c $&TlsClient) read(max int) string
用途
$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int- 上限(int)
返回值
string — 结果字符串
($&TlsClient) read_exact
pub fn (c $&TlsClient) read_exact(need int) string
用途
$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int- need(int)
返回值
string — 结果字符串
($&TlsClient) close
pub fn (c $&TlsClient) close() int
用途
$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
返回值
int — 该类型的值
($&TlsClient) set_deadline
pub fn (c $&TlsClient) set_deadline(t_ns int) int
用途
$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int- t_ns(int)
返回值
int — 该类型的值
($&TlsClient) set_read_deadline
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int
用途
$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int- t_ns(int)
返回值
int — 该类型的值
($&TlsClient) set_write_deadline
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int
用途
$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
参数
c $&TlsClient- 本次调用使用的
TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int- t_ns(int)
返回值
int — 该类型的值