aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/common/messaging/producer.py
blob: b6adc0c176ca5b16b4579da09aa5de416ac1f8ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright (c) 2018 Intel Corporation
#
# Licensed 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.

import abc
import logging
import os

from oslo_config import cfg
import oslo_messaging
import six

from yardstick.common import messaging


LOG = logging.getLogger(__name__)


@six.add_metaclass(abc.ABCMeta)
class MessagingProducer(object):
    """Abstract class to implement a MQ producer

    This abstract class allows a class implementing this interface to publish
    messages in a message queue.
    """

    def __init__(self, topic, pid=os.getpid(), fanout=True):
        """Init function.

        :param topic: (string) MQ exchange topic
        :param pid: (int) PID of the process implementing this MQ Notifier
        :param fanout: (bool) MQ clients may request that a copy of the message
                       be delivered to all servers listening on a topic by
                       setting fanout to ``True``, rather than just one of them
        :returns: `MessagingNotifier` class object
        """
        self._topic = topic
        self._pid = pid
        self._fanout = fanout
        self._transport = oslo_messaging.get_rpc_transport(
            cfg.CONF, url=messaging.TRANSPORT_URL)
        self._target = oslo_messaging.Target(topic=topic, fanout=fanout,
                                             server=messaging.SERVER)
        self._notifier = oslo_messaging.RPCClient(self._transport,
                                                  self._target)

    def send_message(self, method, payload):
        """Send a cast message, that will invoke a method without blocking.

        The cast() method is used to invoke an RPC method that does not return
        a value.  cast() RPC requests may be broadcast to all Servers listening
        on a given topic by setting the fanout Target property to ``True``.

        :param methos: (string) method name, that must be implemented in the
                       consumer endpoints
        :param payload: (subclass `Payload`) payload content
        """
        self._notifier.cast({'pid': self._pid},
                            method,
                            **payload.obj_to_dict())