Search in sources :

Example 56 with TransactionAttribute

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

the class SFSB method setupConfig.

@TransactionAttribute(TransactionAttributeType.NEVER)
public void setupConfig() {
    // static {
    try {
        // prepare the configuration
        Configuration configuration = new Configuration().setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.getProperties().put(AvailableSettings.JTA_PLATFORM, JBossAppServerJtaPlatform.class);
        configuration.getProperties().put(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        // fetch the properties
        Properties properties = new Properties();
        configuration = configuration.configure("hibernate.cfg.xml");
        properties.putAll(configuration.getProperties());
        ConfigurationHelper.resolvePlaceHolders(properties);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        ex.printStackTrace();
        throw new ExceptionInInitializerError(ex);
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) Properties(java.util.Properties) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 57 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project javaee7-samples by javaee-samples.

the class JmsClient method process.

// <1> we need to send message in the middle of the method, therefore we cannot be transactional
@TransactionAttribute(NOT_SUPPORTED)
public String process(String request) {
    TextMessage requestMessage = jms.createTextMessage(request);
    TemporaryQueue responseQueue = jms.createTemporaryQueue();
    jms.createProducer().setJMSReplyTo(// <2> set the temporary queue as replyToDestination
    responseQueue).send(requestQueue, // <3> immediately send the request message
    requestMessage);
    try (JMSConsumer consumer = jms.createConsumer(responseQueue)) {
        // <4> listen on the temporary queue
        // <5> wait for a +TextMessage+ to arrive
        String response = consumer.receiveBody(String.class, 20000);
        if (response == null) {
            // <6> +receiveBody+ returns +null+ in case of timeout
            throw new IllegalStateException("Message processing timed out");
        }
        return response;
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 58 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project muikku by otavanopisto.

the class ChatRoomSyncScheduler method updateChatRooms.

@Schedule(second = "*", minute = "*/15", hour = "*", persistent = false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateChatRooms() {
    String enabledWorkspacesCsv = pluginSettingsController.getPluginSetting("chat", "enabledWorkspaces");
    if (enabledWorkspacesCsv == null) {
        return;
    }
    List<String> enabledWorkspaces = Arrays.asList(enabledWorkspacesCsv.split(","));
    String openfireToken = pluginSettingsController.getPluginSetting("chat", "openfireToken");
    if (openfireToken == null) {
        logger.log(Level.INFO, "No openfire token set, skipping room sync");
        return;
    }
    String openfireUrl = pluginSettingsController.getPluginSetting("chat", "openfireUrl");
    if (openfireUrl == null) {
        logger.log(Level.INFO, "No openfire url set, skipping room sync");
        return;
    }
    String openfirePort = pluginSettingsController.getPluginSetting("chat", "openfirePort");
    if (openfirePort == null) {
        logger.log(Level.INFO, "No openfire port set, skipping room sync");
        return;
    }
    if (!StringUtils.isNumeric(openfirePort)) {
        logger.log(Level.WARNING, "Invalid openfire port, skipping room sync");
        return;
    }
    AuthenticationToken token = new AuthenticationToken(openfireToken);
    RestApiClient client = new RestApiClient(openfireUrl, Integer.parseInt(openfirePort, 10), token);
    for (String enabledWorkspace : enabledWorkspaces) {
        try {
            // Checking before creating is subject to a race condition, but in the worst case
            // the creation just fails, resulting in a log entry
            MUCRoomEntity chatRoomEntity = client.getChatRoom(enabledWorkspace);
            if (chatRoomEntity == null) {
                logger.log(Level.INFO, "Syncing chat workspace " + enabledWorkspace);
                SchoolDataIdentifier identifier = SchoolDataIdentifier.fromId(enabledWorkspace);
                if (identifier == null) {
                    logger.log(Level.WARNING, "Invalid workspace identifier " + enabledWorkspace + ", skipping...");
                    continue;
                }
                WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByUrlName(enabledWorkspace);
                if (workspaceEntity == null) {
                    logger.log(Level.WARNING, "No workspace entity found for identifier " + enabledWorkspace + ", skipping...");
                    continue;
                }
                Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
                chatRoomEntity = new MUCRoomEntity(enabledWorkspace, workspace.getName(), workspace.getDescription());
                client.createChatRoom(chatRoomEntity);
            }
        } catch (Exception e) {
            logger.log(Level.INFO, "Exception when syncing chat workspace " + enabledWorkspace, e);
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) AuthenticationToken(fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) MUCRoomEntity(fi.otavanopisto.muikku.openfire.rest.client.entity.MUCRoomEntity) RestApiClient(fi.otavanopisto.muikku.openfire.rest.client.RestApiClient) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) TransactionAttribute(javax.ejb.TransactionAttribute) Schedule(javax.ejb.Schedule)

Example 59 with TransactionAttribute

use of javax.ejb.TransactionAttribute in project muikku by otavanopisto.

the class ChatUserSyncScheduler method updateChatUsers.

@Schedule(second = "0", minute = "*/15", hour = "*", persistent = false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void updateChatUsers() {
    String enabledUsersCsv = pluginSettingsController.getPluginSetting("chat", "enabledUsers");
    if (enabledUsersCsv == null) {
        return;
    }
    List<String> enabledUsers = Arrays.asList(enabledUsersCsv.split(","));
    String openfireToken = pluginSettingsController.getPluginSetting("chat", "openfireToken");
    if (openfireToken == null) {
        logger.log(Level.INFO, "No openfire token set, skipping user sync");
        return;
    }
    String openfireUrl = pluginSettingsController.getPluginSetting("chat", "openfireUrl");
    if (openfireUrl == null) {
        logger.log(Level.INFO, "No openfire url set, skipping user sync");
        return;
    }
    String openfirePort = pluginSettingsController.getPluginSetting("chat", "openfirePort");
    if (openfirePort == null) {
        logger.log(Level.INFO, "No openfire port set, skipping user sync");
        return;
    }
    if (!StringUtils.isNumeric(openfirePort)) {
        logger.log(Level.WARNING, "Invalid openfire port, skipping user sync");
        return;
    }
    AuthenticationToken token = new AuthenticationToken(openfireToken);
    RestApiClient client = new RestApiClient(openfireUrl, Integer.parseInt(openfirePort, 10), token);
    SecureRandom random = new SecureRandom();
    for (String enabledUser : enabledUsers) {
        try {
            // Checking before creating is subject to a race condition, but in the worst case
            // the creation just fails, resulting in a log entry
            UserEntity userEntity = client.getUser(enabledUser);
            if (userEntity == null) {
                logger.log(Level.INFO, "Syncing chat user " + enabledUser);
                SchoolDataIdentifier identifier = SchoolDataIdentifier.fromId(enabledUser);
                if (identifier == null) {
                    logger.log(Level.WARNING, "Invalid user identifier " + enabledUser + ", skipping...");
                    continue;
                }
                User user = userController.findUserByIdentifier(identifier);
                if (user == null) {
                    logger.log(Level.WARNING, "No user found for identifier " + enabledUser + ", skipping...");
                    continue;
                }
                // Can't leave the password empty, so next best thing is random passwords
                // The passwords are not actually used
                byte[] passwordBytes = new byte[20];
                random.nextBytes(passwordBytes);
                String password = Base64.encodeBase64String(passwordBytes);
                userEntity = new UserEntity(enabledUser, user.getDisplayName(), "", password);
                client.createUser(userEntity);
            }
        } catch (Exception e) {
            logger.log(Level.INFO, "Exception when syncing user " + enabledUser, e);
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) AuthenticationToken(fi.otavanopisto.muikku.openfire.rest.client.entity.AuthenticationToken) User(fi.otavanopisto.muikku.schooldata.entity.User) SecureRandom(java.security.SecureRandom) RestApiClient(fi.otavanopisto.muikku.openfire.rest.client.RestApiClient) UserEntity(fi.otavanopisto.muikku.openfire.rest.client.entity.UserEntity) TransactionAttribute(javax.ejb.TransactionAttribute) Schedule(javax.ejb.Schedule)

Example 60 with TransactionAttribute

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

the class ConfigMessageProducerBean method sendModuleMessage.

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public String sendModuleMessage(String text, Queue queue) throws ConfigMessageException {
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        final Session session = JMSUtils.connectToQueue(connection);
        TextMessage message = session.createTextMessage();
        message.setJMSReplyTo(responseQueue);
        message.setText(text);
        getProducer(session, queue).send(message);
        return message.getJMSMessageID();
    } catch (Exception e) {
        LOG.error("[ Error when sending data source message. ] {}", e.getMessage());
        throw new ConfigMessageException(e.getMessage());
    } finally {
        JMSUtils.disconnectQueue(connection);
    }
}
Also used : ConfigMessageException(eu.europa.ec.fisheries.uvms.config.exception.ConfigMessageException) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) ConfigMessageException(eu.europa.ec.fisheries.uvms.config.exception.ConfigMessageException) Session(javax.jms.Session) 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