From c6f28e42f246b1cbb764a45b727ddc4f1e47915e Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Fri, 20 Jan 2017 13:49:19 +0000 Subject: [PATCH] Created thread for job notifications. Enhanced the post notifications one with a constructor that uses Set instead of lists to avoid duplicates git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/notifications-common-library@141667 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../thread/JobStatusNotificationThread.java | 55 +++++++++++++++++++ .../thread/PostNotificationsThread.java | 15 +++-- 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/gcube/portal/notifications/thread/JobStatusNotificationThread.java diff --git a/src/main/java/org/gcube/portal/notifications/thread/JobStatusNotificationThread.java b/src/main/java/org/gcube/portal/notifications/thread/JobStatusNotificationThread.java new file mode 100644 index 0000000..b4a4708 --- /dev/null +++ b/src/main/java/org/gcube/portal/notifications/thread/JobStatusNotificationThread.java @@ -0,0 +1,55 @@ +package org.gcube.portal.notifications.thread; + +import java.util.List; + +import org.gcube.applicationsupportlayer.social.NotificationsManager; +import org.gcube.portal.databook.shared.RunningJob; +import org.gcube.portal.notifications.bean.GenericItemBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * A job status notification thread. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class JobStatusNotificationThread implements Runnable { + + private static final Logger logger = LoggerFactory.getLogger(JobStatusNotificationThread.class); + private RunningJob jobDescriptor; + private List recipients; + private NotificationsManager nm; + + /** + * @param jobDescriptor + * @param applicationQualifier + * @param recipients + * @param nm + */ + public JobStatusNotificationThread(RunningJob jobDescriptor, List recipients, + NotificationsManager nm) { + super(); + this.jobDescriptor = jobDescriptor; + this.recipients = recipients; + this.nm = nm; + } + + + @Override + public void run() { + + logger.debug("Starting job notification thread. Recipients of this notification are " + recipients); + + for (GenericItemBean recipient : recipients) { + try{ + String userIdToNotify = recipient.getName(); + nm.notifyJobStatus(userIdToNotify, jobDescriptor); + }catch(Exception e){ + logger.error("Failed to send notification", e); + } + } + logger.debug("Notification job thread ended"); + + } + +} diff --git a/src/main/java/org/gcube/portal/notifications/thread/PostNotificationsThread.java b/src/main/java/org/gcube/portal/notifications/thread/PostNotificationsThread.java index bf7f0bc..07f72a2 100644 --- a/src/main/java/org/gcube/portal/notifications/thread/PostNotificationsThread.java +++ b/src/main/java/org/gcube/portal/notifications/thread/PostNotificationsThread.java @@ -1,6 +1,6 @@ package org.gcube.portal.notifications.thread; -import java.util.List; +import java.util.Set; import org.gcube.applicationsupportlayer.social.NotificationsManager; import org.gcube.vomanagement.usermanagement.UserManager; @@ -19,19 +19,22 @@ public class PostNotificationsThread implements Runnable { private String postText; private String postId; private long groupId; - private List hashtags; + private Set hashtags; private NotificationsManager nm; private UserManager userManager; + private Set mentionedVREGroups; /** * + * @param userManager * @param postId * @param postText - * @param groupId the LR groupId + * @param groupId * @param nm * @param hashtags + * @param mentionedVREGroups */ - public PostNotificationsThread(UserManager userManager, String postId, String postText, String groupId, NotificationsManager nm, List hashtags) { + public PostNotificationsThread(UserManager userManager, String postId, String postText, String groupId, NotificationsManager nm, Set hashtags, Set mentionedVREGroups) { super(); this.postId = postId; this.postText = postText; @@ -39,14 +42,14 @@ public class PostNotificationsThread implements Runnable { this.hashtags = hashtags; this.nm = nm; this.userManager = userManager; + this.mentionedVREGroups = mentionedVREGroups; } @Override public void run() { - String[] hashtagsToPass = hashtags.toArray(new String[hashtags.size()]); try { for (GCubeUser user : userManager.listUsersByGroup(groupId)) { - boolean result = nm.notifyPost(user.getUsername(), postId, postText, hashtagsToPass); + boolean result = nm.notifyPost(user.getUsername(), postId, postText, mentionedVREGroups, hashtags); _log.trace("Sending Notification for post alert to: " + user.getUsername() + " result?"+ result); } } catch (Exception e) {