Files
amq-adapter-python/amqp/adapter/amq_route_factory.py
T
2025-02-20 15:45:26 +08:00

61 lines
1.8 KiB
Python

import logging
from amqp.model.model import AMQRoute
RS = ":"
NULL_ROUTE = {
"componentName": "",
"exchange": None,
"queue": None,
"routingKey": "#",
"timeout": 1,
"validUntil": 0
}
class AMQRouteFactory:
@staticmethod
def from_string(data):
try:
keys = data.split(RS)
if len(keys) >= 5:
i = 0
component_name = keys[i]
i += 1
exchange = keys[i]
i += 1
queue = keys[i] if i < len(keys) else ""
i += 1
routing_key = keys[i] if i < len(keys) else ""
i += 1
timeout = int(keys[i]) if i < len(keys) else 0
i += 1
valid_until = int(keys[i]) if i < len(keys) else 0
return AMQRoute(
componentName=component_name,
exchange=exchange,
queue=queue,
key=routing_key,
timeout=timeout,
validUntil=valid_until
)
logging.error(
" [E] AMQRoute incorrect format. AMQRoute expects input as: "
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
"got: ['%s']", data
)
except Exception as err:
logging.error(
" [E] %s: AMQRoute incorrect format. AMQRoute expects input as: "
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
"got: ['%s']", err, data
)
return NULL_ROUTE
@staticmethod
def validate(route_str):
if route_str is not None:
return route_str.count(":") > 4
return False