DEBUGGING THE DREADED “SEVERE: ERROR LISTENERSTART” AND “SEVERE: ERROR FILTERSTART” TOMCAT ERROR MESSAGES



|
Just a quick post that I hope might benefit others. If you have been developing web applications on tomcat for a while you have likely come the two error messages mentioned in the title.

SEVERE: Error listenerStart

Occurs when an exception is thrown in the contextInitialized method of a ServletContextListener

SEVERE: Error filterStart

Occurs when an exception is thrown in the init method of a Filter
Unfortunately by default, tomcat won’t provide you with any details about the cause of the error. Infact it wont even tell you which filter or listener is failing. This can be big problem in applications of significant size that have many filters and listeners configured. Fortunately there is a solution. In your webapplication’s WEB-INF/classes folder you can create a logging.properties file with the following contents
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
Now you will be presented with the stacktrace

Reference: http://blog.trifork.com/2011/03/18/debugging-the-dreaded-severe-error-listenerstart-and-severe-error-filterstart-tomcat-error-messages/

Schedule a job using Quartz

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import com.ef.monitoring.FeedUpdaterTasklet;

public class FeedBackEmailScheduler implements Job {

private static Logger logger = Logger.getLogger(FeedBackEmailScheduler.class);

private static Scheduler scheduler;

public static void start() throws Exception {

scheduler = StdSchedulerFactory.getDefaultScheduler();

JobDetail jobDetail = new JobDetail("FeedBackEmailScheduler", Scheduler.DEFAULT_GROUP,
FeedBackEmailScheduler.class);

// Run job at each day 10.30 AM and 6.30 PM
CronTrigger cTrigger = new CronTrigger("FeedBackEmailScheduler", "MCC", "0 30 10,18 1/1 * ? *");

scheduler.scheduleJob(jobDetail, cTrigger);

scheduler.start();
}

public void execute(JobExecutionContext context) {
try {
FeedUpdaterTasklet feedUpdaterTasklet = (FeedUpdaterTasklet) SpringApplicationContextProvider
.getBean("feedUpdaterTasklet", FeedUpdaterTasklet.class);
feedUpdaterTasklet.run();
} catch (Exception e) {
logger.error("Error while monitoring the products", e);
}
}

public static void shutdown() {
try {
if (scheduler != null) {
scheduler.shutdown();
}
} catch (Exception ignore) {
}
}
}

SEVERE: Exception sending context initialized event to listener instance of class + Null Pointer Exception

If at all you are trying to load the bean before, use like below. This is also useful when you try to get a bean in @weblistener  as well.


@Override
public void contextInitialized(ServletContextEvent sce) {
    WebApplicationContextUtils
        .getRequiredWebApplicationContext(sce.getServletContext())
        .getAutowireCapableBeanFactory()
        .autowireBean(this);

    //your code goes here

}


Scheduler Job using Java8, Better than TimerJob


import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

@WebListener
public class BackgroundJobManager implements ServletContextListener {

private ScheduledExecutorService scheduler;

@Override
public void contextInitialized(ServletContextEvent event) {

LocalDateTime localNow = LocalDateTime.now();
ZoneId currentZone = ZoneId.of("America/New_York");

ZonedDateTime zonedNow = ZonedDateTime.now(currentZone);

ZonedDateTime zonedNext1030AM = zonedNow.withHour(11).withMinute(49).withSecond(0);
ZonedDateTime zonedNext630PM = zonedNow.withHour(11).withMinute(50).withSecond(2);

System.out.println(zonedNow);
System.out.println(zonedNext1030AM);
System.out.println(zonedNext630PM);
System.out.println(localNow);

Duration duration1030AM = Duration.between(zonedNow, zonedNext1030AM);
long initalDelay1030AM = duration1030AM.getSeconds();

Duration duration630PM = Duration.between(zonedNow, zonedNext630PM);
long initalDelay630PM = duration630PM.getSeconds();

scheduler = Executors.newScheduledThreadPool(2);

XYZ object = (XYZ) WebApplicationContextUtils
.getRequiredWebApplicationContext(event.getServletContext()).getBean("XYZ");

scheduler.scheduleAtFixedRate(object, initalDelay1030AM, 24 * 60 * 60, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(object, initalDelay630PM, 24 * 60 * 60, TimeUnit.SECONDS);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdownNow();
}


}

Git + How to list all the files in a commit?

git show --pretty="" --name-only bd61ad98

show tables like in mysql

SHOW TABLES WHERE Tables_in_mydbname LIKE '%reward%'

suppose mydbname is 'blogposts'

SHOW TABLES WHERE Tables_in_blogposts LIKE '%reward%'

Firefox ignores option selected=“selected”, Selected = '"

This behaviour is hard coded in FF.

Add autocomplete="off" HTML attribute to the respective select tag.