BrightSide Workbench Full Report + Source Code
MailHeavy.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.mail.sender;
20 
21 import java.util.concurrent.locks.Lock;
22 import java.util.concurrent.locks.ReentrantLock;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.queue.NotificationCategory;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.elephant.context.ElephantContext;
30 
35 @ElephantPlugin(label = "heavy")
36 public class MailHeavy extends MailPool implements Runnable {
37 
38  private NotificationCategory category;
39  private Object entity;
40  private String template, subject, message;
41 
42  @Override
43  protected void doSendTemplate(NotificationCategory category, Object entity, String template, String subject) throws EmailException {
44  this.category = category;
45  this.entity = entity;
46  this.template = template;
47  this.subject = subject;
48  doBuild();
49  }
50 
51  @Override
52  protected void doSend(NotificationCategory category, String subject, String message) {
53  this.category = category;
54  this.subject = subject;
55  this.message = message;
56  doBuild();
57  }
58 
59  private void doBuild() {
60  if(onBuild != null) {
61  Thread sender = new Thread(this, "HeavySenderTask");
62  sender.start();
63  }
64  }
65 
66  /* Runnable */
67 
68  @Override
69  public void run() {
70  boolean lockAcquired = HEAVY_LOCK.tryLock();
71  if(lockAcquired) {
72  try {
73  if(onStart != null) onStart.accept(MailHeavy.this);
74  onBuild.accept(MailHeavy.this);
75  if(!Strings.isBlank(template)) {
76  super.doSendTemplate(category, entity, template, subject);
77  } else {
78  super.doSend(category, subject, message);
79  }
80  } catch (Exception ex) {
81  Logger.getLogger(MailHeavy.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("onBuild"), ex);
82  } finally {
83  HEAVY_LOCK.unlock();
84  }
85  } else {
86  if(onCancel != null) onCancel.accept(MailHeavy.this);
87  }
88  }
89 
90  /* Lock for senders */
91 
92  private static final Lock HEAVY_LOCK = new ReentrantLock();
93 
94 }
95 
void doSend(NotificationCategory category, String subject, String message)
Definition: MailHeavy.java:52
void doSendTemplate(NotificationCategory category, Object entity, String template, String subject)
Definition: MailHeavy.java:43