modified to be more robust

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@144929 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-03-15 09:04:26 +00:00
parent 992b8edd2f
commit 32376bcdb8
3 changed files with 82 additions and 67 deletions

View File

@ -1,11 +0,0 @@
package org.gcube.portal;
public class EmailParserThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
}
}

View File

@ -6,7 +6,6 @@ import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;
@ -19,14 +18,12 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
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.PeriodicTask;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
@ -49,9 +46,11 @@ import com.liferay.portal.service.VirtualHostLocalServiceUtil;
public class PortalSchedulerService extends HttpServlet {
private static final Logger _log = LoggerFactory.getLogger(PortalSchedulerService.class);
private static final String POP3_POLLING_MINUTES_INTERVAL = "pop3pollinginminutes";
private static final String POP3_SERVER_NAME = "Pop3MailServer";
private static DatabookStore store;
public void init() {
@ -71,14 +70,86 @@ public class PortalSchedulerService extends HttpServlet {
}
String toReturn = "<DIV>Check Notification Email Starting ... </DIV>";
Thread likesThread = new Thread(new PeriodicTask(store, popAccount, request));
likesThread.start();
int pollingInterval = getPollingInterval();
ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, popAccount, request), 0, pollingInterval, TimeUnit.MINUTES);
String portalName = "unknown";
try {
popAccount.getPortalName() ;
}
catch (NullPointerException e){
_log.warn("Could not read popAccount data portal name", e);
}
_log.info("EmailParser stared for " + portalName + ", pollingInterval (in minutes)=" + pollingInterval);
boolean keepPolling = true;
while (keepPolling) {
try {
int newPolling = getPollingInterval();
if (newPolling <= 0) {
pop3Scheduler.shutdown();
_log.info("EmailParser stopped for " + popAccount.getPortalName() + ", found value less than 1 in gcube-data.properties file under $CATALINA_HOME/conf");
keepPolling = false;
}
else if (newPolling != pollingInterval) {
pollingInterval = newPolling;
pop3Scheduler.shutdown();
_log.debug("Current thread EmailParser stopped, starting new one with different polling rate ... ->" + pollingInterval);
pop3Scheduler = Executors.newScheduledThreadPool(1);
pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, popAccount, request), 0, pollingInterval, TimeUnit.MINUTES);
_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 (Exception e) {
e.printStackTrace();
}
}
toReturn = "<DIV>Check Notification Email Started ... </DIV>";
// Thread likesThread = new Thread(new PeriodicTask(store, popAccount, request));
// likesThread.start();
response.setContentType("text/html");
response.getWriter().write(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 = 7;
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")+"/");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
/**
@ -162,5 +233,5 @@ public class PortalSchedulerService extends HttpServlet {
}
}

View File

@ -74,8 +74,7 @@ import com.sun.mail.util.MailSSLSocketFactory;
public class PeriodicTask implements Runnable {
private static final Log _log = LogFactoryUtil.getLog(PeriodicTask.class);
private static final String APP_ID_NEWSFEED = "org.gcube.portlets.user.newsfeed.server.NewsServiceImpl";
private static final String POP3_POLLING_MINUTES_INTERVAL = "pop3pollinginminutes";
private DatabookStore socialStore;
private EmailPopAccount popAccount;
private SocialNetworkingSite site;
@ -94,55 +93,11 @@ public class PeriodicTask implements Runnable {
@Override
public void run() {
boolean keepPolling = true;
while (keepPolling) {
try {
_log.debug("PeriodicTask starting for portal/site having name: " + popAccount.getPortalName());
check(popAccount.getPortalName(), popAccount.getPop3Server(), popAccount.getPop3user(), popAccount.getPop3password());
int pollingInterval = getPollingInterval();
if (pollingInterval > 0) {
_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);
}
else {
_log.info("EmailParser stopped for " + popAccount.getPortalName() + ", found value less than 1 in gcube-data.properties file under $CATALINA_HOME/conf");
keepPolling = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 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")+"/");
_log.debug("PeriodicTask starting for portal/site having name: " + popAccount.getPortalName());
check(popAccount.getPortalName(), popAccount.getPop3Server(), popAccount.getPop3user(), popAccount.getPop3password());
}
/**
* @return a fake session usuful for Notifications
*/