DaithiG
DaithiG

Reputation: 969

Incorporating Redis/Spring Session into my application prevents me from deploying the app

I am trying to incorporate SpringSession into my existing (non-Spring boot) application. I have followed Baeldung, which seems to make the most sense: https://www.baeldung.com/spring-session

Here is my Maven dependencies:

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>   

Here is the configuration code that I have created:

@Configuration
@EnableRedisHttpSession
public class SessionConfig{

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new 
JedisConnectionFactory();
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setDatabase(1);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

And here is the Initializer class that extends the HttpSessionInitializer:

public class Initializer extends AbstractHttpSessionApplicationInitializer {
    public Initializer() {
        super(SessionConfig.class);
    }
}

My understanding is that this creates a bean that effectively intercepts sessions and uses Redis to manage them rather that using the Java In Memory session management.

However. I'm unable to deploy the application and am getting the following error in mt catalina logs:

07-Aug-2018 11:13:24.241 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener] java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml! at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:262) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4751) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:629) at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

What I derive from this is that the classes that I am now including (possibly in the AbstractHttpSessionApplicationInitializer) are trying to load an application context which is conflicting with the ContextLoaderListener that is already declared in my web.xml. But if I remove that Listener from the web.xml, then that doesn't allow my app to deploy also.

I'm also getting this exception in my tomcat logs, but not sure if it's related:

07-Aug-2018 11:14:57.652 INFO [ektorp-idle-connection-monitor-thread-1] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [org.apache.http.pool.AbstractConnPool$4]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [org.apache.http.pool.AbstractConnPool$4]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1311) at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1299) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1158) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) at org.apache.http.pool.AbstractConnPool.closeExpired(AbstractConnPool.java:558) at org.apache.http.impl.conn.PoolingClientConnectionManager.closeExpiredConnections(PoolingClientConnectionManager.java:302) at org.ektorp.http.IdleConnectionMonitor$CleanupTask.run(IdleConnectionMonitor.java:52) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Is there anything that I am missing that could cause this error?

Upvotes: 0

Views: 855

Answers (1)

Vedran Pavic
Vedran Pavic

Reputation: 2389

If you're using web.xml to bootstrap you web app, then you shouldn't be using AbstractHttpSessionApplicationInitializer but rather traditional XML configuration based approach.

You can take a look at Redis HttpSession with XML config guide that's available from Spring Session's reference documentation. Your Spring config should contain the

Upvotes: 0

Related Questions