Search in sources :

Example 1 with JaffaMessagingFrameworkException

use of org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException in project jaffa-framework by jaffa-projects.

the class MessageViewerTx method buildDto.

/**
 * Obtains the Message based on the messageMode and creates the output.
 */
private MessageViewerOutDto buildDto(MessageViewerInDto input) throws FrameworkException, ApplicationExceptions {
    try {
        Message message = findMessage(input);
        MessageViewerOutDto output = new MessageViewerOutDto();
        output.setJMSMessageID(input.getJMSMessageID());
        output.setError(message.getStringProperty(JmsBrowser.HEADER_ERROR_DETAILS));
        output.setPriority(new Long(message.getJMSPriority()));
        output.setHasPriorityAccess(JmsBrowser.hasChangePriorityAccess(message.getStringProperty(JmsBrowser.HEADER_ORIGINAL_QUEUE_NAME)));
        output.setJMSDestination(message.getJMSDestination());
        output.setJMSDeliveryMode(message.getJMSDeliveryMode());
        output.setJMSTimestamp(message.getJMSTimestamp() != 0 ? new DateTime(message.getJMSTimestamp()) : null);
        output.setJMSCorrelationID(message.getJMSCorrelationID());
        output.setJMSReplyTo(message.getJMSReplyTo());
        try {
            output.setJMSRedelivered(message.getJMSRedelivered());
        } catch (Exception e) {
        // JBossMessaging throws "java.lang.IllegalStateException: This should never be called directly". Do nothing
        }
        output.setJMSType(message.getJMSType());
        output.setJMSExpiration(message.getJMSExpiration());
        if (message instanceof TextMessage)
            output.setPayLoad(((TextMessage) message).getText());
        // Generate a Map of header elements, keyed by the name of each header element
        // Ignore Error Details as we are showing it in a separate section
        Map<String, HeaderElementDto> headerElements = new LinkedHashMap<String, HeaderElementDto>();
        for (Enumeration e = message.getPropertyNames(); e.hasMoreElements(); ) {
            String name = (String) e.nextElement();
            if (!JmsBrowser.HEADER_ERROR_DETAILS.equals(name)) {
                String value = Formatter.format(message.getObjectProperty(name));
                HeaderElementDto headerElement = headerElements.get(name);
                if (headerElement == null) {
                    headerElement = new HeaderElementDto();
                    headerElement.setName(name);
                    headerElements.put(name, headerElement);
                }
                headerElement.setValue(value);
            }
        }
        // Add labels to the header-elements based on the QueueInfo
        // It is possible that a display-param points to a property on the Message (eg. JMSMessageID, JMSPriority etc.)
        // Use bean intropsection to extract the value of that property
        QueueInfo queueInfo = ConfigurationService.getInstance().getQueueInfo(message.getStringProperty(JmsBrowser.HEADER_ORIGINAL_QUEUE_NAME));
        if (queueInfo != null && queueInfo.getDisplayParam() != null) {
            for (DisplayParam displayParam : queueInfo.getDisplayParam()) {
                HeaderElementDto headerElement = headerElements.get(displayParam.getName());
                if (headerElement == null) {
                    try {
                        headerElement = new HeaderElementDto();
                        headerElement.setName(displayParam.getName());
                        headerElement.setLabel(displayParam.getLabel());
                        headerElements.put(displayParam.getName(), headerElement);
                        if (displayParam.getName().equals("JMSTimestamp")) {
                            String value = message.getJMSTimestamp() != 0 ? Formatter.format(new DateTime(message.getJMSTimestamp())) : null;
                            headerElement.setValue(value);
                        } else {
                            String value = Formatter.format(BeanHelper.getField(message, displayParam.getName()));
                            headerElement.setValue(value);
                        }
                    } catch (Exception e) {
                    // do nothing
                    }
                } else {
                    headerElement.setLabel(displayParam.getLabel());
                }
            }
        }
        output.setHeaderElements(headerElements.values().toArray(new HeaderElementDto[headerElements.values().size()]));
        buildBusinessEventLogDto(input, output, message);
        return output;
    } catch (JMSException e) {
        throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.MESSAGE_INFO_MISSING, null, e);
    }
}
Also used : MessageViewerOutDto(org.jaffa.modules.messaging.components.messageviewer.dto.MessageViewerOutDto) QueueInfo(org.jaffa.modules.messaging.services.configdomain.QueueInfo) Enumeration(java.util.Enumeration) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) JMSException(javax.jms.JMSException) DateTime(org.jaffa.datatypes.DateTime) FrameworkException(org.jaffa.exceptions.FrameworkException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) JMSException(javax.jms.JMSException) LinkedHashMap(java.util.LinkedHashMap) HeaderElementDto(org.jaffa.modules.messaging.components.messageviewer.dto.HeaderElementDto) DisplayParam(org.jaffa.modules.messaging.services.configdomain.DisplayParam) TextMessage(javax.jms.TextMessage)

Example 2 with JaffaMessagingFrameworkException

use of org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException in project jaffa-framework by jaffa-projects.

the class TransactionAdmin method invokeHandler.

/**
 * Invokes the intended handler. This Handler must implement the IMessageHandler Interface in order to be invoked.
 *
 * @param uow
 * @param transaction
 * @param methodName
 * @throws JaffaMessagingFrameworkException
 */
private static void invokeHandler(UOW uow, Transaction transaction, String methodName) throws Exception {
    try {
        TransactionPayload tp = transaction.getTransactionPayloadObject();
        Object dataBean = tp != null ? tp.moldInternalPayload() : null;
        if (dataBean != null) {
            // Load transaction configuration
            TransactionInfo transactionInfo = ConfigurationService.getInstance().getTransactionInfo(tp.getInternalMessageClass());
            if (transactionInfo == null) {
                throw new JaffaMessagingFrameworkException(JaffaTransactionFrameworkException.TRANSACTION_INFO_MISSING, new Object[] { tp.getInternalMessageClass() });
            }
            // Obtain the handlerClass
            if (transactionInfo.getToClass() == null || transactionInfo.getToClass().length() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug(methodName + " toClass is not defined in data bean configuration: " + transactionInfo.getDataBean());
                }
                return;
            }
            Class handlerClass = Class.forName(transactionInfo.getToClass());
            // Class dataBeanClass = Class.forName(transactionInfo.getDataBean());
            if (IMessageHandler.class.isAssignableFrom(handlerClass)) {
                // Unmarshals the Message payload into a dataBean using the dataBeanClassName
                Method handlerMethod = null;
                Object handlerObject = null;
                // Obtain the handler method
                try {
                    handlerMethod = handlerClass.getMethod(methodName, new Class[] { UOW.class, Map.class, Object.class });
                } catch (NoSuchMethodException e) {
                    // Hence use the dataBeanClass specified in the messageInfo to get the appropriate handlerMethod
                    if (log.isDebugEnabled()) {
                        log.debug(methodName + " method not found in " + handlerClass.getName());
                    }
                    return;
                }
                handlerObject = handlerClass.newInstance();
                Map<String, String> headerMap = new HashMap<String, String>();
                // Sets the transactionField elements as defined in the configuration file.
                TransactionField[] fields = transaction.getTransactionFieldArray();
                if (fields != null) {
                    for (TransactionField field : fields) {
                        headerMap.put(field.getFieldName(), field.getValue());
                    }
                }
                // Invoke the handler
                if (log.isDebugEnabled()) {
                    log.debug("Invoking the handler " + handlerMethod);
                }
                handlerMethod.invoke(handlerObject, new Object[] { uow, headerMap, dataBean });
            }
        }
    } catch (Exception e) {
        // Just log the error
        log.error("Exception thrown while deleting the transaction. Transaction was: " + transaction, e);
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) TransactionField(org.jaffa.transaction.domain.TransactionField) Method(java.lang.reflect.Method) IllegalPersistentStateRuntimeException(org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) PostLoadFailedException(org.jaffa.persistence.exceptions.PostLoadFailedException) FrameworkException(org.jaffa.exceptions.FrameworkException) QueryFailedException(org.jaffa.persistence.exceptions.QueryFailedException) ApplicationException(org.jaffa.exceptions.ApplicationException) TransactionInfo(org.jaffa.transaction.services.configdomain.TransactionInfo) TransactionPayload(org.jaffa.transaction.domain.TransactionPayload) UOW(org.jaffa.persistence.UOW) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with JaffaMessagingFrameworkException

use of org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException in project jaffa-framework by jaffa-projects.

the class TransactionMessagingEngine method processTransactions.

/**
 * Any internally queued {@link Transaction} needs a corresponding JMS message submitted.
 * Prior versions did this in the postAdd() of the Transaction domain object.
 * <p/>
 * This location is now one of a few places that should be using the JmsClientHelper.send() method.
 * The other place(s) are:
 * - In the drl files for sending messages to topic queues (resources/rules/jaffa/soa/SOAEventService.drl)
 */
private void processTransactions() throws FrameworkException, ApplicationExceptions {
    log.debug("Need to Write JMS for buffered Transactions. BufferSize=" + m_transactions.size());
    // send each transaction to the JmsClientHelper
    for (Transaction transaction : m_transactions.values()) {
        // if the transaction is not defined, skip it
        if (transaction == null) {
            continue;
        }
        // if the direction is not "IN", skip this transaction
        if ((transaction.getDirection() != null) && !Direction.IN.toString().equals(transaction.getDirection())) {
            log.debug("Transaction: " + transaction.getId() + " is not an IN transaction, it will be skipped.");
            continue;
        }
        // if the status is not "O", skip this transaction
        if ((transaction.getStatus() != null) && !Status.O.toString().equals(transaction.getStatus())) {
            log.debug("Transaction: " + transaction.getId() + " is not OPEN, it will be skipped.");
            continue;
        }
        // try to submit the transaction
        try {
            // see if a scheduled ID is defined for this transaction
            String scheduledId = null;
            TransactionField[] transactionFields = transaction.getTransactionFieldArray();
            if (transactionFields != null) {
                for (TransactionField field : transaction.getTransactionFieldArray()) {
                    if ("JaffaTransactionInvokerScheduledTaskId".equals(field.getFieldName())) {
                        scheduledId = field.getValue();
                        break;
                    }
                }
            }
            // send the transaction to the JMS queue to be consumed
            getSender().send(new TransactionMessage(transaction), transaction.getCreatedBy(), scheduledId, null);
        } catch (Exception e) {
            // If there is a failure to put the transaction in the JMS queue, set the transaction to error
            Boolean postImmediate = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(Transaction.RULE_POST_IMMEDIATE));
            if (postImmediate == null || !postImmediate.booleanValue()) {
                TransactionEngine.getInstance().updateTransactionStatusToError(transaction.getId(), e);
            }
            // Handle case where an application exception caused the issue
            ApplicationExceptions appExps = ExceptionHelper.extractApplicationExceptions(e);
            if (appExps != null) {
                log.error(MessageHelper.findMessage("exception.org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException.jmsTransactionError", new Object[] { transaction }), appExps);
                TransactionEngine.getInstance().updateTransactionStatusToError(transaction.getId(), appExps);
                throw appExps;
            }
            // If the originating exception is not an application exception log it
            log.error(MessageHelper.findMessage("exception.org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException.jmsTransactionError", new Object[] { transaction }), e);
            // Create a framework exception and rethrow
            FrameworkException frameworkException = new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.JMS_TRANSACTION_ERROR, new Object[] { transaction }, e);
            if (postImmediate == null || !postImmediate.booleanValue()) {
                TransactionEngine.getInstance().updateTransactionStatusToError(transaction.getId(), frameworkException);
            }
            // Throw the framework exception and exit
            throw frameworkException;
        }
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Transaction(org.jaffa.transaction.domain.Transaction) FrameworkException(org.jaffa.exceptions.FrameworkException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException) TransactionField(org.jaffa.transaction.domain.TransactionField) FrameworkException(org.jaffa.exceptions.FrameworkException) JaffaMessagingFrameworkException(org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException)

Aggregations

FrameworkException (org.jaffa.exceptions.FrameworkException)3 JaffaMessagingFrameworkException (org.jaffa.modules.messaging.services.JaffaMessagingFrameworkException)3 LinkedHashMap (java.util.LinkedHashMap)2 TransactionField (org.jaffa.transaction.domain.TransactionField)2 Method (java.lang.reflect.Method)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 TextMessage (javax.jms.TextMessage)1 DateTime (org.jaffa.datatypes.DateTime)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 HeaderElementDto (org.jaffa.modules.messaging.components.messageviewer.dto.HeaderElementDto)1 MessageViewerOutDto (org.jaffa.modules.messaging.components.messageviewer.dto.MessageViewerOutDto)1 DisplayParam (org.jaffa.modules.messaging.services.configdomain.DisplayParam)1 QueueInfo (org.jaffa.modules.messaging.services.configdomain.QueueInfo)1 UOW (org.jaffa.persistence.UOW)1 IllegalPersistentStateRuntimeException (org.jaffa.persistence.exceptions.IllegalPersistentStateRuntimeException)1