2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (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 Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero 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 javax.ejb.ActivationConfigProperty;
22 import javax.ejb.MessageDriven;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.MessageListener;
26 import javax.jms.ObjectMessage;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
31 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
34 * A message queue for sending out emails
36 * @author Roland Haeder<roland@mxchange.org>
40 description = "A message bean for email delivery",
42 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
43 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
45 public class JobsEmailDeliveryMessageBean implements MessageListener {
51 private LoggerBeanLocal loggerBeanLocal;
56 public JobsEmailDeliveryMessageBean () {
58 // Get initial context
59 Context context = new InitialContext();
62 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
63 } catch (final NamingException ex) {
65 throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
70 public void onMessage (final Message message) {
72 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
74 // Is the message castable to ObjectMessage?
75 if (null == message) {
77 throw new NullPointerException("message is null"); //NOI18N
78 } else if (!(message instanceof ObjectMessage)) {
80 throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
84 ObjectMessage objectMessage = (ObjectMessage) message;
91 object = objectMessage.getObject();
92 } catch (final JMSException ex) {
94 this.loggerBeanLocal.logException(ex);
96 // ... and don't continue
101 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
103 // Does this object implement WrapableCheckout ?
104 if (null == object) {
105 // object cannot be null
106 throw new NullPointerException("object is null"); //NOI18N
107 } else if (!(object instanceof WrapableEmailDelivery)) {
108 // Not proper interface used
109 throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
112 // Cast the object to the wrapper interface
113 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
116 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N