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 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.
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.
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/>.
17 package org.mxchange.jmailee.model.delivery;
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;
35 * A message queue for sending out emails
37 * @author Roland Haeder<roland@mxchange.org>
41 description = "A message bean for email delivery",
43 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
44 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
46 public class JobsEmailDeliveryMessageBean extends BaseJobsDatabaseBean implements MessageListener {
51 private static final long serialVersionUID = 75_638_176_619_024L;
56 private final DeliverableJobsEmail mailer;
61 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
66 public JobsEmailDeliveryMessageBean () {
67 // Init mailer instance
68 this.mailer = new JobsMailer();
72 public void onMessage (final Message message) {
74 this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
75 // The parameter should be valid
76 if (null == message) {
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
85 ObjectMessage objectMessage = (ObjectMessage) message;
88 Serializable serializable;
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);
100 this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
103 this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
110 public void init () {
112 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
114 // Try to load bundle
115 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
118 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
120 // The bunble should be valid
121 if (null == bundle) {
123 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
127 Properties properties = new Properties();
129 // Is the bundle not empty?
130 if (!bundle.keySet().isEmpty()) {
132 for (final String key : bundle.keySet()) {
134 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
136 // Get string from bundle and set it in properties
137 properties.put(key, bundle.getString(key));
141 // Handle it over to the mailer
142 this.mailer.init(properties);
145 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N