]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java
Continued a bit:
[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 java.util.Properties;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.ejb.ActivationConfigProperty;
25 import javax.ejb.MessageDriven;
26 import javax.jms.JMSException;
27 import javax.jms.Message;
28 import javax.jms.MessageListener;
29 import javax.jms.ObjectMessage;
30 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
31 import org.mxchange.jjobsmailer.model.delivery.DeliverableJobsEmail;
32 import org.mxchange.jjobsmailer.model.delivery.JobsMailer;
33
34 /**
35  * A message queue for sending out emails
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @MessageDriven (
40                 name = "jjobsEmail",
41                 description = "A message bean for email delivery",
42                 activationConfig = {
43                         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
44                         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
45                 })
46 public class JobsEmailDeliveryMessageBean extends BaseJobsDatabaseBean implements MessageListener {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 75_638_176_619_024L;
52
53         /**
54          * Mailer instance
55          */
56         private final DeliverableJobsEmail mailer;
57
58         /**
59          * Configuration file
60          */
61         private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
62
63         /**
64          * Default constructor
65          */
66         public JobsEmailDeliveryMessageBean () {
67                 // Init mailer instance
68                 this.mailer = new JobsMailer();
69         }
70
71         @Override
72         public void onMessage (final Message message) {
73                 // Trace message
74                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
75                 // The parameter should be valid
76                 if (null == message) {
77                         // Throw NPE
78                         throw new NullPointerException("message is null"); //NOI18N
79                 } else if (!(message instanceof ObjectMessage)) {
80                         // Not implementing right interface
81                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
82                 }
83
84                 // Securely cast it
85                 ObjectMessage objectMessage = (ObjectMessage) message;
86
87                 // Init variable
88                 Serializable serializable;
89
90                 try {
91                         // Get object from message
92                         serializable = objectMessage.getObject();
93                 } catch (final JMSException ex) {
94                         // Log it and don't continue any further
95                         this.getLoggerBeanLocal().logException(ex);
96                         return;
97                 }
98
99                 // Debug message
100                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
104         }
105
106         /**
107          * Post-construction
108          */
109         @PostConstruct
110         public void init () {
111                 // Trace message
112                 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
113
114                 // Try to load bundle
115                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
116
117                 // Debug message
118                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
119
120                 // The bunble should be valid
121                 if (null == bundle) {
122                         // Throw NPE
123                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
124                 }
125
126                 // Init Properties
127                 Properties properties = new Properties();
128
129                 // Is the bundle not empty?
130                 if (!bundle.keySet().isEmpty()) {
131                         // Loop through all
132                         for (final String key : bundle.keySet()) {
133                                 // Log debug message
134                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
135
136                                 // Get string from bundle and set it in properties
137                                 properties.put(key, bundle.getString(key));
138                         }
139                 }
140
141                 // Handle it over to the mailer
142                 this.mailer.init(properties);
143
144                 // Trace message
145                 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
146         }
147 }