### Connect to AMQP Broker using Configuration Object Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Establishes a connection to an AMQP broker using a configuration object. This method allows for more granular control over connection parameters and relies on Swift-NIO for network operations. ```swift let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) var connection: AMQPConnection do { connection = try await AMQPConnection.connect(use: eventLoopGroup.next(), from: .init(connection: .plain, server: .init())) print("Succesfully connected") } catch { print("Error while connecting", error) } ``` -------------------------------- ### Open AMQP Channel Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Opens a new channel on an existing AMQP connection. Channels are multiplexed over a single connection and are used for performing AMQP operations. Errors during channel opening are caught and logged. ```swift var channel: AMQPChannel do { channel = try await connection.openChannel() print("Succesfully opened a channel") } catch { print("Error while opening a channel", error) } ``` -------------------------------- ### Connect to AMQP Broker using Connection String Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Establishes a connection to an AMQP broker using a provided connection string. It utilizes a MultiThreadedEventLoopGroup for managing NIO threads and handles potential connection errors. ```swift let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) var connection: AMQPConnection do { connection = try await AMQPConnection.connect(use: eventLoopGroup.next(), from: .init(url: "amqp://guest:guest@localhost:5672/%2f")) print("Succesfully connected") } catch { print("Error while connecting", error) } ``` -------------------------------- ### Publish Message to Queue Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Publishes a message to a specified queue via the AMQP channel. The message content is provided as a ByteBuffer. Errors during publishing are caught and reported. ```swift do { let deliveryTag = try await channel.basicPublish( from: ByteBuffer(string: "{}"), exchange: "", routingKey: "test" ) print("Succesfully publish a message") } catch { print("Error while publishing a message", error) } ``` -------------------------------- ### Retry Pattern for Channel/Connection Errors in Swift Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Demonstrates a simple retry pattern to re-create channels or connections when errors like `AMQPConnectionError.channelClosed` or `AMQPConnectionError.connectionClosed` occur during operations. ```swift let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) var connection = try await AMQPConnection.connect(use: eventLoopGroup.next(), from: .init(connection: .plain, server: .init())) var channel = try await connection.openChannel() for _ in 0..<3 { do { let deliveryTag = try await channel.basicPublish( from: ByteBuffer(string: "{}"), exchange: "", routingKey: "test" ) break } catch AMQPConnectionError.channelClosed { do { channel = try await connection.openChannel() } catch AMQPConnectionError.connectionClosed { connection = try await AMQPConnection.connect(use: eventLoopGroup.next(), from: .init(connection: .plain, server: .init())) channel = try await connection.openChannel() } } catch { print("Unknown problem", error) } } ``` -------------------------------- ### Safe Channel Pattern in Swift Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Implements a safe channel pattern by wrapping standard connection and channel objects in a class. This ensures a reusable channel is available before operations like publishing. ```swift @available(macOS 12.0, *) class SimpleSafeConnection { private let eventLoop: EventLoop private let config: AMQPConnectionConfiguration private var channel: AMQPChannel? private var connection: AMQPConnection? init(eventLoop: EventLoop, config: AMQPConnectionConfiguration) { self.eventLoop = eventLoop self.config = config } func reuseChannel() async throws -> AMQPChannel { guard let channel = self.channel, channel.isOpen else { if self.connection == nil || self.connection!.isConnected { self.connection = try await AMQPConnection.connect(use: self.eventLoop, from: self.config) } self.channel = try await connection!.openChannel() return self.channel! } return channel } } let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) let connection = SimpleSafeConnection(eventLoop: eventLoopGroup.next(), config: .init(connection: .plain, server: .init())) while(true) { let deliveryTag = try await connection.reuseChannel().basicPublish( from: ByteBuffer(string: "{}"), exchange: "", routingKey: "test" ) } ``` -------------------------------- ### Mixed Recovery Patterns (Safe Channel + Retry) in Swift Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Combines the safe channel pattern with a retry mechanism to handle channel closure errors. It attempts to reuse the channel and re-establishes it if a `channelClosed` error is encountered. ```swift let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) let connection = SimpleSafeConnection(eventLoop: eventLoopGroup.next(), config: .init(connection: .plain, server: .init())) var channel = try await connection.reuseChannel() for _ in 0..<3 { do { let deliveryTag = try await channel.basicPublish( from: ByteBuffer(string: "{}"), exchange: "", routingKey: "test" ) break } catch AMQPConnectionError.channelClosed { channel = try await connection.reuseChannel() } catch { print("Unknown problem", error) } } ``` -------------------------------- ### Set Quality of Service (QOS) Limit Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Sets a Quality of Service (QOS) limit for message prefetching on the channel. This helps prevent memory overflow on the consumer by limiting the number of unacknowledged messages. ```swift try await channel.basicQos(count: 1000) ``` -------------------------------- ### Declare AMQP Queue Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Declares a queue on the AMQP broker. This operation can specify queue properties like durability. If the queue already exists, it will not be recreated. Errors during declaration are handled. ```swift do { try await channel.queueDeclare(name: "test", durable: false) print("Succesfully created queue") } catch { print("Error while creating queue", error) } ``` -------------------------------- ### Close AMQP Channel and Connection Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Gracefully closes the AMQP channel and the underlying connection to the broker. This ensures that all resources are released properly. Errors encountered during the closing process are handled. ```swift do { try await channel.close() try await connection.close() print("Succesfully closed", msg) } catch { print("Error while closing", error) } ``` -------------------------------- ### Manually Cancel Consumer Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Allows for manual cancellation of an active message consumer using its unique consumer tag. This is an alternative to relying on automatic cancellation during deinitialization. ```swift try await channel.basicCancel(consumerTag: consumer.name) ``` -------------------------------- ### Consume Multiple Messages as AsyncThrowingStream Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Consumes messages from a queue as an asynchronous stream. This allows for continuous message processing. The consumer is automatically cancelled upon deinitialization or can be manually cancelled using its tag. ```swift do { let consumer = try await channel.basicConsume(queue: "test") for try await msg in consumer { print("Succesfully consumed a message", msg) break } } catch { print("Delivery failure", error) } ``` -------------------------------- ### Consume Single Message Source: https://github.com/funcmike/rabbitmq-nio/blob/main/README.md Retrieves a single message from a specified queue. If no message is available, it returns nil. This is a non-blocking operation that returns the message or indicates its absence. Errors during consumption are handled. ```swift do { guard let msg = try await channel.basicGet(queue: "test") else { print("No message currently available") return } print("Succesfully consumed a message", msg) } catch { print("Error while consuming a message", error) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.