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);
}
}
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);
}
}
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"));
}
}
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);
}
}
Aggregations