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 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 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>
38 @MessageDriven (activationConfig = {
39 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
40 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
42 public class JobsEmailDeliveryMessageBean implements MessageListener {
48 private LoggerBeanLocal loggerBeanLocal;
53 public JobsEmailDeliveryMessageBean () {
55 // Get initial context
56 Context context = new InitialContext();
59 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
60 } catch (final NamingException ex) {
62 throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
67 public void onMessage (final Message message) {
69 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
71 // Is the message castable to ObjectMessage?
72 if (null == message) {
74 throw new NullPointerException("message is null"); //NOI18N
75 } else if (!(message instanceof ObjectMessage)) {
77 throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
81 ObjectMessage objectMessage = (ObjectMessage) message;
88 object = objectMessage.getObject();
89 } catch (final JMSException ex) {
91 this.loggerBeanLocal.logException(ex);
93 // ... and don't continue
98 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
100 // Does this object implement WrapableCheckout ?
101 if (null == object) {
102 // object cannot be null
103 throw new NullPointerException("object is null"); //NOI18N
104 } else if (!(object instanceof WrapableEmailDelivery)) {
105 // Not proper interface used
106 throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
109 // Cast the object to the wrapper interface
110 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
113 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N