1 # Copyright (c) 2018 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from oslo_config import cfg
23 from yardstick.common import messaging
26 LOG = logging.getLogger(__name__)
29 @six.add_metaclass(abc.ABCMeta)
30 class MessagingProducer(object):
31 """Abstract class to implement a MQ producer
33 This abstract class allows a class implementing this interface to publish
34 messages in a message queue.
37 def __init__(self, topic, _id=os.getpid(), fanout=True):
40 :param topic: (string) MQ exchange topic
41 :param id: (int) ID of the process implementing this MQ Notifier
42 :param fanout: (bool) MQ clients may request that a copy of the message
43 be delivered to all servers listening on a topic by
44 setting fanout to ``True``, rather than just one of them
45 :returns: `MessagingNotifier` class object
50 self._transport = oslo_messaging.get_rpc_transport(
51 cfg.CONF, url=messaging.TRANSPORT_URL)
52 self._target = oslo_messaging.Target(topic=topic, fanout=fanout,
53 server=messaging.SERVER)
54 self._notifier = oslo_messaging.RPCClient(self._transport,
57 def send_message(self, method, payload):
58 """Send a cast message, that will invoke a method without blocking.
60 The cast() method is used to invoke an RPC method that does not return
61 a value. cast() RPC requests may be broadcast to all Servers listening
62 on a given topic by setting the fanout Target property to ``True``.
64 :param methos: (string) method name, that must be implemented in the
66 :param payload: (subclass `Payload`) payload content
68 self._notifier.cast({'id': self._id},
70 **payload.obj_to_dict())
74 """Return MQ producer ID"""