modified so that the polling interval is modifiable at runtime

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@131055 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-08-30 16:14:40 +00:00
parent 6cd25ea0a9
commit 72bb81a301
1 changed files with 56 additions and 8 deletions

View File

@ -3,9 +3,12 @@ package org.gcube.portal;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -16,12 +19,14 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.notifications.thread.LikeNotificationsThread;
import org.gcube.portal.socialmail.EmailPopAccount; import org.gcube.portal.socialmail.EmailPopAccount;
import org.gcube.portal.socialmail.PeriodicTask; import org.gcube.portal.socialmail.PeriodicTask;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
@ -47,14 +52,14 @@ public class PortalSchedulerService extends HttpServlet {
private static final String POP3_SERVER_NAME = "Pop3MailServer"; private static final String POP3_SERVER_NAME = "Pop3MailServer";
private static final int POP3_MINUTES_DELAY = 1; private static final String POP3_POLLING_MINUTES_INTERVAL = "pop3pollinginminutes";
private static DatabookStore store; private static DatabookStore store;
public void init() { public void init() {
store = new DBCassandraAstyanaxImpl(); store = new DBCassandraAstyanaxImpl();
} }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@ -67,17 +72,32 @@ public class PortalSchedulerService extends HttpServlet {
e.printStackTrace(); e.printStackTrace();
} }
ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1); //ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, popAccount, request), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES);
String toReturn = "<DIV>Check Notification Email Started ... </DIV>";
String toReturn = "<DIV>Check Notification Email Starting ... </DIV>";
response.setContentType("text/html"); response.setContentType("text/html");
response.getWriter().write(toReturn); response.getWriter().write(toReturn);
//pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, popAccount, request), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES);
while (true) {
Thread likesThread = new Thread(new PeriodicTask(store, popAccount, request));
likesThread.start();
try {
int pollingInterval = getPollingInterval();
_log.debug("EmailParser restarts in " + pollingInterval + " minutes, to change this polling delay edit gcube-data.properties file under $CATALINA_HOME/conf");
Thread.sleep(pollingInterval * 1000 * 60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
/** /**
* *
* @param request * @param request
@ -105,7 +125,7 @@ public class PortalSchedulerService extends HttpServlet {
} }
private EmailPopAccount getPopAccountData(Group site) throws GroupRetrievalFault { private EmailPopAccount getPopAccountData(Group site) throws GroupRetrievalFault {
_log.debug("Found site for vhost, name " + site.getName() + " reading custom field: " + CustomAttributeKeys.GATEWAY_SITE_NAME); _log.debug("Found site for vhost, name " + site.getName() + " reading custom field: " + CustomAttributeKeys.GATEWAY_SITE_NAME);
String gatewayName = (String) new LiferayGroupManager().readCustomAttr(site.getGroupId(), CustomAttributeKeys.GATEWAY_SITE_NAME.getKeyName()); String gatewayName = (String) new LiferayGroupManager().readCustomAttr(site.getGroupId(), CustomAttributeKeys.GATEWAY_SITE_NAME.getKeyName());
@ -158,7 +178,35 @@ public class PortalSchedulerService extends HttpServlet {
return toReturn; return toReturn;
} }
/**
* read the time interval in minutes from a property file and returns it
*/
public int getPollingInterval() {
//get the portles to look for from the property file
Properties props = new Properties();
int toReturn = 3;
try {
String propertyfile = getCatalinaHome() + File.separator + "conf" + File.separator + "gcube-data.properties";
File propsFile = new File(propertyfile);
FileInputStream fis = new FileInputStream(propsFile);
props.load(fis);
toReturn = Integer.parseInt(props.getProperty(POP3_POLLING_MINUTES_INTERVAL));
}
//catch exception in case properties file does not exist
catch(IOException e) {
_log.error("gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default interval in minutes = " + toReturn);
return toReturn;
}
_log.debug("Returning poling interval in minutes: " + toReturn );
return toReturn;
}
/**
*
* @return $CATALINA_HOME
*/
private static String getCatalinaHome() {
return (System.getenv("CATALINA_HOME").endsWith("/") ? System.getenv("CATALINA_HOME") : System.getenv("CATALINA_HOME")+"/");
}
} }