Search in sources :

Example 41 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project wildfly by wildfly.

the class TransactedQueueMessageSender method sendToQueueAndRollback.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToQueueAndRollback() throws JMSException {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        Message message = session.createTextMessage("Hello world 2!");
        logger.trace("Sending message");
        producer.send(message);
        // ROLLBACK
        ctx.setRollbackOnly();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 42 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project wildfly by wildfly.

the class TransactedTopicMessageSender method sendToTopicAndRollback.

@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void sendToTopicAndRollback() throws JMSException {
    Connection connection = null;
    Session session = null;
    try {
        logger.trace("Creating a Connection");
        connection = factory.createConnection();
        logger.trace("Creating a Session");
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Message message = session.createTextMessage("Hello world 2!");
        logger.trace("Sending message");
        producer.send(message);
        // ROLLBACK
        ctx.setRollbackOnly();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException) Session(javax.jms.Session) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 43 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project tomee by apache.

the class TransactionRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
            final Transactional annotation2 = description.getAnnotation(Transactional.class);
            if (annotation == null && annotation2 == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
            final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
            beanContext.getMethodContext(method).setTransactionType(transactionType);
            ThreadContext tc = ThreadContext.getThreadContext();
            final boolean tcCreated;
            if (tc == null) {
                tcCreated = true;
                tc = ThreadContext.enter(new ThreadContext(beanContext, null));
            } else {
                tcCreated = false;
            }
            final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
            try {
                base.evaluate();
            } finally {
                if (rollback) {
                    policy.setRollbackOnly();
                }
                EjbTransactionUtil.afterInvoke(policy, tc);
                if (tcCreated) {
                    ThreadContext.exit(tc);
                }
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionType(org.apache.openejb.core.transaction.TransactionType) TransactionAttribute(javax.ejb.TransactionAttribute) Statement(org.junit.runners.model.Statement) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) Transactional(javax.transaction.Transactional)

Example 44 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project ART-TIME by Artezio.

the class ReportEngine method generate.

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public byte[] generate(String templateName, OutputFormat outputFormat, Map<String, Object> params) {
    IReportEngine engine = null;
    IRunAndRenderTask task = null;
    try (ByteArrayOutputStream reportOutput = new ByteArrayOutputStream()) {
        engine = createReportEngine();
        task = createEngineTask(engine, templateName);
        setTaskParams(task, params);
        setTaskRenderOptions(task, outputFormat, reportOutput);
        task.run();
        return reportOutput.toByteArray();
    } catch (IOException | BirtException e) {
        throw new RuntimeException("Can't generate a report: ", e);
    } finally {
        if (task != null)
            task.close();
        if (engine != null)
            engine.destroy();
    }
}
Also used : BirtException(org.eclipse.birt.core.exception.BirtException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 45 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project UVMS-ExchangeModule-APP by UnionVMS.

the class ExchangeMessageConsumerBean method onMessage.

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void onMessage(Message message) {
    TextMessage textMessage = (TextMessage) message;
    ExchangeBaseRequest request = tryConsumeExchangeBaseRequest(textMessage);
    LOG.info("Message received in Exchange Message MDB");
    LOG.debug("Request body : ", request);
    if (request == null) {
        LOG.error("[ERROR] ExchangeBaseRequest is null!! Check the message sent...");
        try {
            // Handle PingResponse from plugin
            JAXBMarshaller.unmarshallTextMessage(textMessage, PingResponse.class);
            updatePingStateEvent.fire(new ExchangeMessageEvent(textMessage));
        } catch (ExchangeModelMarshallException e) {
            AcknowledgeResponse type = tryConsumeAcknowledgeResponse(textMessage);
            if (type == null) {
                LOG.error("[ Error when receiving message in exchange: {}]", message);
                errorEvent.fire(new ExchangeMessageEvent(textMessage, ExchangeModuleResponseMapper.createFaultMessage(FaultCode.EXCHANGE_MESSAGE, "Error when receiving message in exchange")));
            } else {
                updateStateEvent.fire(new ExchangeMessageEvent(textMessage));
            }
        }
    } else if (!checkUsernameShouldBeProvided(request)) {
        LOG.error("[ Error when receiving message in exchange, username must be set in the request: ]");
        errorEvent.fire(new ExchangeMessageEvent(textMessage, ExchangeModuleResponseMapper.createFaultMessage(FaultCode.EXCHANGE_MESSAGE, "Username in the request must be set")));
    } else {
        LOG.info("[INFO] Going to process following message type (aka Exchange Method) : " + request.getMethod());
        switch(request.getMethod()) {
            case LIST_SERVICES:
                pluginConfigEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_COMMAND:
                sendCommandToPluginEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SEND_REPORT_TO_PLUGIN:
                sendMessageToPluginEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_MOVEMENT_REPORT:
                processMovementEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case RECEIVE_SALES_REPORT:
                receiveSalesReportEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case RECEIVE_SALES_QUERY:
                receiveSalesQueryEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case RECEIVE_SALES_RESPONSE:
                receiveSalesResponseEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case RECEIVE_INVALID_SALES_MESSAGE:
                receiveInvalidSalesMessageEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SEND_SALES_RESPONSE:
                sendSalesResponseEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SEND_SALES_REPORT:
                sendSalesReportEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case UPDATE_PLUGIN_SETTING:
                updatePluginSettingEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case PING:
                pingEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case PROCESSED_MOVEMENT:
                processedMovementEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_MDR_SYNC_MESSAGE_REQUEST:
                mdrSyncRequestMessageEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_MDR_SYNC_MESSAGE_RESPONSE:
                mdrSyncResponseMessageEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_FLUX_FA_REPORT_MESSAGE:
                processFLUXFAReportMessageEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_FA_QUERY_MESSAGE:
                receivedFaQueryFromFlux.fire(new ExchangeMessageEvent(textMessage));
                break;
            case SET_FLUX_FA_RESPONSE_MESSAGE:
                processFLUXFAResponseMessageEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case UPDATE_LOG_STATUS:
                updateLogStatusEvent.fire(new ExchangeMessageEvent(textMessage));
                break;
            case LOG_REF_ID_BY_TYPE_EXISTS:
                logRefIdByTyeExists.fire(new ExchangeMessageEvent(textMessage));
                break;
            case LOG_ID_BY_TYPE_EXISTS:
                logIdByTyeExists.fire(new ExchangeMessageEvent(textMessage));
                break;
            default:
                LOG.error("[ Not implemented method consumed: {} ] ", request.getMethod());
                errorEvent.fire(new ExchangeMessageEvent(textMessage, ExchangeModuleResponseMapper.createFaultMessage(FaultCode.EXCHANGE_MESSAGE, "Method not implemented")));
        }
    }
}
Also used : ExchangeModelMarshallException(eu.europa.ec.fisheries.uvms.exchange.model.exception.ExchangeModelMarshallException) AcknowledgeResponse(eu.europa.ec.fisheries.schema.exchange.plugin.v1.AcknowledgeResponse) TextMessage(javax.jms.TextMessage) ExchangeBaseRequest(eu.europa.ec.fisheries.schema.exchange.module.v1.ExchangeBaseRequest) ExchangeMessageEvent(eu.europa.ec.fisheries.uvms.exchange.message.event.carrier.ExchangeMessageEvent) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

TransactionAttribute (javax.ejb.TransactionAttribute)61 JMSException (javax.jms.JMSException)8 IOException (java.io.IOException)7 Connection (javax.jms.Connection)6 Session (javax.jms.Session)6 TextMessage (javax.jms.TextMessage)6 Query (javax.persistence.Query)5 Configuration (org.hibernate.cfg.Configuration)5 Dataset (edu.harvard.iq.dataverse.Dataset)4 ConfigMessageException (eu.europa.ec.fisheries.uvms.config.exception.ConfigMessageException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 EJBException (javax.ejb.EJBException)4 Message (javax.jms.Message)4 MessageProducer (javax.jms.MessageProducer)4 DataFile (edu.harvard.iq.dataverse.DataFile)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 ExchangeMessageException (eu.europa.ec.fisheries.uvms.exchange.message.exception.ExchangeMessageException)3 Properties (java.util.Properties)3 EntityManager (javax.persistence.EntityManager)3