def send(self, message, destination): if self._disconnecting: self.logger.info( "Connection factory disconnecting, aborting receive") return else: self.logger.log(TRACE1, "send -> not disconnecting") if not self._is_connected: self.logger.log(TRACE1, "send -> _is_connected1 %s" % self._is_connected) self._connect() self.logger.log(TRACE1, "send -> _is_connected2 %s" % self._is_connected) destination = self._strip_prefixes_from_destination(destination) # Will consist of an MQRFH2 header and the actual business payload. buff = StringIO() # Build the message descriptor (MQMD) md = self._build_md(message) # Create MQRFH2 header now = long(time() * 1000) mqrfh2jms = MQRFH2JMS(self.needs_mcd).build_header( message, destination, self.CMQC, now) buff.write(mqrfh2jms) if message.text != None: buff.write(message.text.encode("utf-8")) body = buff.getvalue() buff.close() queue = self.get_queue_for_sending(destination) try: queue.put(body, md) except self.mq.MQMIError, e: self.logger.error( "MQMIError in queue.put, e.comp [%s], e.reason [%s] " % (e.comp, e.reason)) exc = WebSphereMQJMSException(e, e.comp, e.reason) raise exc
def receive(self, destination, wait_interval): if self._disconnecting: self.logger.info( "Connection factory disconnecting, aborting receive") return else: self.logger.log(TRACE1, "receive -> not disconnecting") if not self._is_connected: self.logger.log( TRACE1, "receive -> _is_connected1 %s" % self._is_connected) self._connect() self.logger.log( TRACE1, "receive -> _is_connected2 %s" % self._is_connected) queue = self.get_queue_for_receiving(destination) try: # Default message descriptor .. md = self.mq.md() # .. and custom get message options gmo = self.mq.gmo() gmo.Options = self.CMQC.MQGMO_WAIT | self.CMQC.MQGMO_FAIL_IF_QUIESCING gmo.WaitInterval = wait_interval message = queue.get(None, md, gmo) return self._build_text_message(md, message) except self.mq.MQMIError, e: if e.reason == self.CMQC.MQRC_NO_MSG_AVAILABLE: text = "No message available for destination [%s], " \ "wait_interval [%s] ms" % (destination, wait_interval) raise NoMessageAvailableException(text) else: self.logger.log( TRACE1, "Exception caught in get, e.comp=[%s], e.reason=[%s]" % (e.comp, e.reason)) exc = WebSphereMQJMSException(e, e.comp, e.reason) raise exc
def _connect(self): if self._is_connected: return conn_name = "%s(%s)" % (self.host, self.listener_port) self.logger.info("Connecting to queue manager [%s], channel [%s]" \ ", connection info [%s]" % (self.queue_manager, self.channel, conn_name)) self.mgr = self.mq.QueueManager(None) sco = self.mq.sco() cd = self.mq.cd() cd.ChannelName = self.channel cd.ConnectionName = conn_name cd.ChannelType = self.CMQC.MQCHT_CLNTCONN cd.TransportType = self.CMQC.MQXPT_TCP if self.ssl: if not (self.ssl_cipher_spec and self.ssl_key_repository): msg = "SSL support requires setting both ssl_cipher_spec and ssl_key_repository" self.logger.error(msg) raise JMSException(msg) sco.KeyRepository = self.ssl_key_repository cd.SSLCipherSpec = self.ssl_cipher_spec if self.use_shared_connections: connect_options = self.CMQC.MQCNO_HANDLE_SHARE_BLOCK else: connect_options = self.CMQC.MQCNO_HANDLE_SHARE_NONE try: self.mgr.connectWithOptions(self.queue_manager, cd=cd, opts=connect_options, sco=sco) except self.mq.MQMIError, e: exc = WebSphereMQJMSException(e, e.comp, e.reason) raise exc
def _build_text_message(self, md, message): self.logger.log(TRACE1, "Building a text message [%r], md [%r]" % (repr(message), repr(md))) mqrfh2 = MQRFH2JMS() mqrfh2.build_folders_and_payload_from_message(message) jms_folder = mqrfh2.folders.get("jms", None) mcd_folder = mqrfh2.folders.get("mcd", None) usr_folder = mqrfh2.folders.get("usr", None) # Create a message instance .. text_message = TextMessage() if usr_folder: for attr_name, attr_value in usr_folder.items(): setattr(text_message, attr_name, str(attr_value)) # .. set its JMS properties .. if jms_folder: if jms_folder.find("Dst") is not None: text_message.jms_destination = jms_folder.find("Dst").text.strip() if jms_folder.find("Exp") is not None: text_message.jms_expiration = long(jms_folder.find("Exp").text) else: text_message.jms_expiration = 0 # Same as in Java if jms_folder.find("Cid") is not None: text_message.jms_correlation_id = jms_folder.find("Cid").text if md.Persistence == self.CMQC.MQPER_NOT_PERSISTENT: text_message.jms_delivery_mode = DELIVERY_MODE_NON_PERSISTENT elif md.Persistence in(self.CMQC.MQPER_PERSISTENT, self.CMQC.MQPER_PERSISTENCE_AS_Q_DEF): text_message.jms_delivery_mode = DELIVERY_MODE_PERSISTENT else: text = "Don't know how to handle md.Persistence mode [%s]" % (md.Persistence) self.logger.error(text) exc = WebSphereMQJMSException(text) raise exc if md.ReplyToQ.strip(): self.logger.log(TRACE1, "Found md.ReplyToQ=[%r]" % md.ReplyToQ) text_message.jms_reply_to = "queue://" + md.ReplyToQMgr.strip() + "/" + md.ReplyToQ.strip() text_message.jms_priority = md.Priority text_message.jms_message_id = _WMQ_ID_PREFIX + hexlify(md.MsgId) text_message.jms_timestamp = self._get_jms_timestamp_from_md(md.PutDate.strip(), md.PutTime.strip()) text_message.jms_redelivered = bool(int(md.BackoutCount)) text_message.JMSXUserID = md.UserIdentifier.strip() text_message.JMSXAppID = md.PutApplName.strip() text_message.JMSXDeliveryCount = md.BackoutCount text_message.JMSXGroupID = md.GroupId.strip() text_message.JMSXGroupSeq = md.MsgSeqNumber md_report_to_jms = { self.CMQC.MQRO_EXCEPTION: "Exception", self.CMQC.MQRO_EXPIRATION: "Expiration", self.CMQC.MQRO_COA: "COA", self.CMQC.MQRO_COD: "COD", self.CMQC.MQRO_PAN: "PAN", self.CMQC.MQRO_NAN: "NAN", self.CMQC.MQRO_PASS_MSG_ID: "Pass_Msg_ID", self.CMQC.MQRO_PASS_CORREL_ID: "Pass_Correl_ID", self.CMQC.MQRO_DISCARD_MSG: "Discard_Msg", } for report_name, jms_header_name in md_report_to_jms.iteritems(): report_value = md.Report & report_name if report_value: header_value = report_value else: header_value = None setattr(text_message, "JMS_IBM_Report_" + jms_header_name, header_value) text_message.JMS_IBM_MsgType = md.MsgType text_message.JMS_IBM_Feedback = md.Feedback text_message.JMS_IBM_Format = md.Format.strip() text_message.JMS_IBM_PutApplType = md.PutApplType text_message.JMS_IBM_PutDate = md.PutDate.strip() text_message.JMS_IBM_PutTime = md.PutTime.strip() if md.MsgFlags & self.CMQC.MQMF_LAST_MSG_IN_GROUP: text_message.JMS_IBM_Last_Msg_In_Group = self.CMQC.MQMF_LAST_MSG_IN_GROUP else: text_message.JMS_IBM_Last_Msg_In_Group = None # .. and its payload too. if mqrfh2.payload: text_message.text = mqrfh2.payload return text_message
sco.KeyRepository = self.ssl_key_repository cd.SSLCipherSpec = self.ssl_cipher_spec if self.use_shared_connections: connect_options = self.CMQC.MQCNO_HANDLE_SHARE_BLOCK else: connect_options = self.CMQC.MQCNO_HANDLE_SHARE_NONE try: self.mgr.connectWithOptions(self.queue_manager, cd=cd, opts=connect_options, sco=sco) except self.mq.MQMIError, e: exc = WebSphereMQJMSException(e, e.comp, e.reason) raise exc except Exception, e: self.logger.error("Could not connect to queue manager, e=[%s]" % e) exc = WebSphereMQJMSException(e, None, None) raise exc else: self._is_connected = True self.logger.info("Successfully connected to queue manager [%s]" \ ", channel [%s], connection info [%s]" % (self.queue_manager, self.channel, conn_name)) def _get_queue_from_cache(self, destination, cache): lock = RLock() lock.acquire() try: # Will usually choose this path and find the queue here. if destination in cache: return cache[destination] else: self.logger.debug("Adding queue [%s] to the cache" % destination)