Search in sources :

Example 1 with EventMessage

use of org.jaffa.soa.services.event.EventMessage in project jaffa-framework by jaffa-projects.

the class JmsClientHelper method send.

/**
 * Writes a JMS Message to the destination, as defined in the configuration file.
 * @param inst An instance of this class. If passed, then the newly created Message will simply be cached to be sent later. Else it will be sent right away.
 * @param payload Any serializable object.
 * @param userId A header element in the JMS message will be set to this value.
 * @param scheduledTaskId A header element in the JMS message will be set to this value.
 * @return the MessageId.
 * @throws FrameworkException Indicates some system error.
 * @throws ApplicationExceptions Indicates application error(s).
 */
private static String send(JmsClientHelper inst, Object payload, String userId, String scheduledTaskId) throws FrameworkException, ApplicationExceptions {
    Session session = inst != null ? inst.m_session : null;
    try {
        if (log.isDebugEnabled())
            log.debug("Input to send is userId=" + userId + ", scheduledTaskId=" + scheduledTaskId + ", payload=" + payload);
        if (userId == null) {
            // If the input userId is null, then use SecurityManager.getPrincipal().getName()
            if (SecurityManager.getPrincipal() != null)
                userId = SecurityManager.getPrincipal().getName();
        }
        if (userId == null || userId.length() == 0) {
            log.error("Error in locating the user id info for " + payload.getClass().getName());
            throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.USERID_NOT_FOUND, new Object[] { payload.getClass().getName() });
        }
        // Reads the MessageInfo for the className of the input payload
        MessageInfo messageInfo = null;
        try {
            messageInfo = ConfigurationService.getInstance().getMessageInfo(payload.getClass().getName());
            if (messageInfo == null)
                throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.MESSAGE_INFO_MISSING, new Object[] { payload.getClass().getName() });
        } catch (ClassNotFoundException e) {
            log.error("Error in locating the Message info for " + payload.getClass().getName(), e);
            throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.MESSAGE_INFO_MISSING, new Object[] { payload.getClass().getName() }, e);
        }
        // Invokes the LockingService, to ensure that no locks exist if specified in the MessageInfo
        LockingService.checkLock(payload, messageInfo);
        // Process the payload if RULE_POST_IMMEDIATE is true
        Boolean postImmediate = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(RULE_POST_IMMEDIATE));
        if (postImmediate != null && postImmediate.booleanValue()) {
            if (log.isDebugEnabled())
                log.debug("jaffa.messaging.postImmediate=true");
            if (log.isDebugEnabled())
                log.debug("Rule '" + RULE_POST_IMMEDIATE + "' is enabled. Will process the payload immediately");
            // Process the payload
            JaffaMessageConsumer.processPayload(messageInfo, payload, userId, scheduledTaskId, null);
            // Return a null since no message has been created.
            return null;
        } else {
            if (log.isDebugEnabled())
                log.debug("jaffa.messaging.postImmediate=false");
            // Create a Session, if not passed
            if (inst == null) {
                final Connection connection = JaffaConnectionFactory.obtainConnection();
                session = obtainSession(connection, false);
            }
            // Marshals the payload into XML
            String xml = marshalPayload(payload);
            // Obtain the destination from the payload, if it implements IHasDestinationName.
            // Fallback to the static message config, if the payload does not provide any destination.
            String queueName = payload instanceof IHasDestinationName ? ((IHasDestinationName) payload).getQueueName() : null;
            if (log.isDebugEnabled())
                log.debug("Destination from the payload: queueName=" + queueName);
            if (queueName == null) {
                queueName = messageInfo.getQueueName();
                if (log.isDebugEnabled())
                    log.debug("Fallback to the static message config: queueName=" + queueName);
            }
            String topicName = payload instanceof IHasDestinationName ? ((IHasDestinationName) payload).getTopicName() : null;
            if (topicName == null)
                topicName = messageInfo.getTopicName();
            // Creates the JMS Message and sets the standard properties
            Message message = session.createTextMessage(xml);
            message.setStringProperty(JmsBrowser.HEADER_USER_ID, userId);
            message.setStringProperty(JmsBrowser.HEADER_SCHEDULED_TASK_ID, scheduledTaskId);
            message.setStringProperty(JmsBrowser.HEADER_DATA_BEAN_CLASS_NAME, payload.getClass().getName());
            if (queueName != null)
                message.setStringProperty(JmsBrowser.HEADER_ORIGINAL_QUEUE_NAME, queueName);
            // Sets the header elements as defined in the configuration file.
            if (messageInfo.getHeader() != null && messageInfo.getHeader().getParam() != null) {
                for (Param param : messageInfo.getHeader().getParam()) {
                    Object value = InitialContextFactrory.obtainParamValue(param, payload);
                    message.setObjectProperty(param.getName(), value);
                }
            }
            // Sets additional header elements if the payload implements the IHasHeaderParams interface
            if (payload instanceof IHasHeaderParams) {
                HeaderParam[] headerParams = ((IHasHeaderParams) payload).getHeaderParams();
                if (headerParams != null) {
                    for (HeaderParam headerParam : headerParams) message.setStringProperty(headerParam.getName(), headerParam.getValue());
                }
            }
            // Sets additional header elements if the payload has header params.
            if (payload instanceof EventMessage) {
                List<org.jaffa.soa.services.event.HeaderParam> headerParams = ((EventMessage) payload).getHeaderParams();
                if (headerParams != null) {
                    for (org.jaffa.soa.services.event.HeaderParam headerParam : headerParams) {
                        message.setStringProperty(headerParam.getName(), headerParam.getValue());
                    }
                }
            }
            if ((queueName == null || 0 == queueName.length()) && (topicName == null || 0 == topicName.length())) {
                return message.getJMSMessageID();
            }
            // Sends the Message for a local Session only. The Message will be sent later when part of a transaction
            if (inst == null) {
                if (queueName != null && 0 < queueName.length())
                    send(session, message, queueName);
                if (topicName != null && 0 < topicName.length())
                    send(session, message, topicName);
            } else {
                if (inst.m_messages == null)
                    inst.m_messages = new LinkedHashMap<Message, String>();
                if (queueName != null && 0 < queueName.length())
                    inst.m_messages.put(message, queueName);
                if (topicName != null && 0 < topicName.length())
                    inst.m_messages.put(message, topicName);
            }
            // Return the message id
            return message.getJMSMessageID();
        }
    } catch (JMSException e) {
        log.error("Error in sending the message", e);
        throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.SEND_ERROR, null, e);
    } finally {
        if (inst == null && session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                log.warn("Error in closing a JMS Session", e);
            }
        }
    }
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) EventMessage(org.jaffa.soa.services.event.EventMessage) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageInfo(org.jaffa.modules.messaging.services.configdomain.MessageInfo) EventMessage(org.jaffa.soa.services.event.EventMessage) Param(org.jaffa.modules.messaging.services.configdomain.Param) Session(javax.jms.Session)

Example 2 with EventMessage

use of org.jaffa.soa.services.event.EventMessage in project jaffa-framework by jaffa-projects.

the class EventMessageService method getEventMessage.

/**
 * This method will map the SOAEventQueueMessage to EventMessage.
 *
 * @param SOAEventQueueMessage
 * @return EventMessage
 */
public static EventMessage getEventMessage(SOAEventQueueMessage eventQueueMessage) {
    if (log.isDebugEnabled()) {
        log.debug("Converting SOAEventQueueMessage to EventMessage");
    }
    EventMessage eventMessage = new EventMessage();
    eventMessage.setEventId(eventQueueMessage.getEventId());
    eventMessage.setEventName(eventQueueMessage.getEventName());
    eventMessage.setDescription(eventQueueMessage.getDescription());
    eventMessage.setCategory(eventQueueMessage.getCategory());
    eventMessage.setCreatedBy(eventQueueMessage.getCreatedBy());
    eventMessage.setCreatedOn(XMLDateConverter.getCalendarFromDate(eventQueueMessage.getCreatedOn() != null ? eventQueueMessage.getCreatedOn().getUtilDate() : new DateTime().getUtilDate()));
    org.jaffa.modules.messaging.services.HeaderParam[] headerParams = eventQueueMessage.getHeaderParams();
    if (headerParams != null) {
        for (org.jaffa.modules.messaging.services.HeaderParam headerParam : headerParams) {
            HeaderParam eventHeaderParams = new HeaderParam();
            eventHeaderParams.setName(headerParam.getName());
            eventHeaderParams.setValue(headerParam.getValue());
            eventMessage.getHeaderParams().add(eventHeaderParams);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Successfully converted SOAEventQueueMessage to EventMessage");
    }
    return eventMessage;
}
Also used : HeaderParam(org.jaffa.soa.services.event.HeaderParam) EventMessage(org.jaffa.soa.services.event.EventMessage) DateTime(org.jaffa.datatypes.DateTime)

Aggregations

EventMessage (org.jaffa.soa.services.event.EventMessage)2 Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 ObjectMessage (javax.jms.ObjectMessage)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 DateTime (org.jaffa.datatypes.DateTime)1 MessageInfo (org.jaffa.modules.messaging.services.configdomain.MessageInfo)1 Param (org.jaffa.modules.messaging.services.configdomain.Param)1 HeaderParam (org.jaffa.soa.services.event.HeaderParam)1