From 73025a275b535b47e09dc8ddf2387fdb5dec3793 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 25 Aug 2016 12:34:53 +0200 Subject: [PATCH] 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- ...va => JobsUserActivityLogMessageBean.java} | 44 ++++++- .../activity/JobsUserActivitySessionBean.java | 107 +++++++++++++++++- 2 files changed, 142 insertions(+), 9 deletions(-) rename src/java/org/mxchange/jusercore/model/user/activity/{LandingUserActivityLogMessageBean.java => JobsUserActivityLogMessageBean.java} (61%) diff --git a/src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java similarity index 61% rename from src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java rename to src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java index c4a865b..5558717 100644 --- a/src/java/org/mxchange/jusercore/model/user/activity/LandingUserActivityLogMessageBean.java +++ b/src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivityLogMessageBean.java @@ -16,7 +16,6 @@ */ 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; @@ -25,7 +24,7 @@ import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; -import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; +import org.mxchange.jjobs.database.BaseJobsDatabaseBean; /** * A message-driven bean for adding user activity log @@ -46,7 +45,7 @@ import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; ) } ) -public class LandingUserActivityLogMessageBean extends BaseLandingDatabaseBean implements MessageListener { +public class JobsUserActivityLogMessageBean extends BaseJobsDatabaseBean implements MessageListener { /** * Serial number @@ -56,7 +55,7 @@ public class LandingUserActivityLogMessageBean extends BaseLandingDatabaseBean i /** * Default constructor */ - public LandingUserActivityLogMessageBean () { + public JobsUserActivityLogMessageBean () { } @Override @@ -95,10 +94,43 @@ public class LandingUserActivityLogMessageBean extends BaseLandingDatabaseBean i if (null == serializable) { // Throw NPE throw new NullPointerException("serializable is null"); //NOI18N - } else if (!(serializable instanceof WrapableEmailDelivery)) { + } else if (!(serializable instanceof LogableUserActivity)) { // Not correct object send - throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N + 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 -- 2.39.5