Trying to publish a message through AMQP via the r...
# kotlin-native
n
Trying to publish a message through AMQP via the rabbitmq-c library ( https://github.com/alanxz/rabbitmq-c ), and it looks as though the message isn't getting through (the Broker has dropped the message). The
amqp_basic_publish
function is returning 0 (this means the message has been successfully transmitted to the Broker). Below is the code that is being used to publish the message:
Copy code
private fun publishMessage(conn: amqp_connection_state_t?) = memScoped {
    val props = alloc<amqp_basic_properties_t>()
    with(props) {
        _flags = (AMQP_BASIC_CONTENT_TYPE_FLAG or AMQP_BASIC_DELIVERY_MODE_FLAG).toUInt()
        // TODO: Figure out how to set the AMQP content type.
//        content_type = amqp_cstring_bytes("text/plain")
        // Persistent delivery mode.
        delivery_mode = 2.toUByte()
    }
    println("Publishing message...")
    val publishRc = amqp_basic_publish(
        state = conn,
        channel = channel,
        exchange = amqp_cstring_bytes(EXCHANGE),
        routing_key = amqp_cstring_bytes(ROUTING_KEY),
        mandatory = 0,
        immediate = 0,
        properties = props.ptr,
        body = amqp_cstring_bytes(MSG_BODY)
    )
    if (publishRc != 0) println("Cannot publish message.")
    Unit
}
After looking through the Broker log I found that the message was dropped because the exchange is incorrect:
Copy code
operation basic.publish caused a channel exception not_found: no exchange 'ø' in vhost '/'
As I suspected there is something strange going on with the String handling.
The exchange is set to amq.topic (this exchange exists in the Broker) in the program however the library turns it into ø (in a terminal it is displayed as a square), which causes the message to be dropped by the Broker.
All strings that are passed to the amqp_basic_publish function must be converted to amqp_bytes_t. The amqp_cstring_bytes function is used to convert a string to amqp_bytes_t.
Below is the mapping for the amqp_cstring_bytes function:
Copy code
@kotlinx.cinterop.internal.CCall public external fun amqp_cstring_bytes(@kotlinx.cinterop.internal.CCall.CString cstr: kotlin.String?): kotlinx.cinterop.CValue<amqpClient.amqp_bytes_t /* = amqpClient.amqp_bytes_t_ */>