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;
*/
private static final long serialVersionUID = 217_687_175_985_875L;
+ /**
+ * Connection
+ */
+ private Connection connection;
+
/**
* Entity manager
*/
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 {
}
}
+ /**
+ * 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>
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