### STOMP 1.2 Connection Example Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/CHANGELOG.md Illustrates the basic connection process for STOMP 1.2, including starting the connection, and connecting with username, password, and wait option. Constructor arguments have changed to accommodate protocol and transport separation. ```python conn = stomp.Connection([('localhost', 61613)]) conn.start() conn.connect('admin', 'password', wait=True) ``` -------------------------------- ### Install Stomp.py via pip Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Commands to set up a virtual environment and install the library. ```shell $ virtualenv -p python3 teststomp $ . teststomp/bin/activate (teststomp) $ pip install stomp.py (teststomp) $ stomp --version ``` -------------------------------- ### Install Dependencies for Testing Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Install project dependencies using poetry. This command is typically run before building Docker images or running tests. ```bash poetry install ``` -------------------------------- ### Quick Start: Connect and Send Message Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.rst Connect to a message broker, send a message, and disconnect. Ensure 'sys' is imported if using sys.argv. ```python import stomp conn = stomp.Connection() conn.connect('admin', 'password', wait=True) conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test') conn.disconnect() ``` -------------------------------- ### Run Podman Container for Testing Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Start the Podman container for testing. This command uses 'make' and assumes the Podman image has been built. ```bash make run-podman ``` -------------------------------- ### Command-line Subscription Listener Example Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/CHANGELOG.md Demonstrates how to use the command-line client to subscribe to a queue. The verbose option controls whether headers are written to stdout. ```bash > stomp -H localhost -P 61613 -L /queue/test ``` -------------------------------- ### Run Docker Container for Testing Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Start the Docker container for testing. This command uses 'make' and assumes the Docker image has been built. ```bash make run-docker ``` -------------------------------- ### Command-line Client Usage Example Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/CHANGELOG.md Shows how to run the stomp command-line client with host and port specified. This provides an alternative to running the script directly via python. ```bash > stomp -H localhost -P 61613 ``` -------------------------------- ### Connect to STOMP server via CLI Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md Methods to initiate a connection to a STOMP server using the module or the installed binary. ```default python -m stomp -H localhost -P 61613 ``` ```default stomp -H localhost -P 61613 ``` -------------------------------- ### Get help for specific CLI commands Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md Retrieve usage information for advanced CLI-specific commands. ```default > help run Usage: run Description: Execute commands in a specified file ``` ```default > help sendfile Usage: sendfile Required Parameters: destination - where to send the message filename - the file to send Description: Sends a file to a destination in the messaging system. ``` ```default > help stats Usage: stats [on|off] Description: Record statistics on messages sent, received, errors, etc. If no argument (on|off) is specified, dump the current statistics. ``` -------------------------------- ### Build Podman Image for Testing Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Build the Podman image required for running tests. This command uses 'make' and assumes Podman is installed. ```bash make podman-image ``` -------------------------------- ### Build Docker Image for Testing Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Build the Docker image required for running tests. This command uses 'make' and assumes Docker is installed. ```bash make docker-image ``` -------------------------------- ### Implement a STOMP listener and message sender Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Example of defining a custom listener class and performing basic connection, subscription, and message transmission. ```default import time import sys import stomp class MyListener(stomp.ConnectionListener): def on_error(self, frame): print('received an error "%s"' % frame.body) def on_message(self, frame): print('received a message "%s"' % frame.body) conn = stomp.Connection() conn.set_listener('', MyListener()) conn.connect('admin', 'password', wait=True) conn.subscribe(destination='/queue/test', id=1, ack='auto') conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test') time.sleep(2) conn.disconnect() ``` -------------------------------- ### View CLI help and options Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md Display available startup arguments and command-line options. ```default $ stomp --help Stomp.py command-line client Usage: stomp [options] Options: --version Show the version number and exit -h, --help Show this help message and exit -H , --host= Hostname or IP address to connect to. [default: localhost] -P , --port= Port providing stomp protocol connections. [default: 61613] -U , --user= Username for the connection -W , --password= Password for the connection -F , --file= File containing commands to be executed, instead of prompting from the command prompt. -S , --protocol= Set the STOMP protocol version (1.0, 1.1, 1.2) [default: 1.1] -L , --listen= Listen for messages on a queue/destination -V, --verbose Verbose logging "on" or "off" (if on, full headers from stomp server responses are printed) --heartbeats= Heartbeats to request when connecting with protocol >= 1.1 (two comma separated integers required) [default: 0,0] --ssl Enable SSL connection --ssl-key-file= ssl key file --ssl-cert-file= ssl cert file --ssl-ca-file= ssl ca certs file ``` -------------------------------- ### View command-line client help Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Command to display available startup arguments for the CLI. ```shell $ stomp --help ``` -------------------------------- ### Connect using the command-line client Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Basic command to initiate a connection to a STOMP broker. ```shell $ stomp -H localhost -P 61613 ``` -------------------------------- ### Connect with authentication in the command-line client Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Command to connect to a broker using specific credentials. ```shell $ stomp -H localhost -P 61613 -U admin -W password ``` -------------------------------- ### Send and Receive Messages with PrintingListener Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Demonstrates setting up a connection, attaching a PrintingListener to observe interactions, connecting, subscribing to a queue, sending a message, and receiving it. The listener outputs connection events, sent frames, and received messages. ```python >>> from stomp import * >>> c = Connection([('127.0.0.1', 62613)]) >>> c.set_listener('', PrintingListener()) >>> c.connect('admin', 'password', wait=True) on_connecting 127.0.0.1 62613 on_send STOMP {'passcode': 'password', 'login': 'admin', 'accept-version': '1.2', 'host': '127.0.0.1'} on_connected {'server': 'apache-apollo/1.7.1', 'host-id': 'mybroker', 'session': 'mybroker-13e0', 'heart-beat': '100,10000', 'version': '1.2', 'user-id': 'admin'} >>> c.subscribe('/queue/test', 123) on_send SUBSCRIBE {'id': 123, 'ack': 'auto', 'destination': '/queue/test'} >>> c.send('/queue/test', 'a test message') on_send SEND {'content-length': 5, 'destination': '/queue/test'} b'a test message' on_before_message {'destination': '/queue/test', 'message-id': 'mybroker-13e01', 'subscription': '123', 'ack': '2', 'content-length': '5'} a test message on_message {'destination': '/queue/test', 'message-id': 'mybroker-13e01', 'subscription': '123', 'ack': '2', 'content-length': '5'} a test message ``` -------------------------------- ### Establish STOMP 1.1 Connection Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Connect to a STOMP message broker running on the local machine using default STOMP 1.1. Requires 'admin' and 'password' credentials. ```python >>> import stomp >>> c = stomp.Connection([('127.0.0.1', 62613)]) >>> c.connect('admin', 'password', wait=True) ``` -------------------------------- ### Establish Specific STOMP Version Connections Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Instantiate connections for STOMP 1.0, 1.1, or 1.2 by using the respective Connection classes. ```python >>> c = stomp.Connection10([('127.0.0.1', 62613)]) ``` ```python >>> c = stomp.Connection11([('127.0.0.1', 62613)]) ``` ```python >>> c = stomp.Connection12([('127.0.0.1', 62613)]) ``` -------------------------------- ### List internal application commands Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md View available commands within the interactive CLI session. ```default > help Documented commands (type help ): ======================================== EOF begin help rollback sendfile stats ver abort commit nack run sendrec subscribe version ack exit quit send sendreply unsubscribe ``` -------------------------------- ### Establish STOMP Connection with Failover Addresses Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Configure a STOMP connection to attempt connections to multiple brokers for failover. The client will try each address in the list until a connection is established. ```python >>> import stomp >>> c = stomp.Connection([('192.168.1.100', 61613), ('192.168.1.101', 62613)]) ``` -------------------------------- ### Run stomp.py Unit Tests Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Execute the unit tests for the stomp.py library. This command should be run after the Docker container is active. ```bash make test ``` -------------------------------- ### Execute STOMP commands Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md Commands for subscribing to destinations and sending messages after a successful connection. ```default subscribe /queue/test send /queue/test hello world ``` -------------------------------- ### Authenticate with STOMP server Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/commandline.md Connect to the server using specific credentials. ```default stomp -H localhost -P 61613 -U admin -W password ``` -------------------------------- ### Establish STOMP Connection with IPv6 Address Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Connect to a STOMP broker using an IPv6 address. Ensure the interface name is correctly specified if required by the system. ```python >>> import stomp >>> c = stomp.Connection(['fe80::a00:27ff:fe90:3f1a%en1', 62613]) ``` -------------------------------- ### Execute the STOMP test script Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/quickstart.md Command to run the previously defined Python script. ```shell $ python stomptest.py This is a test received a message "This is a test" ``` -------------------------------- ### STOMP Transactions: Commit Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Begin a transaction using conn.begin(), send messages with the transaction ID, and then commit the transaction using conn.commit() to send all messages atomically. ```default >>> conn.subscribe('/queue/test', id=5) >>> txid = conn.begin() >>> conn.send('/queue/test', 'test1', transaction=txid) >>> conn.send('/queue/test', 'test2', transaction=txid) >>> conn.send('/queue/test', 'test3', transaction=txid) >>> conn.commit(txid) on_message {'subscription': '5', 'content-length': '5', 'destination': '/queue/test', 'message-id': 'mybroker-14b03', 'transaction': 'b39f3136-46a3-4e11-8ba8-845e36d48412'} test1 on_message {'subscription': '5', 'content-length': '5', 'destination': '/queue/test', 'message-id': 'mybroker-14b04', 'transaction': 'b39f3136-46a3-4e11-8ba8-845e36d48412'} test2 on_message {'subscription': '5', 'content-length': '5', 'destination': '/queue/test', 'message-id': 'mybroker-14b05', 'transaction': 'b39f3136-46a3-4e11-8ba8-845e36d48412'} test3 ``` -------------------------------- ### Client Acknowledgements and Nacks Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Subscribe to a queue with client-side acknowledgements enabled. Use conn.ack() to acknowledge a message or conn.nack() to negatively acknowledge it, referencing the message and subscription IDs. ```default >>> conn.subscribe('/queue/test', id=4, ack='client') on_before_message {'message-id': 'mybroker-14aa2', 'destination': '/queue/test', 'subscription': '4', 'content-length': '14'} test message 1 on_message {'message-id': 'mybroker-14aa2', 'destination': '/queue/test', 'subscription': '4', 'content-length': '14'} test message 1 >>> conn.ack('mybroker-14aa2', 4) on_before_message {'message-id': 'mybroker-14ab2', 'destination': '/queue/test', 'subscription': '4', 'content-length': '14'} test message 2 on_message {'message-id': 'mybroker-14ab2', 'destination': '/queue/test', 'subscription': '4', 'content-length': '14'} test message 2 >>> conn.nack('mybroker-14ab2', 4) ``` -------------------------------- ### Client-side Message Sender Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md A simple client to send a message to a specified queue. Ensure the server is running and subscribed before sending. ```python import stomp conn = stomp.Connection([('localhost', 62613)]) conn.connect('guest', 'guest', wait=True) conn.send('/queue/test', 'test message') ``` -------------------------------- ### Access Podman Container Network Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Run a command within the Podman container's network namespace to access private IP addresses. This is useful when using Podman in rootless mode. ```bash podman unshare --rootless-netns ``` -------------------------------- ### Set Multiple Listeners Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Attach multiple listeners to a STOMP connection by assigning unique names to each listener. This allows for different handling of events. ```python >>> c.set_listener('stats', StatsListener()) ``` ```python >>> c.set_listener('print', PrintingListener()) ``` -------------------------------- ### Graceful Disconnection with Receipt Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Initiate a graceful disconnection using conn.disconnect(). The connection is only closed after the server acknowledges the receipt. ```default >>> conn.disconnect() on_send DISCONNECT {'receipt': '825a5cd6-9e3c-4a72-8051-72348a94f5ce'} on_receipt {'receipt-id': '825a5cd6-9e3c-4a72-8051-72348a94f5ce'} on_disconnected ``` -------------------------------- ### Remove Docker Container Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Clean up and remove the Docker container after testing. This command uses 'make' and is optional. ```bash make remove-docker ``` -------------------------------- ### Remove Podman Container Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/README.md Clean up and remove the Podman container after testing. This command uses 'make' and is optional. ```bash make remove-podman ``` -------------------------------- ### STOMP Transactions: Abort Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Begin a transaction, send messages, and then abort the transaction using conn.abort() to discard all messages sent within that transaction. ```pycon >>> conn.subscribe('/queue/test', id=6) >>> txid = conn.begin() >>> conn.send('/queue/test', 'test4', transaction=txid) >>> conn.send('/queue/test', 'test5', transaction=txid) >>> conn.abort(txid) ``` -------------------------------- ### Server-side Listener for Disconnects and Reconnection Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md This listener handles connection errors, incoming messages, and disconnections. The on_disconnected method automatically attempts to reconnect. ```python import os import time import stomp def connect_and_subscribe(conn): conn.connect('guest', 'guest', wait=True) conn.subscribe(destination='/queue/test', id=1, ack='auto') class MyListener(stomp.ConnectionListener): def __init__(self, conn): self.conn = conn def on_error(self, frame): print('received an error "%s"' % frame.body) def on_message(self, frame): print('received a message "%s"' % frame.body) for x in range(10): print(x) time.sleep(1) print('processed message') def on_disconnected(self): print('disconnected') connect_and_subscribe(self.conn) conn = stomp.Connection([('localhost', 62613)], heartbeats=(4000, 4000)) conn.set_listener('', MyListener(conn)) connect_and_subscribe(conn) time.sleep(60) conn.disconnect() ``` -------------------------------- ### Unsubscribe from a Queue Source: https://github.com/jasonrbriggs/stomp.py/blob/dev/docs/source/api.md Remove a subscription from a STOMP queue or topic using the unique subscription ID. This stops messages for that subscription from being processed. ```python >>> c.subscribe('/queue/test', 123) >>> c.unsubscribe(123) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.