2 * Copyright (C) 2016 quix0r
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.
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.
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/>.
17 package org.mxchange.jmailee.model.delivery;
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;
41 * A message queue for sending out emails
43 * @author Roland Haeder<roland@mxchange.org>
45 @MessageDriven (activationConfig = {
46 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
47 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
49 public class JobsEmailDeliveryMessageBean implements MessageListener {
54 @Resource (name = "jmail/jjobs")
55 private Session jmailjjobs;
61 private LoggerBeanLocal loggerBeanLocal;
66 public JobsEmailDeliveryMessageBean () {
68 // Get initial context
69 Context context = new InitialContext();
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) {
75 throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
80 public void onMessage (final Message message) {
82 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
84 // Is the message castable to ObjectMessage?
85 if (null == message) {
87 throw new NullPointerException("message is null"); //NOI18N
88 } else if (!(message instanceof ObjectMessage)) {
90 throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
94 ObjectMessage objectMessage = (ObjectMessage) message;
100 // Get object from it
101 object = objectMessage.getObject();
102 } catch (final JMSException ex) {
104 this.loggerBeanLocal.logException(ex);
106 // ... and don't continue
111 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
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
122 // Cast the object to the wrapper interface
123 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
126 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
130 * Sends an email to given email address with subject line.
132 * @param emailAddress Email address for recipient
133 * @param subjectLine Subject line
134 * @param body Body part
136 * @throws NamingException If the resource cannot be found
137 * @throws MessagingException If something happened on message delivery
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);
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);
149 // Directly send email
150 Transport.send(message);