--- /dev/null
+/*
+ * Copyright (C) 2016 Cho-Time GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jmailee.model.delivery;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import javax.ejb.ActivationConfigProperty;
+import javax.ejb.MessageDriven;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcoreeelogger.beans.local.logger.Log;
+import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+
+/**
+ * A message queue for sending out emails
+ * <p>
+ * @author Roland Haeder<rhaeder@cho-time.de>
+ */
+@MessageDriven (
+ name = "rateCalcEmail",
+ description = "A message bean for email delivery",
+ activationConfig = {
+ @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jratecalc-email-queue"),
+ @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
+ }
+)
+public class RateCalcEmailDeliveryMessageBean implements MessageListener {
+
+ /**
+ * Logger bean
+ */
+ @Log
+ private LoggerBeanLocal loggerBeanLocal;
+
+ /**
+ * Default constructor
+ */
+ public RateCalcEmailDeliveryMessageBean () {
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Lookup logger
+ this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+ } catch (final NamingException ex) {
+ // Continue to throw
+ throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
+ }
+ }
+
+ @Override
+ public void onMessage (final Message message) {
+ // Trace message
+ this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
+
+ // Is the message castable to ObjectMessage?
+ if (null == message) {
+ // message is null
+ throw new NullPointerException("message is null"); //NOI18N
+ } else if (!(message instanceof ObjectMessage)) {
+ // Not castable
+ throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
+ }
+
+ // Securely cast it
+ ObjectMessage objectMessage = (ObjectMessage) message;
+
+ // Init instance
+ Serializable object;
+
+ try {
+ // Get object from it
+ object = objectMessage.getObject();
+ } catch (final JMSException ex) {
+ // Log exception ...
+ this.loggerBeanLocal.logException(ex);
+
+ // ... and don't continue
+ return;
+ }
+
+ // Debug message
+ this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
+
+ // Does this object implement WrapableCheckout ?
+ if (null == object) {
+ // object cannot be null
+ throw new NullPointerException("object is null"); //NOI18N
+ } else if (!(object instanceof WrapableEmailDelivery)) {
+ // Not proper interface used
+ throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
+ }
+
+ // Cast the object to the wrapper interface
+ WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
+
+ // Trace message
+ this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
+ }
+
+}