X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjcoreee%2Fdatabase%2FBaseDatabaseBean.java;h=8e3f9f31e760cc6138d34352169c8d9ff0853f42;hb=479979d00f68f66eae2920cab2dc7051fafa01fd;hp=c6dab1ab08b27beb3b471eeba8f5e73cb22728b6;hpb=fa70f70594c5b1bd2b9545d0cd67e0b46d4fbddf;p=jcore-utils.git diff --git a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java index c6dab1a..8e3f9f3 100644 --- a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java +++ b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java @@ -16,9 +16,17 @@ */ package org.mxchange.jcoreee.database; +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. @@ -38,6 +46,12 @@ public abstract class BaseDatabaseBean extends BaseBean { @PersistenceContext private EntityManager entityManager; + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + /** * This class' default protected constructor. Please call * super("jms/project-queue-factory", "jms/project-email-queue"); if you @@ -46,17 +60,39 @@ public abstract class BaseDatabaseBean extends BaseBean { protected BaseDatabaseBean () { // Call super constructor super(); + + // Init logger instance + this.initLoggerInstance(); } /** - * Constructor with queue factory JNDI and email-queue JNDI names + * Constructor with queue factory JNDI and queue JNDI names *
- * @param factoryJndi JNDI name for queue factory - * @param emailQueueJndi JNDI name for email queue + * @param factoryJndi JNDI name for queue factory + * @param queueJndi JNDI name for email queue */ - protected BaseDatabaseBean (final String factoryJndi, final String emailQueueJndi) { + protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) { // Call super constructor - super(factoryJndi, emailQueueJndi); + super(factoryJndi, queueJndi); + + // Init logger instance + this.initLoggerInstance(); + } + + /** + * Initializes logger instance + */ + private void initLoggerInstance () { + 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 + } } /** @@ -68,4 +104,40 @@ public abstract class BaseDatabaseBean extends BaseBean { return this.entityManager; } + /** + * Getter for loggerBeanLocal + *
+ * @return Logger instance + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } + + /** + * Sends given message to configured queue + *
+ * @param message Message to send + *
+ * @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 + } + }