/*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016, 2017 Roland Häder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*/
package org.mxchange.jcoreee.database;
-import java.io.Serializable;
+import java.text.MessageFormat;
+import javax.jms.JMSException;
+import javax.jms.ObjectMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
+import org.mxchange.jcoreee.bean.BaseBean;
import org.mxchange.jcoreeelogger.beans.local.logger.Log;
import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
/**
* A helper class for beans that access the database.
* <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
*/
-public abstract class BaseDatabaseBean implements Serializable {
+public abstract class BaseDatabaseBean extends BaseBean {
/**
* Serial number
private LoggerBeanLocal loggerBeanLocal;
/**
- * Protected constructor
+ * This class' default protected constructor. Please call
+ * super("jms/project-queue-factory", "jms/project-email-queue"); if you
+ * need to send emails.
*/
protected BaseDatabaseBean () {
+ // Call super constructor
+ super();
+
+ // Init logger instance
+ this.initLoggerInstance();
+ }
+
+ /**
+ * Constructor with queue factory JNDI and queue JNDI names
+ * <p>
+ * @param factoryJndi JNDI name for queue factory
+ * @param queueJndi JNDI name for email queue
+ */
+ protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
+ // Call super constructor
+ super(factoryJndi, queueJndi);
+
+ // Init logger instance
+ this.initLoggerInstance();
+ }
+
+ /**
+ * Initializes logger instance
+ */
+ private void initLoggerInstance () {
try {
// Get initial context
Context context = new InitialContext();
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("context.lookup() failed.", ex); //NOI18N
+ throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
}
}
protected LoggerBeanLocal getLoggerBeanLocal () {
return this.loggerBeanLocal;
}
+
+ /**
+ * Sends given message to configured queue
+ * <p>
+ * @param message Message to send
+ * <p>
+ * @throws JMSException if something went wrong
+ */
+ protected void sendMessage (final ObjectMessage message) throws JMSException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
+
+ // The parameter should be valid
+ if (null == message) {
+ // Throw NPE
+ throw new NullPointerException("message is null"); //NOI18N
+ } else if (this.getMessageProducer() == null) {
+ // Throw NPE again
+ throw new NullPointerException("this.messageProvider is null"); //NOI18N
+ }
+
+ // Send it
+ this.getMessageProducer().send(message);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
+ }
+
}