]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java
Continued with email delivery:
[jjobs-ejb.git] / src / java / org / mxchange / jmailee / model / delivery / JobsEmailDeliveryMessageBean.java
1 /*
2  * Copyright (C) 2016 quix0r
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jmailee.model.delivery;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Date;
22 import javax.annotation.Resource;
23 import javax.ejb.ActivationConfigProperty;
24 import javax.ejb.MessageDriven;
25 import javax.jms.JMSException;
26 import javax.jms.Message;
27 import javax.jms.MessageListener;
28 import javax.jms.ObjectMessage;
29 import javax.mail.MessagingException;
30 import javax.mail.Session;
31 import javax.mail.Transport;
32 import javax.mail.internet.InternetAddress;
33 import javax.mail.internet.MimeMessage;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
38 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
39
40 /**
41  * A message queue for sending out emails
42  * <p>
43  * @author Roland Haeder<roland@mxchange.org>
44  */
45 @MessageDriven (activationConfig = {
46         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
47         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
48 })
49 public class JobsEmailDeliveryMessageBean implements MessageListener {
50
51         /**
52          * Email session
53          */
54         @Resource (name = "jmail/jjobs")
55         private Session jmailjjobs;
56
57         /**
58          * Logger bean
59          */
60         @Log
61         private LoggerBeanLocal loggerBeanLocal;
62
63         /**
64          * Default constructor
65          */
66         public JobsEmailDeliveryMessageBean () {
67                 try {
68                         // Get initial context
69                         Context context = new InitialContext();
70
71                         // Lookup logger
72                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
73                 } catch (final NamingException ex) {
74                         // Continue to throw
75                         throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
76                 }
77         }
78
79         @Override
80         public void onMessage (final Message message) {
81                 // Trace message
82                 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
83
84                 // Is the message castable to ObjectMessage?
85                 if (null == message) {
86                         // message is null
87                         throw new NullPointerException("message is null"); //NOI18N
88                 } else if (!(message instanceof ObjectMessage)) {
89                         // Not castable
90                         throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
91                 }
92
93                 // Securely cast it
94                 ObjectMessage objectMessage = (ObjectMessage) message;
95
96                 // Init instance
97                 Serializable object;
98
99                 try {
100                         // Get object from it
101                         object = objectMessage.getObject();
102                 } catch (final JMSException ex) {
103                         // Log exception ...
104                         this.loggerBeanLocal.logException(ex);
105
106                         // ... and don't continue
107                         return;
108                 }
109
110                 // Debug message
111                 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
112
113                 // Does this object implement WrapableCheckout ?
114                 if (null == object) {
115                         // object cannot be null
116                         throw new NullPointerException("object is null"); //NOI18N
117                 } else if (!(object instanceof WrapableEmailDelivery)) {
118                         // Not proper interface used
119                         throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
120                 }
121
122                 // Cast the object to the wrapper interface
123                 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
124
125                 // Trace message
126                 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
127         }
128
129         /**
130          * Sends an email to given email address with subject line.
131          * <p>
132          * @param emailAddress Email address for recipient
133          * @param subjectLine  Subject line
134          * @param body         Body part
135          * <p>
136          * @throws NamingException    If the resource cannot be found
137          * @throws MessagingException If something happened on message delivery
138          */
139         private void sendMail (final String emailAddress, final String subjectLine, final String body) throws NamingException, MessagingException {
140                 // Get MIME message instance
141                 MimeMessage message = new MimeMessage(this.jmailjjobs);
142
143                 // Set subject, recipients and body
144                 message.setSubject(subjectLine);
145                 message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailAddress, true));
146                 message.setSentDate(new Date());
147                 message.setText(body);
148
149                 // Directly send email
150                 Transport.send(message);
151         }
152
153 }