From e9406572bf6a0e63cb35be64569cfc48bc9287ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 6 Jun 2017 22:42:29 +0200 Subject: [PATCH] moved generic code to new constructor which takes JNDI names as parameter. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit maybe later this needs to be expanded to allow properties for the wrapped classes. Signed-off-by: Roland Häder --- .../jcoreee/database/BaseDatabaseBean.java | 92 +++++++++++++++++-- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java index 37220a1..d5e0762 100644 --- a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java +++ b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java @@ -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 + *

+ * @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 *

@@ -87,29 +148,46 @@ public abstract class BaseDatabaseBean implements Serializable { return this.loggerBeanLocal; } + /** + * Getter for configured message producer instance + *

+ * @return Message producer + */ + protected MessageProducer getMessageProducer () { + return this.messageProducer; + } + + /** + * Getter for configured session instance + *

+ * @return Session + */ + protected Session getSession () { + return this.session; + } + /** * Sends given message to configured queue *

* @param message Message to send - * @param messageProducer Message producer *

* @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 -- 2.39.5