]> git.mxchange.org Git - jbonuscard-core.git/commitdiff
Initialization can be done here, much lesser code to maintain
authorRoland Häder <roland@mxchange.org>
Tue, 17 May 2016 16:00:22 +0000 (18:00 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 21 May 2016 11:35:55 +0000 (13:35 +0200)
src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java

index b9708922becd6567e16be29ab99f36c329fd6e5f..ff9cd3396e45c424688b03eef5b5b4ef4e3e2678 100644 (file)
@@ -22,11 +22,18 @@ import java.util.Locale;
 import java.util.Objects;
 import java.util.Properties;
 import javax.ejb.EJBException;
+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.mail.Address;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
@@ -50,12 +57,55 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
         */
        private static final long serialVersionUID = 12_895_410_275_811_963L;
 
+       /**
+        * Connection
+        */
+       private Connection connection;
+
+       /**
+        * Message producer
+        */
+       private MessageProducer messageProducer;
+
+       /**
+        * Mailer message queue
+        */
+       private Queue queue;
+
+       /**
+        * Session instance
+        */
+       private Session session;
+
        /**
         * Protected constructor
         */
        protected BaseAddressbookDatabaseBean () {
                // Call super constructor
                super();
+
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Get factory from JMS resource
+                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
+
+                       // Lookup queue
+                       this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //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);
+               }
        }
 
        /**
@@ -410,12 +460,10 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
         * @param templateName Template name
         * @param emailAddress Recipient's email address
         * @param user User instance
-        * @param session JMS Session
-        * @param messageProducer Message producer
         */
-       protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final Session session, final MessageProducer messageProducer) {
+       protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={3},user={4},session={5},messageProducer={6} - CALLED!", subjectLine, templateName, emailAddress, user, session, messageProducer));
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={3},user={4} - CALLED!", subjectLine, templateName, emailAddress, user));
 
                // All should be set
                if (null == subjectLine) {
@@ -451,11 +499,11 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 
                try {
                        // Send out email change
-                       ObjectMessage message = session.createObjectMessage();
+                       ObjectMessage message = this.session.createObjectMessage();
                        message.setObject(emailWrapper);
 
                        // Send message
-                       this.sendMessage(message, messageProducer);
+                       this.sendMessage(message, this.messageProducer);
                } catch (final JMSException ex) {
                        // Throw again
                        throw new EJBException(ex);