#github.com/xulang-org/packages/rabbitmq
Xu 0.16.0 API
import: rabbitmq #github.com/xulang-org/packages/rabbitmq
counts: 20 functions, 27 methods, 5 types, 10 constants, 0 method sets
Public API convenience helpers for RabbitMQ driver
github.com/xulang-org/packages · 1.17.2 · Install and use
Examples · API declarations
Examples
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
37_standalone_rabbitmq/main.xu
Required SDK: Xu 0.8.3 · Complete project · Download source
@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 declarations
open
pub fn open(host string, port int, user string, password string, vhost string) Client
Purpose
Open resource or establish connection.
Parameters
host string- host (string)
port int- port (int)
user string- user (string)
password string- password (string)
vhost string- vhost (string)
Returns
Client — a value of this type
See usage example
open_default
pub fn open_default() Client
Purpose
Open default.
Parameters
No arguments are required.
Returns
Client — a value of this type
struct Message
pub struct Message {
pub exchange string
pub routing_key string
pub body string
pub message_count int
}
Purpose
Structure encapsulating exchange, routing_key, body, message_count.
Public fields
exchange string- field (string)
routing_key string- field (string)
body string- body
message_count int- field (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
}
Purpose
Rabbitmq Client session maintaining connection and operations.
Public fields
fd int- file descriptor
host string- host
port int- port
user string- field (string)
password string- field (string)
vhost string- field (string)
channel_id int- field (int)
($&Client) write_exact
pub fn (c $&Client) write_exact(data string) int
Purpose
Operation on $&Client: write exact. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
data string- data (string)
Returns
int — a value of this type
($&Client) read_exact
pub fn (c $&Client) read_exact(n int) string
Purpose
Operation on $&Client: read exact. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
n int- count, length, or limit (int)
Returns
string — result string
($&Client) send_frame
pub fn (c $&Client) send_frame(frame_type int, channel int, payload string) int
Purpose
Operation on $&Client: send frame.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
frame_type int- frame_type (int)
channel int- channel (int)
payload string- payload (string)
Returns
int — a value of this type
($&Client) read_frame
pub fn (c $&Client) read_frame() Frame
Purpose
Operation on $&Client: read frame. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
Frame — a value of this type
connect
pub fn connect(host string, port int, user string, password string, vhost string) Client
Purpose
Execute connect. Throws on failure; handle the error or propagate it to the caller.
Parameters
host string- host (string)
port int- port (int)
user string- user (string)
password string- password (string)
vhost string- vhost (string)
Returns
Client — a value of this type
($&Client) declare_queue
pub fn (c $&Client) declare_queue(queue_name string, durable bool) int
Purpose
Operation on $&Client: declare queue. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
queue_name string- queue_name (string)
durable bool- durable (bool)
Returns
int — a value of this type
($&Client) declare_exchange
pub fn (c $&Client) declare_exchange(exchange_name string, exchange_type string, durable bool) int
Purpose
Operation on $&Client: declare exchange. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
exchange_name string- exchange_name (string)
exchange_type string- exchange_type (string)
durable bool- durable (bool)
Returns
int — a value of this type
($&Client) bind_queue
pub fn (c $&Client) bind_queue(queue_name string, exchange_name string, routing_key string) int
Purpose
Operation on $&Client: bind queue. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
queue_name string- queue_name (string)
exchange_name string- exchange_name (string)
routing_key string- routing_key (string)
Returns
int — a value of this type
($&Client) publish
pub fn (c $&Client) publish(exchange_name string, routing_key string, message_body string) int
Purpose
Publish message data to the given topic or channel. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
exchange_name string- exchange_name (string)
routing_key string- routing_key (string)
message_body string- message_body (string)
Returns
int — a value of this type
($&Client) get
pub fn (c $&Client) get(queue_name string, auto_ack bool) string
Purpose
Execute get on $&Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
queue_name string- queue_name (string)
auto_ack bool- auto_ack (bool)
Returns
string — result string
($&Client) ping
pub fn (c $&Client) ping() bool
Purpose
Send a ping heartbeat request to verify connection liveness.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
bool — true or false
($&Client) close
pub fn (c $&Client) close()
Purpose
Operation on $&Client: close the resource.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
This call does not return a value.
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
}
Purpose
Connection Pool for RabbitMQ AMQP 0-9-1. Manages reusable TCP client connections and channels via scalar channel.
Public fields
host string- host
port int- port
user string- field (string)
password string- field (string)
vhost string- field (string)
idle chan int- field (channel; type chan int)
max_idle int- field (int)
new_pool
pub fn new_pool(host string, port int, user string, password string, vhost string, max_idle int) Pool
Purpose
Construct pool.
Parameters
host string- host (string)
port int- port (int)
user string- user (string)
password string- password (string)
vhost string- vhost (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
open_pool
pub fn open_pool(host string, port int, user string, password string, vhost string, max_idle int) Pool
Purpose
Open pool.
Parameters
host string- host (string)
port int- port (int)
user string- user (string)
password string- password (string)
vhost string- vhost (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
See usage example
(&Pool) get
pub fn (p &Pool) get() Client
Purpose
Execute get on &Pool. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Client — a value of this type
(&Pool) get_conn
pub fn (p &Pool) get_conn() int
Purpose
Operation on &Pool: read conn.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
int — a value of this type
(&Pool) put_conn
pub fn (p &Pool) put_conn(fd int)
Purpose
Operation on &Pool: put connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
(&Pool) put
pub fn (p &Pool) put(c Client)
Purpose
Store or update the value associated with the specified key.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
c Client- byte or character code (Client)
Returns
This call does not return a value.
(&Pool) declare_queue
pub fn (p &Pool) declare_queue(queue_name string, durable bool) int
Purpose
Operation on &Pool: declare queue. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
queue_name string- queue_name (string)
durable bool- durable (bool)
Returns
int — a value of this type
(&Pool) publish
pub fn (p &Pool) publish(exchange_name string, routing_key string, body string) int
Purpose
Publish message data to the given topic or channel. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
exchange_name string- exchange_name (string)
routing_key string- routing_key (string)
body string- body (string)
Returns
int — a value of this type
(&Pool) get_message
pub fn (p &Pool) get_message(queue_name string, auto_ack bool) string
Purpose
Operation on &Pool: read message. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
queue_name string- queue_name (string)
auto_ack bool- auto_ack (bool)
Returns
string — result string
(&Pool) ping
pub fn (p &Pool) ping() bool
Purpose
Send a ping heartbeat request to verify connection liveness.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
bool — true or false
(&Pool) close
Purpose
Operation on &Pool: close the resource.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
This call does not return a value.
const FRAME_METHOD
pub const FRAME_METHOD = 1
Purpose
AMQP 0-9-1 Protocol Constants
const FRAME_BODY
Purpose
Constant FRAME_BODY, defining protocol or algorithm parameters.
const FRAME_HEARTBEAT
pub const FRAME_HEARTBEAT = 8
Purpose
Constant FRAME_HEARTBEAT, defining protocol or algorithm parameters.
const FRAME_END
pub const FRAME_END = 206
Purpose
Constant FRAME_END, defining protocol or algorithm parameters.
const CLASS_CONNECTION
pub const CLASS_CONNECTION = 10
Purpose
Class IDs
const CLASS_CHANNEL
pub const CLASS_CHANNEL = 20
Purpose
Constant CLASS_CHANNEL, defining protocol or algorithm parameters.
const CLASS_EXCHANGE
pub const CLASS_EXCHANGE = 40
Purpose
Constant CLASS_EXCHANGE, defining protocol or algorithm parameters.
const CLASS_QUEUE
pub const CLASS_QUEUE = 50
Purpose
Constant CLASS_QUEUE, defining protocol or algorithm parameters.
const CLASS_BASIC
pub const CLASS_BASIC = 60
Purpose
Constant CLASS_BASIC, defining protocol or algorithm parameters.
struct Frame
pub struct Frame {
pub frame_type int
pub channel int
pub payload string
}
Purpose
Frame structure
Public fields
frame_type int- field (int)
channel int- field (int)
payload string- field (string)
encode_i8
pub fn encode_i8(v int) string
Purpose
Encode i8 data.
Parameters
v int- value (int)
Returns
string — result string
encode_i16
pub fn encode_i16(v int) string
Purpose
Encode i16 data.
Parameters
v int- value (int)
Returns
string — result string
encode_i32
pub fn encode_i32(v int) string
Purpose
Encode i32 data.
Parameters
v int- value (int)
Returns
string — result string
encode_i64
pub fn encode_i64(v int) string
Purpose
Encode i64 data.
Parameters
v int- value (int)
Returns
string — result string
decode_i8
pub fn decode_i8(s string, off int) int
Purpose
Decode i8 data.
Parameters
s string- source string (string)
off int- offset (int)
Returns
int — a value of this type
decode_i16
pub fn decode_i16(s string, off int) int
Purpose
Decode i16 data.
Parameters
s string- source string (string)
off int- offset (int)
Returns
int — a value of this type
decode_i32
pub fn decode_i32(s string, off int) int
Purpose
Decode i32 data.
Parameters
s string- source string (string)
off int- offset (int)
Returns
int — a value of this type
decode_i64
pub fn decode_i64(s string, off int) int
Purpose
Decode i64 data.
Parameters
s string- source string (string)
off int- offset (int)
Returns
int — a value of this type
encode_shortstr
pub fn encode_shortstr(s string) string
Purpose
Short string: 1-byte length prefix (0-255) + content
Parameters
s string- source string (string)
Returns
string — result string
encode_longstr
pub fn encode_longstr(s string) string
Purpose
Long string: 4-byte length prefix + content
Parameters
s string- source string (string)
Returns
string — result string
encode_empty_table
pub fn encode_empty_table() string
Purpose
Empty AMQP field table (4 bytes = 0 length)
Parameters
No arguments are required.
Returns
string — result string
build_frame
pub fn build_frame(frame_type int, channel int, payload string) string
Purpose
Frame builder
Parameters
frame_type int- frame_type (int)
channel int- channel (int)
payload string- payload (string)
Returns
string — result string
build_method_payload
pub fn build_method_payload(class_id int, method_id int, arguments string) string
Purpose
Method payload builder: Class ID (2B) + Method ID (2B) + Arguments
Parameters
class_id int- class_id (int)
method_id int- method_id (int)
arguments string- arguments (string)
Returns
string — result string
struct TlsClient
pub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}
Purpose
Rabbitmq Client session maintaining connection and operations.
Public fields
conn transport.Conn- field (transport.Conn)
host string- host
port int- port
tls_connect
pub fn tls_connect(endpoint string, default_port int) TlsClient
Purpose
Tls connect. Throws on failure; handle the error or propagate it to the caller.
Parameters
endpoint string- endpoint (string)
default_port int- default_port (int)
Returns
TlsClient — a value of this type
tls_connect_config
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient
Purpose
Tls connect config. Throws on failure; handle the error or propagate it to the caller.
Parameters
endpoint string- endpoint (string)
default_port int- default_port (int)
cfg tls.Config- cfg (tls.Config)
Returns
TlsClient — a value of this type
($&TlsClient) write_all
pub fn (c $&TlsClient) write_all(data string) int
Purpose
Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
data string- data (string)
Returns
int — a value of this type
($&TlsClient) read
pub fn (c $&TlsClient) read(max int) string
Purpose
Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
max int- maximum (int)
Returns
string — result string
($&TlsClient) read_exact
pub fn (c $&TlsClient) read_exact(need int) string
Purpose
Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
need int- need (int)
Returns
string — result string
($&TlsClient) close
pub fn (c $&TlsClient) close() int
Purpose
Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
Returns
int — a value of this type
($&TlsClient) set_deadline
pub fn (c $&TlsClient) set_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type
($&TlsClient) set_read_deadline
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type
($&TlsClient) set_write_deadline
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int
Purpose
Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&TlsClient- The
TlsClient instance used by this call (exclusive borrow).
t_ns int- t_ns (int)
Returns
int — a value of this type