import java.io.Serializable;
import java.text.MessageFormat;
+import java.util.Properties;
+import java.util.ResourceBundle;
+import javax.annotation.PostConstruct;
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;
+import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
+import org.mxchange.jjobsmailer.model.delivery.DeliverableJobsEmail;
+import org.mxchange.jjobsmailer.model.delivery.JobsMailer;
/**
* A message queue for sending out emails
@ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
@ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
-public class JobsEmailDeliveryMessageBean implements MessageListener {
+public class JobsEmailDeliveryMessageBean extends BaseJobsDatabaseBean implements MessageListener {
/**
- * Logger bean
+ * Serial number
*/
- @Log
- private LoggerBeanLocal loggerBeanLocal;
+ private static final long serialVersionUID = 75_638_176_619_024L;
+
+ /**
+ * Mailer instance
+ */
+ private final DeliverableJobsEmail mailer;
+
+ /**
+ * Configuration file
+ */
+ private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
/**
* Default constructor
*/
public JobsEmailDeliveryMessageBean () {
- 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
- }
+ // Init mailer instance
+ this.mailer = new JobsMailer();
}
@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?
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
+ // The parameter should be valid
if (null == message) {
- // message is null
+ // Throw NPE
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
+ // Not implementing right interface
+ throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
}
// Securely cast it
ObjectMessage objectMessage = (ObjectMessage) message;
- // Init instance
- Serializable object;
+ // Init variable
+ Serializable serializable;
try {
- // Get object from it
- object = objectMessage.getObject();
+ // Get object from message
+ serializable = objectMessage.getObject();
} catch (final JMSException ex) {
- // Log exception ...
- this.loggerBeanLocal.logException(ex);
-
- // ... and don't continue
+ // Log it and don't continue any further
+ this.getLoggerBeanLocal().logException(ex);
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
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
+ }
+
+ /**
+ * Post-construction
+ */
+ @PostConstruct
+ public void init () {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
+
+ // Try to load bundle
+ ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
+
+ // The bunble should be valid
+ if (null == bundle) {
+ // Throw NPE
+ throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
+ }
+
+ // Init Properties
+ Properties properties = new Properties();
+
+ // Is the bundle not empty?
+ if (!bundle.keySet().isEmpty()) {
+ // Loop through all
+ for (final String key : bundle.keySet()) {
+ // Log debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
+
+ // Get string from bundle and set it in properties
+ properties.put(key, bundle.getString(key));
+ }
}
- // Cast the object to the wrapper interface
- WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
+ // Handle it over to the mailer
+ this.mailer.init(properties);
// Trace message
- this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
+ this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
}
-
}