2 * Copyright (C) 2016 Cho-Time GmbH
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<rhaeder@cho-time.de>
39 name = "rateCalcEmail",
40 description = "A message bean for email delivery",
42 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jratecalc-email-queue"),
43 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
46 public class RateCalcEmailDeliveryMessageBean implements MessageListener {
52 private LoggerBeanLocal loggerBeanLocal;
57 public RateCalcEmailDeliveryMessageBean () {
59 // Get initial context
60 Context context = new InitialContext();
63 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
64 } catch (final NamingException ex) {
66 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
71 public void onMessage (final Message message) {
73 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
75 // Is the message castable to ObjectMessage?
76 if (null == message) {
78 throw new NullPointerException("message is null"); //NOI18N
79 } else if (!(message instanceof ObjectMessage)) {
81 throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
85 ObjectMessage objectMessage = (ObjectMessage) message;
92 object = objectMessage.getObject();
93 } catch (final JMSException ex) {
95 this.loggerBeanLocal.logException(ex);
97 // ... and don't continue
102 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
104 // Does this object implement WrapableCheckout ?
105 if (null == object) {
106 // object cannot be null
107 throw new NullPointerException("object is null"); //NOI18N
108 } else if (!(object instanceof WrapableEmailDelivery)) {
109 // Not proper interface used
110 throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
113 // Cast the object to the wrapper interface
114 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
117 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N