]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jmailee/model/delivery/RateCalcEmailDeliveryMessageBean.java
Cleanup: (don't cherry-pick, repeat it all)
[pizzaservice-ejb.git] / src / java / org / mxchange / jmailee / model / delivery / RateCalcEmailDeliveryMessageBean.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jmailee.model.delivery;
18
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;
32
33 /**
34  * A message queue for sending out emails
35  * <p>
36  * @author Roland Haeder<rhaeder@cho-time.de>
37  */
38 @MessageDriven (
39                 name = "rateCalcEmail",
40                 description = "A message bean for email delivery",
41                 activationConfig = {
42                         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jratecalc-email-queue"),
43                         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
44                 }
45 )
46 public class RateCalcEmailDeliveryMessageBean implements MessageListener {
47
48         /**
49          * Logger bean
50          */
51         @Log
52         private LoggerBeanLocal loggerBeanLocal;
53
54         /**
55          * Default constructor
56          */
57         public RateCalcEmailDeliveryMessageBean () {
58                 try {
59                         // Get initial context
60                         Context context = new InitialContext();
61
62                         // Lookup logger
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) {
65                         // Continue to throw
66                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
67                 }
68         }
69
70         @Override
71         public void onMessage (final Message message) {
72                 // Trace message
73                 this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
74
75                 // Is the message castable to ObjectMessage?
76                 if (null == message) {
77                         // message is null
78                         throw new NullPointerException("message is null"); //NOI18N
79                 } else if (!(message instanceof ObjectMessage)) {
80                         // Not castable
81                         throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
82                 }
83
84                 // Securely cast it
85                 ObjectMessage objectMessage = (ObjectMessage) message;
86
87                 // Init instance
88                 Serializable object;
89
90                 try {
91                         // Get object from it
92                         object = objectMessage.getObject();
93                 } catch (final JMSException ex) {
94                         // Log exception ...
95                         this.loggerBeanLocal.logException(ex);
96
97                         // ... and don't continue
98                         return;
99                 }
100
101                 // Debug message
102                 this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
103
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
111                 }
112
113                 // Cast the object to the wrapper interface
114                 WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
115
116                 // Trace message
117                 this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
118         }
119
120 }