class OutgoingConnector(BaseZMQConnector): """ An outgoing connector started as a subprocess. Each connection to a queue manager gets its own connector. """ def __init__(self, repo_location=None, out_id=None, init=True): super(OutgoingConnector, self).__init__(repo_location, None) self.broker_client_name = 'zmq-outgoing-connector' self.logger = logging.getLogger(self.__class__.__name__) self.out_id = out_id self.out_lock = RLock() self.broker_push_client_pull_port = PORTS.BROKER_PUSH_PUBLISHING_CONNECTOR_ZMQ_PULL self.client_push_broker_pull_port = PORTS.PUBLISHING_CONNECTOR_ZMQ_PUSH_BROKER_PULL self.broker_pub_client_sub_port = PORTS.BROKER_PUB_PUBLISHING_CONNECTOR_ZMQ_SUB if init: self._init() self._setup_connector() def _setup_odb(self): super(OutgoingConnector, self)._setup_odb() item = self.odb.get_out_zmq(self.server.cluster.id, self.out_id) self.out = Bunch() self.out.id = item.id self.out.name = item.name self.out.is_active = item.is_active self.out.address = item.address self.out.socket_type = self.socket_type = item.socket_type self.out.sender = None def filter(self, msg): """ Can we handle the incoming message? """ if super(OutgoingConnector, self).filter(msg): return True elif msg.action in(OUTGOING.ZMQ_SEND, OUTGOING.ZMQ_DELETE, OUTGOING.ZMQ_EDIT): return self.out.name == msg['name'] def _stop_connection(self): """ Stops the given outgoing connection's sender. The method must be called from a method that holds onto all related RLocks. """ if self.out.get('sender'): sender = self.out.sender self.out.clear() sender.close() def _recreate_sender(self): self._stop_connection() if self.out.get('is_active'): factory = self._get_factory(None, self.out.address, None) sender = self._sender(factory) self.out.sender = sender def _sender(self, factory): """ Starts the outgoing connection in a new thread and returns it. """ sender = OutgoingConnection(factory, self.out.name) t = Thread(target=sender._run) t.start() return sender def _setup_connector(self): """ Sets up the connector on startup. """ with self.out_lock: self._recreate_sender() def _close_delete(self): """ Stops the connections, exits the process. """ with self.out_lock: self._stop_connection() self._close() def on_broker_pull_msg_OUTGOING_ZMQ_SEND(self, msg, args=None): """ Puts a message on a socket. """ if not self.out.get('is_active'): log_msg = 'Not sending, the connection is not active [{0}]'.format(self.out) self.logger.info(log_msg) return if self.out.get('sender'): self.out.sender.send(msg.body) else: if self.logger.isEnabledFor(TRACE1): log_msg = 'No sender for [{0}]'.format(self.out) self.logger.log(TRACE1, log_msg) def on_broker_pull_msg_OUTGOING_ZMQ_DELETE(self, msg, args=None): self._close_delete() def on_broker_pull_msg_OUTGOING_ZMQ_EDIT(self, msg, args=None): with self.out_lock: sender = self.out.get('sender') self.out = msg self.out.sender = sender self._recreate_sender()
class OutgoingConnector(BaseJMSWMQConnector): """ An outgoing connector started as a subprocess. Each connection to a queue manager gets its own connector. """ def __init__(self, repo_location=None, def_id=None, out_id=None, init=True): super(OutgoingConnector, self).__init__(repo_location, def_id) self.broker_client_name = 'jms-wmq-outgoing-connector' self.logger = logging.getLogger(self.__class__.__name__) self.out_id = out_id self.out_lock = RLock() self.def_lock = RLock() self.broker_push_client_pull_port = PORTS.BROKER_PUSH_PUBLISHING_CONNECTOR_JMS_WMQ_PULL self.client_push_broker_pull_port = PORTS.PUBLISHING_CONNECTOR_JMS_WMQ_PUSH_BROKER_PULL self.broker_pub_client_sub_port = PORTS.BROKER_PUB_PUBLISHING_CONNECTOR_JMS_WMQ_SUB if init: self._init() self._setup_connector() def _setup_odb(self): super(OutgoingConnector, self)._setup_odb() item = self.odb.get_def_jms_wmq(self.server.cluster.id, self.def_id) self.def_ = Bunch() self.def_.name = item.name self.def_.id = item.id self.def_.host = item.host self.def_.port = item.port self.def_.queue_manager = item.queue_manager self.def_.channel = item.channel self.def_.cache_open_send_queues = item.cache_open_send_queues self.def_.cache_open_receive_queues = item.cache_open_receive_queues self.def_.use_shared_connections = item.use_shared_connections self.def_.ssl = item.ssl self.def_.ssl_cipher_spec = item.ssl_cipher_spec self.def_.ssl_key_repository = item.ssl_key_repository self.def_.needs_mcd = item.needs_mcd self.def_.max_chars_printed = item.max_chars_printed item = self.odb.get_out_jms_wmq(self.server.cluster.id, self.out_id) self.out = Bunch() self.out.id = item.id self.out.name = item.name self.out.is_active = item.is_active self.out.delivery_mode = item.delivery_mode self.out.priority = item.priority self.out.expiration = item.expiration self.out.sender = None def filter(self, msg): """ Can we handle the incoming message? """ if super(OutgoingConnector, self).filter(msg): return True elif msg.action in(OUTGOING.JMS_WMQ_SEND, OUTGOING.JMS_WMQ_DELETE, OUTGOING.JMS_WMQ_EDIT): return self.out.name == msg['name'] def _stop_connection(self): """ Stops the given outgoing connection's sender. The method must be called from a method that holds onto all related RLocks. """ if self.out.get('sender'): sender = self.out.sender self.out.clear() sender.close() def _recreate_sender(self): self._stop_connection() if self.out.is_active: factory = self._get_factory() sender = self._sender(factory) self.out.sender = sender def _sender(self, factory): """ Starts the outgoing connection in a new thread and returns it. """ sender = OutgoingConnection(factory, self.out.name) t = Thread(target=sender._run) t.start() return sender def _setup_connector(self): """ Sets up the connector on startup. """ with self.out_lock: with self.def_lock: self._recreate_sender() def _close_delete(self): """ Stops the connections, exits the process. """ with self.def_lock: with self.out_lock: self._stop_connection() self._close() def on_broker_pull_msg_DEFINITION_JMS_WMQ_EDIT(self, msg, args=None): with self.def_lock: with self.out_lock: self.def_ = msg self._recreate_sender() def on_broker_pull_msg_OUTGOING_JMS_WMQ_SEND(self, msg, args=None): """ Puts a message on a queue. """ if not self.out.get('is_active'): log_msg = 'Not sending, the connection is not active [{0}]'.format(self.out) self.logger.info(log_msg) return if self.out.get('sender'): if self.out.get('sender').factory._is_connected: self.out.sender.send(msg, self.out.delivery_mode, self.out.expiration, self.out.priority, self.def_.max_chars_printed) else: if self.logger.isEnabledFor(logging.DEBUG): log_msg = 'Not sending, the factory for [{0}] is not connected'.format(self.out) self.logger.debug(log_msg) else: if self.logger.isEnabledFor(TRACE1): log_msg = 'No sender for [{0}]'.format(self.out) self.logger.log(TRACE1, log_msg) def on_broker_pull_msg_OUTGOING_JMS_WMQ_DELETE(self, msg, args=None): self._close_delete() def on_broker_pull_msg_OUTGOING_JMS_WMQ_EDIT(self, msg, args=None): with self.def_lock: with self.out_lock: sender = self.out.get('sender') self.out = msg self.out.sender = sender self._recreate_sender()