Search in sources :

Example 6 with Asynchronous

use of javax.ejb.Asynchronous in project quickstart by wildfly.

the class LongRunningService method readData.

/**
 * The use of {@link Asynchronous} causes this EJB method to be executed asynchronously, by a different thread from a
 * dedicated, container managed thread pool.
 *
 * @param asyncContext the context for a suspended Servlet request that this EJB will complete later.
 */
@Asynchronous
public void readData(AsyncContext asyncContext) {
    try {
        // This is just to simulate a long running operation for demonstration purposes.
        Thread.sleep(5000);
        PrintWriter writer = asyncContext.getResponse().getWriter();
        writer.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
        writer.close();
        asyncContext.complete();
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PrintWriter(java.io.PrintWriter) Asynchronous(javax.ejb.Asynchronous)

Example 7 with Asynchronous

use of javax.ejb.Asynchronous in project dataverse by IQSS.

the class HarvestingClientServiceBean method deleteClient.

// Deleting a client, with all the associated content, can take a while -
// hence it's an async action:
// TOFIGUREOUT:
// for whatever reason I cannot call the DeleteHarvestingClientCommand from
// inside this method; something to do with it being asynchronous?
@Asynchronous
public void deleteClient(Long clientId) {
    String errorMessage = null;
    HarvestingClient victim = find(clientId);
    if (victim == null) {
        return;
    }
    try {
        // engineService.submit(new DeleteHarvestingClientCommand(dvRequestService.getDataverseRequest(), victim));
        HarvestingClient merged = em.merge(victim);
        // if this was a scheduled harvester, make sure the timer is deleted:
        dataverseTimerService.removeHarvestTimer(victim);
        // purge indexed objects:
        indexService.deleteHarvestedDocuments(victim);
        // proceed removing the client itself.
        for (DataFile harvestedFile : dataFileService.findHarvestedFilesByClient(merged)) {
            DataFile mergedFile = em.merge(harvestedFile);
            em.remove(mergedFile);
            harvestedFile = null;
        }
        em.remove(merged);
    } catch (Exception e) {
        errorMessage = "Failed to delete cleint. Unknown exception: " + e.getMessage();
    }
    if (errorMessage != null) {
        logger.warning(errorMessage);
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) NoResultException(javax.persistence.NoResultException) NonUniqueResultException(javax.persistence.NonUniqueResultException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) Asynchronous(javax.ejb.Asynchronous)

Example 8 with Asynchronous

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

the class CommunicatorMessageSentNotifier method onCommunicatorMessageSent.

@Asynchronous
@AccessTimeout(value = 30, unit = TimeUnit.MINUTES)
@Transactional(value = TxType.REQUIRES_NEW)
public void onCommunicatorMessageSent(@Observes(during = TransactionPhase.AFTER_COMPLETION) CommunicatorMessageSent event) {
    CommunicatorMessage communicatorMessage = communicatorController.findCommunicatorMessageById(event.getCommunicatorMessageId());
    UserEntity sender = userEntityController.findUserEntityById(communicatorMessage.getSender());
    UserEntity recipient = userEntityController.findUserEntityById(event.getRecipientUserEntityId());
    if ((communicatorMessage != null) && (sender != null) && (recipient != null)) {
        if (!Objects.equals(sender.getId(), recipient.getId())) {
            Map<String, Object> params = new HashMap<String, Object>();
            User senderUser = userController.findUserByUserEntityDefaults(sender);
            params.put("sender", String.format("%s %s", senderUser.getFirstName(), senderUser.getLastName()));
            params.put("subject", communicatorMessage.getCaption());
            params.put("content", communicatorMessage.getContent());
            params.put("url", String.format("%s/communicator", event.getBaseUrl()));
            // TODO Hash paramters cannot be utilized in redirect URLs
            // params.put("url", String.format("%s/communicator#inbox/%d", baseUrl, message.getCommunicatorMessageId().getId()));
            notifierController.sendNotification(communicatorNewInboxMessageNotification, sender, recipient, params);
        }
    } else {
        logger.log(Level.SEVERE, String.format("Communicator couldn't send notifications as some entity was not found"));
    }
}
Also used : CommunicatorMessage(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage) User(fi.otavanopisto.muikku.schooldata.entity.User) HashMap(java.util.HashMap) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Asynchronous(javax.ejb.Asynchronous) AccessTimeout(javax.ejb.AccessTimeout) Transactional(javax.transaction.Transactional)

Example 9 with Asynchronous

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

the class JndiMailService method onMailEvent.

@Asynchronous
@Lock(LockType.READ)
public void onMailEvent(@Observes JndiMailEvent event) {
    try {
        MailType mimeType = event.getMimeType();
        String from = event.getFrom();
        List<String> to = event.getTo();
        List<String> cc = event.getCc();
        List<String> bcc = event.getBcc();
        String subject = event.getSubject();
        String content = event.getContent();
        List<MailAttachment> attachments = event.getAttachments();
        Properties props = new Properties();
        InitialContext initialContext = new InitialContext(props);
        Session mailSession = (Session) initialContext.lookup("java:/mail/muikku");
        MimeMessage message = new MimeMessage(mailSession);
        if (!to.isEmpty()) {
            message.setRecipients(Message.RecipientType.TO, parseToAddressArray(to));
        }
        if (!cc.isEmpty()) {
            message.setRecipients(Message.RecipientType.CC, parseToAddressArray(cc));
        }
        if (!bcc.isEmpty()) {
            message.setRecipients(Message.RecipientType.BCC, parseToAddressArray(bcc));
        }
        if (from != null) {
            InternetAddress fromAddress = new InternetAddress(from);
            message.setFrom(fromAddress);
            message.setReplyTo(new InternetAddress[] { fromAddress });
        }
        if (subject != null) {
            message.setSubject(subject);
        }
        message.setSentDate(new Date());
        if (!attachments.isEmpty()) {
            Multipart multipart = new MimeMultipart();
            if (content != null) {
                MimeBodyPart contentBodyPart = new MimeBodyPart();
                contentBodyPart.setContent(content, mimeType.toString());
                multipart.addBodyPart(contentBodyPart);
            }
            for (MailAttachment attachment : attachments) {
                MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                attachmentBodyPart.setContent(attachment.getContent(), attachment.getMimeType());
                attachmentBodyPart.setFileName(attachment.getFileName());
                multipart.addBodyPart(attachmentBodyPart);
            }
            message.setContent(multipart);
        } else {
            if (content != null) {
                message.setContent(content, mimeType.toString());
            }
        }
        // Sending
        Transport.send(message);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error sending mail", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MailAttachment(fi.otavanopisto.muikku.mail.MailAttachment) MailType(fi.otavanopisto.muikku.mail.MailType) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) Date(java.util.Date) AddressException(javax.mail.internet.AddressException) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session) Asynchronous(javax.ejb.Asynchronous) Lock(javax.ejb.Lock)

Aggregations

Asynchronous (javax.ejb.Asynchronous)9 Date (java.util.Date)5 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 PrintWriter (java.io.PrintWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 InternetAddress (javax.mail.internet.InternetAddress)2 MimeMessage (javax.mail.internet.MimeMessage)2 DataFile (edu.harvard.iq.dataverse.DataFile)1 DvObject (edu.harvard.iq.dataverse.DvObject)1 ImportException (edu.harvard.iq.dataverse.api.imports.ImportException)1 RemoveLockCommand (edu.harvard.iq.dataverse.engine.command.impl.RemoveLockCommand)1 WorkflowStep (edu.harvard.iq.dataverse.workflow.step.WorkflowStep)1 WorkflowStepData (edu.harvard.iq.dataverse.workflow.step.WorkflowStepData)1 MailAttachment (fi.otavanopisto.muikku.mail.MailAttachment)1 MailType (fi.otavanopisto.muikku.mail.MailType)1 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1