Rev 1426 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
236 | jmachado | 1 | package pt.estgp.estgweb.services.email; |
2 | |||
623 | jmachado | 3 | import org.apache.log4j.Logger; |
1312 | jmachado | 4 | import pt.estgp.estgweb.Globals; |
623 | jmachado | 5 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
236 | jmachado | 6 | import pt.estgp.estgweb.services.jobs.JobHandler; |
7 | import pt.estgp.estgweb.utils.Email; |
||
8 | |||
9 | import java.io.Serializable; |
||
623 | jmachado | 10 | import java.util.ArrayList; |
628 | jmachado | 11 | import java.util.Iterator; |
236 | jmachado | 12 | import java.util.List; |
13 | |||
14 | /** |
||
15 | * @author Jorge Machado |
||
16 | * @date 11/Jul/2008 |
||
17 | * @see pt.estgp.estgweb.services.sms |
||
18 | */ |
||
19 | public class EMAILJob implements JobHandler, 1.5.0/docs/api/java/io/Serializable.html">Serializable |
||
20 | { |
||
21 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(EMAILJob.class); |
||
22 | |||
23 | private Email email; |
||
24 | |||
25 | |||
26 | |||
27 | |||
28 | public EMAILJob(Email email) |
||
29 | { |
||
30 | this.email = email; |
||
31 | } |
||
32 | |||
33 | |||
34 | |||
1312 | jmachado | 35 | |
1427 | jmachado | 36 | //TODO WHEN BACK TO BCC REMOVE this to 20 and remove Thread |
37 | private static final int NUMBER_EMAILS_TO = 1; |
||
38 | private static final int pauseTime = 1000; |
||
39 | |||
40 | |||
236 | jmachado | 41 | public void runJob() |
42 | { |
||
1427 | jmachado | 43 | final Email cloned = email.cloneEmailForJob(); |
44 | logger.info("Opening new Thread to send emails"); |
||
45 | new 1.5.0/docs/api/java/lang/Thread.html">Thread( |
||
46 | new 1.5.0/docs/api/java/lang/Runnable.html">Runnable() { |
||
47 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
48 | public void run() { |
||
49 | if(Globals.TEST_ENVIRONEMENT && !cloned.isSystemAssumingTestEmail()) |
||
50 | { |
||
51 | 1.5.0/docs/api/java/lang/String.html">String warn = "System in TEST Environement - will not send EMAIL"; |
||
52 | logger.warn(warn); |
||
53 | return; |
||
54 | } |
||
55 | try |
||
56 | { |
||
57 | List<String> recipientsOriginal = new ArrayList<String>(); |
||
58 | List<String> recipients = new ArrayList<String>(); |
||
59 | recipients.addAll(cloned.getRecipients()); |
||
60 | recipientsOriginal.addAll(cloned.getRecipients()); |
||
61 | while(recipients.size() > 0) |
||
62 | { |
||
63 | List<String> recipientsToSend = new ArrayList<String>(); |
||
628 | jmachado | 64 | |
1427 | jmachado | 65 | int contador = 0; |
66 | Iterator<String> iter = recipients.iterator(); |
||
67 | while(iter.hasNext() && contador < NUMBER_EMAILS_TO) |
||
68 | { |
||
69 | recipientsToSend.add(iter.next()); |
||
70 | iter.remove(); |
||
71 | contador++; |
||
72 | } |
||
73 | cloned.setRecipients(recipientsToSend); |
||
74 | new SendEmailService().sendEmail(cloned); |
||
75 | try { |
||
76 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(pauseTime); |
||
77 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
78 | logger.error(e,e); |
||
79 | } |
||
80 | } |
||
81 | try { |
||
82 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(1000); |
||
83 | } catch (1.5.0/docs/api/java/lang/InterruptedException.html">InterruptedException e) { |
||
84 | logger.error(e,e); |
||
85 | } |
||
86 | cloned.setRecipients(recipientsOriginal); |
||
87 | } |
||
88 | catch (ServiceException e) |
||
89 | { |
||
90 | logger.error("Sending EMAIL:" + e.toString(),e); |
||
91 | } |
||
92 | } |
||
628 | jmachado | 93 | } |
1427 | jmachado | 94 | ).start(); |
236 | jmachado | 95 | } |
1427 | jmachado | 96 | |
97 | |||
236 | jmachado | 98 | } |