dabla commented on code in PR #62790: URL: https://github.com/apache/airflow/pull/62790#discussion_r3176775117
########## providers/ibm/mq/tests/system/ibm/mq/example_dag_message_queue_trigger.py: ########## @@ -0,0 +1,48 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +# [START howto_trigger_message_queue] +from airflow.providers.common.messaging.triggers.msg_queue import MessageQueueTrigger +from airflow.sdk import DAG, Asset, AssetWatcher, task + +# Define a trigger that listens to an external message queue (IBM MQ in this case) +trigger = MessageQueueTrigger( + queue="ibmmq://mq_default/MY.QUEUE.NAME", +) + +mq_topic_asset = Asset( + "mq_topic_asset", + watchers=[AssetWatcher(name="mq_watcher", trigger=trigger)], +) + +with DAG(dag_id="example_ibm_mq_watcher", schedule=[mq_topic_asset]) as dag: + + @task + def process_message(**context): + for event in context["triggering_asset_events"][mq_topic_asset]: + # Get the message from the TriggerEvent payload + print("Processing event: ", event) + payload = event["payload"] Review Comment: Here is an example of the code we use and of which the returned payload is an XML: ``` from airflow.providers.common.messaging.triggers.msg_queue import MessageQueueTrigger from airflow.sdk import dag, Asset, AssetWatcher, task trigger = MessageQueueTrigger( queue="ibmmq://mq/AQ", ) mq_topic_asset = Asset( "mq_topic_asset", watchers=[AssetWatcher(name="mq_watcher", trigger=trigger)], ) @dag( description=__doc__.partition(".")[0], doc_md=__doc__, schedule=[mq_topic_asset], ) def event_driven_dag(): @task def process_message(**context): import xmltodict for event in context["triggering_asset_events"][mq_topic_asset]: # Get the message from the TriggerEvent payload print(f"Processing message: {event}") data = xmltodict.parse(event.extra["payload"]) print(f"data: {data}") process_message() ``` Below is output of the process_message log: ``` Log message source details sources=["http://159b6bc220ec:8793/log/dag_id=event_driven_dag/run_id=asset_triggered__2026-05-02T14:40:01.728596+00:00_vztoX1oL/task_id=process_message/attempt=1.log"] [2026-05-02 16:40:02] INFO - Stats instance was created in PID 1 but accessed in PID 160. Re-initializing. [2026-05-02 16:40:05] INFO - DAG bundles loaded: dags-folder [2026-05-02 16:40:05] INFO - Filling up the DagBag from /usr/local/airflow/dags/test_mq_dag.py [2026-05-02 16:40:05] INFO - Processing message: asset=AssetReferenceAssetEventDagRun(name='mq_topic_asset', uri='mq_topic_asset', extra={}) extra={'from_trigger': True, 'payload': '<map><elt name="oldRootFolder">file:/var/opt/app/arte2/envs/u00t/interfac/exporter/publish/20260424-1700/</elt><elt name="rootFolder">file:/var/opt/app/arte2/envs/u00t/interfac/exporter/publish/20260425-1700/</elt><elt name="validFrom">20260426</elt><elt name="info">New data available.</elt></map>'} source_task_id=None source_dag_id=None source_run_id=None source_map_index=-1 source_aliases=[] timestamp=datetime.datetime(2026, 5, 2, 14, 39, 59, 732884, tzinfo=datetime.timezone.utc) [2026-05-02 16:40:05] INFO - data: {'map': {'elt': [{'@name': 'oldRootFolder', '#text': 'file:/var/opt/app/publish/20260424-1700/'}, {'@name': 'rootFolder', '#text': 'file:/var/opt/app/publish/20260425-1700/'}, {'@name': 'validFrom', '#text': '20260426'}, {'@name': 'info', '#text': 'New data available.'}]}} [2026-05-02 16:40:05] INFO - Processing message: asset=AssetReferenceAssetEventDagRun(name='mq_topic_asset', uri='mq_topic_asset', extra={}) extra={'from_trigger': True, 'payload': '<map><elt name="oldRootFolder">file:/var/opt/app/publish/20260424-1700/</elt><elt name="rootFolder">file:/var/opt/app/publish/20260425-1700/</elt><elt name="validFrom">20260426</elt><elt name="info">New data available.</elt></map>'} source_task_id=None source_dag_id=None source_run_id=None source_map_index=-1 source_aliases=[] timestamp=datetime.datetime(2026, 5, 2, 14, 40, 1, 725627, tzinfo=datetime.timezone.utc) [2026-05-02 16:40:05] INFO - data: {'map': {'elt': [{'@name': 'oldRootFolder', '#text': 'file:/var/opt/app/publish/20260424-1700/'}, {'@name': 'rootFolder', '#text': 'file:/var/opt/app/publish/20260425-1700/'}, {'@name': 'validFrom', '#text': '20260426'}, {'@name': 'info', '#text': 'New data available.'}]}} [2026-05-02 16:40:05] INFO - Done. Returned value was: None [2026-05-02 16:40:05] INFO - Processing message: asset=AssetReferenceAssetEventDagRun(name='mq_topic_asset', uri='mq_topic_asset', extra={}) extra={'from_trigger': True, 'payload': '<map><elt name="oldRootFolder">file:/var/opt/app/publish/20260424-1700/</elt><elt name="rootFolder">file:/var/opt/app/publish/20260425-1700/</elt><elt name="validFrom">20260426</elt><elt name="info">New data available.</elt></map>'} source_task_id=None source_dag_id=None source_run_id=None source_map_index=-1 source_aliases=[] timestamp=datetime.datetime(2026, 5, 2, 14, 40, 0, 723806, tzinfo=datetime.timezone.utc) [2026-05-02 16:40:05] INFO - data: {'map': {'elt': [{'@name': 'oldRootFolder', '#text': 'file:/var/opt/app/publish/20260424-1700/'}, {'@name': 'rootFolder', '#text': 'file:/var/opt/app/publish/20260425-1700/'}, {'@name': 'validFrom', '#text': '20260426'}, {'@name': 'info', '#text': 'New data available.'}]}} ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
