From: Roland Häder Date: Thu, 25 Aug 2016 10:34:53 +0000 (+0200) Subject: Please cherry-pick: (be careful with project-specific names!) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=73025a275b535b47e09dc8ddf2387fdb5dec3793;p=jjobs-ejb.git Please cherry-pick: (be careful with project-specific names!) - UserActivitySessionBean should better extend BaseDatabaseBean as no mail is being sent from this type of EJB - added activity JMS queue - implemented it in message-driven bean - this whole thing may be shortened to only the EJB but then it is synchronous and people then will experience a slow down on heavy load Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java new file mode 100644 index 0000000..5558717 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2016 Cho-Time GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jusercore.model.user.activity; + +import java.io.Serializable; +import java.text.MessageFormat; +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; +import org.mxchange.jjobs.database.BaseJobsDatabaseBean; + +/** + * A message-driven bean for adding user activity log + *

+ * @author Roland Haeder + */ +@MessageDriven ( + name = "userActivityLog", + description = "A message-driven bean for adding user activity log", + activationConfig = { + @ActivationConfigProperty ( + propertyName = "destinationLookup", + propertyValue = "jms/jlanding-user-activity-log" + ), + @ActivationConfigProperty ( + propertyName = "destinationType", + propertyValue = "javax.jms.Queue" + ) + } +) +public class JobsUserActivityLogMessageBean extends BaseJobsDatabaseBean implements MessageListener { + + /** + * Serial number + */ + private static final long serialVersionUID = 14_920_686_785_732_054L; + + /** + * Default constructor + */ + public JobsUserActivityLogMessageBean () { + } + + @Override + public void onMessage (final Message message) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: 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 (!(message instanceof ObjectMessage)) { + // Not implementing right interface + throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N + } + + // Securely cast it + ObjectMessage objectMessage = (ObjectMessage) message; + + // Init variable + Serializable serializable; + + try { + // Get object from message + serializable = objectMessage.getObject(); + } catch (final JMSException ex) { + // Log it and don't continue any further + this.getLoggerBeanLocal().logException(ex); + return; + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N + + // Okay, is it the right interface? + if (null == serializable) { + // Throw NPE + throw new NullPointerException("serializable is null"); //NOI18N + } else if (!(serializable instanceof LogableUserActivity)) { + // Not correct object send + throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement LogableUserActivity", serializable)); //NOI18N + } + + // Securely cast it + LogableUserActivity userActivity = (LogableUserActivity) serializable; + + // Should be valid + if (null == userActivity) { + // Throw NPE + throw new NullPointerException("userActivity is null"); //NOI18N + } else if (userActivity.getActivityId() instanceof Long) { + // Id number should not be set + throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N + } else if (userActivity.getActivityUser() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser is null"); //NOI18N + } else if (userActivity.getActivityType() == null) { + // Throw again ... + throw new NullPointerException("userActivity.activityType is null"); //NOI18N + } else if (userActivity.getActivityType().isEmpty()) { + // Empty type + throw new NullPointerException("userActivity.activityType is empty"); //NOI18N + } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) { + // Set but empty message + throw new NullPointerException("userActivity.activityMessage is empty"); //NOI18N + } else if (userActivity.getActivityTimestamp() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N + } + + // All fine, persist it + this.getEntityManager().persist(userActivity); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + +} diff --git a/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivitySessionBean.java b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivitySessionBean.java index 1ef7509..e3cd0fe 100644 --- a/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivitySessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivitySessionBean.java @@ -18,28 +18,129 @@ package org.mxchange.jusercore.model.user.activity; import java.text.MessageFormat; import java.util.List; +import javax.ejb.EJBException; import javax.ejb.Stateless; +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; import javax.persistence.Query; -import org.mxchange.jjobs.database.BaseJobsDatabaseBean; +import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jusercore.model.user.User; /** - * An EJB for user activity log + * An EJB for user activity log. This class extends BaseDatabaseBean and not + * project-specific "base class". The simple reason is that this class requires + * no email queue as no emails are ever being sent from this class. *

* @author Roland Haeder */ @Stateless (name = "userActivity", description = "A bean handling the user data") -public class JobsUserActivitySessionBean extends BaseJobsDatabaseBean implements UserActivityLogSessionBeanRemote { +public class JobsUserActivitySessionBean extends BaseDatabaseBean implements UserActivityLogSessionBeanRemote { /** * Serial number */ private static final long serialVersionUID = 219_568_677_671_054L; + /** + * Connection + */ + private Connection activityConnection; + + /** + * Message producer + */ + private MessageProducer activityMessageProducer; + + /** + * Mailer message queue + */ + private Queue activityQueue; + + /** + * Session instance + */ + private Session activitySession; + /** * Default constructor */ public JobsUserActivitySessionBean () { + // 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.activityQueue = (Queue) context.lookup("jms/jlanding-user-activity-log"); //NOI18N + + // Create connection + this.activityConnection = connectionFactory.createConnection(); + + // Init session instance + this.activitySession = this.activityConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // And message producer + this.activityMessageProducer = this.activitySession.createProducer(this.activityQueue); + } catch (final NamingException | JMSException e) { + // Continued to throw + throw new FacesException(e); + } + } + + @Override + public void addUserActivityLog (final LogableUserActivity userActivity) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N + + // Should be valid + if (null == userActivity) { + // Throw NPE + throw new NullPointerException("userActivity is null"); //NOI18N + } else if (userActivity.getActivityId() instanceof Long) { + // Id number should not be set + throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N + } else if (userActivity.getActivityUser() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser is null"); //NOI18N + } else if (userActivity.getActivityType() == null) { + // Throw again ... + throw new NullPointerException("userActivity.activityType is null"); //NOI18N + } else if (userActivity.getActivityType().isEmpty()) { + // Empty type + throw new NullPointerException("userActivity.activityType is empty"); //NOI18N + } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) { + // Set but empty message + throw new NullPointerException("userActivity.activityMessage is empty"); //NOI18N + } else if (userActivity.getActivityTimestamp() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N + } + + try { + // Send out email change + ObjectMessage message = this.activitySession.createObjectMessage(); + message.setObject(userActivity); + + // Send message + this.sendMessage(message, this.activityMessageProducer); + } catch (final JMSException ex) { + // Throw again + throw new EJBException(ex); + } } @Override diff --git a/src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java b/src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java deleted file mode 100644 index c4a865b..0000000 --- a/src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2016 Cho-Time GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.jusercore.model.user.activity; - -import de.chotime.landingpage.database.BaseLandingDatabaseBean; -import java.io.Serializable; -import java.text.MessageFormat; -import javax.ejb.ActivationConfigProperty; -import javax.ejb.MessageDriven; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageListener; -import javax.jms.ObjectMessage; -import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; - -/** - * A message-driven bean for adding user activity log - *

- * @author Roland Haeder - */ -@MessageDriven ( - name = "userActivityLog", - description = "A message-driven bean for adding user activity log", - activationConfig = { - @ActivationConfigProperty ( - propertyName = "destinationLookup", - propertyValue = "jms/jlanding-user-activity-log" - ), - @ActivationConfigProperty ( - propertyName = "destinationType", - propertyValue = "javax.jms.Queue" - ) - } -) -public class LandingUserActivityLogMessageBean extends BaseLandingDatabaseBean implements MessageListener { - - /** - * Serial number - */ - private static final long serialVersionUID = 14_920_686_785_732_054L; - - /** - * Default constructor - */ - public LandingUserActivityLogMessageBean () { - } - - @Override - public void onMessage (final Message message) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: 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 (!(message instanceof ObjectMessage)) { - // Not implementing right interface - throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N - } - - // Securely cast it - ObjectMessage objectMessage = (ObjectMessage) message; - - // Init variable - Serializable serializable; - - try { - // Get object from message - serializable = objectMessage.getObject(); - } catch (final JMSException ex) { - // Log it and don't continue any further - this.getLoggerBeanLocal().logException(ex); - return; - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N - - // Okay, is it the right interface? - if (null == serializable) { - // Throw NPE - throw new NullPointerException("serializable is null"); //NOI18N - } else if (!(serializable instanceof WrapableEmailDelivery)) { - // Not correct object send - throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N - } - } - -}