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