Skip to content

Consumer Loop Script

This guide provides a starter script for continuously consuming messages from the SWIM service using Python and the Apache Qpid Proton library. The script contains SQL filtering and offset tracking capabilities and can be run directly from your terminal as is. It will connect to the METAR service and print all incoming messages and optionally application properties to the console. The script should give you a good starting point for building your own consumer applications.

If you need support or technical guidance contact us at swim.service@dwd.de.


Prerequisites

Ensure you have completed the following setup before proceeding:

  1. A user account and password to access the SWIM service at DWD. If you are not registered, please contact vl.kundenservice@dwd.de and refer to getting started.
  2. You have installed the necessary python libraries (ideally in a dedicated virtual environment):
    pip install python-qpid-proton==0.40.0 python-dotenv==1.1.0 typer==0.20.0 rich==14.0.0
    

Code Example

The following example is a ready script which can be copy and pasted and directly used to connect to the SWIM service and consume messages in a loop.

consumer_loop.py
import typer
from dotenv import load_dotenv
from proton import Described, symbol
from proton.handlers import MessagingHandler
from proton.reactor import Filter
from proton.utils import BlockingConnection
from rich import print
from typing_extensions import Annotated

load_dotenv()


class PrintMessageHandler(MessagingHandler): # (5)!
    def __init__(
        self,
        print_app_properties: bool = True,
        print_message: bool = False
    ):
        super().__init__()
        self.print_app_properties = print_app_properties
        self.print_message = print_message

    def on_message(self, event):
        msg = event.message

        if self.print_app_properties:
            print(msg.properties)

        if not self.print_message:
            return

        if isinstance(msg.body, memoryview):
            print(msg.body.tobytes().decode("utf-8"))
        elif isinstance(msg.body, str):
            print(msg.body)


def consume_message(
    user: Annotated[str, typer.Argument(envvar="SWIM_USER")],
    password: Annotated[str, typer.Argument(envvar="SWIM_PASSWORD")],
):
    conn = BlockingConnection(
        "amqps://swimt.dwd.de",
        user=user,
        password=password
    )

    receiver = conn.create_receiver(
        "/queues/de-dwd.weather.aviation.metar",
        handler=PrintMessageHandler( # (4)!
            print_app_properties=False,
            print_message=True
        ),
        options=Filter( # (1)!
            {
                symbol("rabbitmq:stream-offset-spec"): Described(
                    symbol("rabbitmq:stream-offset-spec"), "30m" # (2)!
                ),
                symbol("sql-filter"): Described(
                    symbol("amqp:sql-filter"),
                    "[properties.location-indicator] = 'EDDF'" # (3)!
                ),
            },
        ),
    )

    if receiver.container is None:
        conn.close()
        raise Exception("Failure in setting up the receiver")

    receiver.container.run()
    conn.close()


if __name__ == "__main__":
    typer.run(consume_message)
  1. This filter enables retrieval of past messages based on an offset specification and SQL filtering. It is entirely optional. If you don't want to use this feature, you can remove the options parameter from the create_receiver call.
  2. This specifies the offset from which to start receiving messages. For details on tracking the offset, refer to the offset tracking tutorial. Here, we use it to retrieve messages from the last 30 minutes (30m).
  3. This specifies an SQL filter on the data. Here, we only receive messages where the location-indicator property equals EDDF (Frankfurt am Main airport).
  4. We us the handler parameter to specify a custom message handler which processes incoming messages.
  5. In case you want to adapt this example, e.g., writing messages to disk, this is the class where incoming messages are handled (specifically in the on_message method). You can modify or substitute this class as needed.

Handling Credentials

Additionally, create a .env file in the same directory as the script to store your SWIM credentials:

.env
SWIM_USER='your_swim_username'
SWIM_PASSWORD='your_swim_password'

We use python-dotenv to load these environment variables automatically.

Running the Script

Now you can run the script using:

python consumer_loop.py

You should see the latest METAR from EDDF which was sent in the last 30 minutes (all METARs are sent every 30 minutes) printed to the console and the loop will continue running, receiving new messages as they arrive.