public void OnMessage(Message message) { messageCount++; LOG.Debug("Message listener count = " + messageCount); TextMessage textMessage = message as TextMessage; if (textMessage != null) { LOG.Info("Message Text = " + textMessage.Text); } else { LOG.Warn("Can not process message of type " + message.GetType()); } }
/// <summary> /// Convert from a EMS Message to a .NET object. /// </summary> /// <param name="messageToConvert">the message to convert</param> /// <returns>the converted .NET object</returns> /// <throws>MessageConversionException in case of conversion failure </throws> public object FromMessage(Message messageToConvert) { if (messageToConvert == null) { throw new MessageConversionException("Can't convert null message"); } try { string converterId = messageToConvert.GetStringProperty(typeMapper.TypeIdFieldName); if (converterId == null) { return defaultMessageConverter.FromMessage(messageToConvert); } TextMessage textMessage = messageToConvert as TextMessage; if (textMessage == null) { throw new MessageConversionException("Can't convert message of type " + messageToConvert.GetType()); } using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(textMessage.Text))) { XmlSerializer xs = new XmlSerializer(GetTargetType(textMessage)); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); return xs.Deserialize(memoryStream); } } catch (Exception e) { throw new MessageConversionException("Can't convert message of type " + messageToConvert.GetType(), e); } }
/// <summary> /// Invoke the specified listener as Spring ISessionAwareMessageListener, /// exposing a new EMS Session (potentially with its own transaction) /// to the listener if demanded. /// </summary> /// <param name="listener">The Spring ISessionAwareMessageListener to invoke.</param> /// <param name="session">The session to operate on.</param> /// <param name="message">The received message.</param> /// <exception cref="EMSException">If thrown by EMS API methods.</exception> /// <see cref="ISessionAwareMessageListener"/> /// <see cref="ExposeListenerSession"/> protected virtual void DoInvokeListener(ISessionAwareMessageListener listener, ISession session, Message message) { IConnection conToClose = null; ISession sessionToClose = null; try { ISession sessionToUse = session; if (!ExposeListenerSession) { //We need to expose a separate Session. conToClose = CreateConnection(); sessionToClose = CreateSession(conToClose); sessionToUse = sessionToClose; } // Actually invoke the message listener if (logger.IsDebugEnabled) { logger.Debug("Invoking listener with message of type [" + message.GetType() + "] and session [" + sessionToUse + "]"); } listener.OnMessage(message, sessionToUse); // Clean up specially exposed Session, if any if (sessionToUse != session) { if (sessionToUse.Transacted && SessionTransacted) { // Transacted session created by this container -> commit. EmsUtils.CommitIfNecessary(sessionToUse); } } } finally { EmsUtils.CloseSession(sessionToClose); EmsUtils.CloseConnection(conToClose); } }