]> git.mxchange.org Git - jcoreee.git/commitdiff
moved generic code to new constructor which takes JNDI names as parameter.
authorRoland Häder <roland@mxchange.org>
Tue, 6 Jun 2017 20:42:29 +0000 (22:42 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 6 Jun 2017 20:42:29 +0000 (22:42 +0200)
maybe later this needs to be expanded to allow properties for the wrapped classes.

Signed-off-by: Roland Häder <roland@mxchange.org>
src/org/mxchange/jcoreee/database/BaseDatabaseBean.java

index 37220a174b55ef643e1ee171b649f1bf0d64d4b3..d5e07624c9387c26bcd052f23c540e49e803e807 100644 (file)
@@ -18,9 +18,14 @@ package org.mxchange.jcoreee.database;
 
 import java.io.Serializable;
 import java.text.MessageFormat;
+import javax.faces.FacesException;
+import javax.jms.Connection;
 import javax.jms.JMSException;
 import javax.jms.MessageProducer;
 import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.Session;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
@@ -41,6 +46,11 @@ public abstract class BaseDatabaseBean implements Serializable {
         */
        private static final long serialVersionUID = 217_687_175_985_875L;
 
+       /**
+        * Connection
+        */
+       private Connection connection;
+
        /**
         * Entity manager
         */
@@ -54,7 +64,24 @@ public abstract class BaseDatabaseBean implements Serializable {
        private LoggerBeanLocal loggerBeanLocal;
 
        /**
-        * Protected constructor
+        * Message producer
+        */
+       private MessageProducer messageProducer;
+
+       /**
+        * Mailer message queue
+        */
+       private Queue queue;
+
+       /**
+        * Session instance
+        */
+       private Session session;
+
+       /**
+        * This class' default protected constructor. Please call
+        * super("jms/project-queue-factory", "jms/project-email-queue"); if you
+        * need to send emails.
         */
        protected BaseDatabaseBean () {
                try {
@@ -69,6 +96,40 @@ public abstract class BaseDatabaseBean implements Serializable {
                }
        }
 
+       /**
+        * Constructor with queue factory JNDI and email-queue JNDI names
+        * <p>
+        * @param factoryJndi    JNDI name for queue factory
+        * @param emailQueueJndi JNDI name for email queue
+        */
+       protected BaseDatabaseBean (final String factoryJndi, final String emailQueueJndi) {
+               // Call default constructor
+               super();
+
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Get factory from JMS resource
+                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
+
+                       // Lookup queue
+                       this.queue = (Queue) context.lookup(emailQueueJndi); //NOI18N
+
+                       // Create connection
+                       this.connection = connectionFactory.createConnection();
+
+                       // Init session instance
+                       this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+                       // And message producer
+                       this.messageProducer = this.session.createProducer(this.queue);
+               } catch (final NamingException | JMSException e) {
+                       // Continued to throw
+                       throw new FacesException(e);
+               }
+       }
+
        /**
         * Getter for connection instance
         * <p>
@@ -87,29 +148,46 @@ public abstract class BaseDatabaseBean implements Serializable {
                return this.loggerBeanLocal;
        }
 
+       /**
+        * Getter for configured message producer instance
+        * <p>
+        * @return Message producer
+        */
+       protected MessageProducer getMessageProducer () {
+               return this.messageProducer;
+       }
+
+       /**
+        * Getter for configured session instance
+        * <p>
+        * @return Session
+        */
+       protected Session getSession () {
+               return this.session;
+       }
+
        /**
         * Sends given message to configured queue
         * <p>
         * @param message Message to send
-        * @param messageProducer Message producer
         * <p>
         * @throws JMSException if something went wrong
         */
-       protected void sendMessage (final ObjectMessage message, final MessageProducer messageProducer) throws JMSException {
+       protected void sendMessage (final ObjectMessage message) throws JMSException {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1},messageProducer={2} - CALLED!", this.getClass().getSimpleName(), message, messageProducer)); //NOI18N
+               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 (null == messageProducer) {
+               } else if (null == this.getMessageProducer()) {
                        // Throw NPE again
-                       throw new NullPointerException("messageProvider is null"); //NOI18N
+                       throw new NullPointerException("this.messageProvider is null"); //NOI18N
                }
 
                // Send it
-               messageProducer.send(message);
+               this.getMessageProducer().send(message);
 
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N