How to clear spring webflow scope data on exiting the application by simply closing the browser? - spring

Details:-
Application is developed using Spring webflow 2.1.1 and Spring core framework.
Spring webflow acts as a MVC controller for application
Application is storing lot of data in spring webflow flowscope variables which is rendered on UI using jsf richfaces.
For same application in web.xml session timeout is configured to 120 minutes.
Problem is when browser is closed in between of webflow OR http session is timed out , what happens to data stored in spring webflow flowscope ? It is observed that end state is not called in both scenario. Does that data reside on Java Heap ?
Also as per memory analyser tool, 70% of heap is consumed by webAppServletContext.
Currently application has out of memory issues even after increasing the heap to 3GB.

Spring WebFlow uses HttpSession to store all its contextual information. As long as Servlet Container invalidates/destroys user associated HttpSession, all Spring WebFlow stored variables become destroyed too (well, can be garbage collected).
If you're having memory problems check your Servlet Container settings. Some have mechanisms like a periodic job to clean expired HttpSession, some others clean upon an invalid request, etc...
Anyway, you'll unlikely reach an OutOfMemoryError because of the described mechanisms, as it will show defective Servlet implementation.

Related

Cannot find a working configuration for Http Session replication in Grails 4

I wanted to run my Grails application (version 4.0.4) in a cluster. I tried to apply Hazelcast to replicate the HTTP session across the nodes/instances but somehow I couldn’t override/replace the SessionRepository bean that Grails uses with the Hazelcast implementation.
My working configuration in Spring Boot is: I declare the Hazelcast bean and annotate the Application with #EnableHazelcastHttpSession which in turn introduces the new SessionRepository from Hazelcast.
But I couldn’t make this configuration work in Grails and override the SessionRepository. (Although the app starts, it acts very strange.)
Any ideas?
Or do you suggest an alternative approach to implement a distributed session in Grails? How did you replicate session from your past experience?
(P.S The reason I chose Hazelcast is, since it is a distributed cache which can be embedded with the application itself, I can avoid dependency on external service such as Redis, to run the app. That is part of the requirement).
Thank you.

Transaction management in Spring: Does support come from Spring or container?

I am trying to understand the transaction management in Spring, and I have got some doubts.
I read a bit about transaction management in EJB world, which can be CMT or BMT. For CMT, as per the documentation, it is Application server (e.g. JBOSS) which manages the transaction.
Now, coming to Spring transaction management, and considering using Web container only (Apache Tomcat), how does this work?
Does Spring have its own transaction management with capability of handling local transaction and global transaction (which works with 2 phase commit). Do the actual support need to come by the underlying container (in this case Apache tomcat) or support from framework is sufficient?
I am not clear how all these pieces fit together.
Can anyone help me understand this?
Spring doesn't include any kind of transaction capability of its own, it only provides ways to connect to transaction functionality provided by the container or by standalone libraries.
If you run your application on Tomcat and don't provide any transaction manager libraries like bitronix, then you get only local jdbc transactions provided by the servlet container.
When you read the bullet points at https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html notice it says spring is providing abstractions, that means it is providing access through its own apis and using aop to make transactions nonintrusive, but not providing any implementation of transactional functionality. It's facilitating gluing things together, which is the main thing spring does.

handling Callable, CompletableFuture and Flux

As of spring 5 seems like Flux/Mono can be also be a returned value of spring rest controller method.
I looked at spring reference guide 4.3 and read a pretty nice description of how Callable and DeferredResult are handled (https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/htmlsingle/#mvc-ann-async)
I also read on some blog posts that CompletableFuture can also be returned from controller methods. If it is true than can anybody shed some light how
CompletableFuture and Flux/Mono are handled in Spring MVC ?
Are they handled more less the same way as DeferredResult/Callable, i.e. "processing" done in an external thread and then Spring MVC dispatches the request back to DispatcherServlet (so the HTTP Response is written to and flushed on servlet container thread)
Indeed, as of Spring Framework 5.0 Spring MVC can handle Publisher-like return types in controller handlers. Depending on the cardinality of the returned type (one or multiple possible values?), Spring MVC will handle it with:
a DeferredResult if returning a single value
a DeferredResult<List<?>> if it's returning an asynchronous collection, collecting all elements in a list
a ResponseBodyEmitter or SseEmitter if you're streaming data to the client
Now the underlying processing model is using Servlet 3.0 async support, moving the blocking I/O operations to a non-container thread, dispatching back to the container when it's done.
On the other hand, Spring WebFlux is fully asynchronous and non-blocking (using Servlet 3.1 support for Servlet containers); in that case, fewer threads will be used.
More on that in the reference documentation.
This video nicely explains what's going on in spring mvc https://www.infoq.com/presentations/servlet-reactive-stack. Simply startAsync is used to resume(dispatch) to servlet thread to write (in a blocking way) to servlet response.

How to make spring servlet load-on-startup wait after tomcat jndi (global resources) are loaded

I have a Spring servlet called with load-on-startup = 1 and it requires a database connection, but sometimes, connection is null and the appLoader servlet fails. The connection is null because tomcat didn't had the time to load the global resources properly.
Question : How do I ask Spring to wait for jndi proper loading? I mean, order it in some way.
Dirty solution : Make another servlet with active wait and waiting for a working connection before calling my appLoader...
1) The job of <load-on-startup> is to start a servlet at the time of deployment. It just initiates(starts) the process after that it has no control over that servlet.
The entire control of the started servlet is in the hands of the web container.
The behaviour of the web container can be programmatically controlled using Event Handling mechanism.
2) Events are basically occurrence of something. Changing the state of an object is known as an event.
2.1) You can handle the events of web container using event listeners of servlet and perform some important tasks such as creating tables of the database, creating database connection object etc. at time of deploying the project.
There are many Event classes and Listener interfaces in the javax.servlet and javax.servlet.http package.
You can use ServletContextListener to store the connection object in the context attribute and later fetch that connection object from servlet context.
Here are few examples where database connection object is stored in servlet context. example1 example2 example3
2.2) You can use Event Handling mechanism of spring framework.
In spring you can poll your database connection using ContextStartedEvent that is raised when an ApplicationContext gets started.
Here, are some spring events with example.

How spring knows to refresh itis initial context if one cluster member goes down

I am using spring portlet mvc as my front end and connecting to some remote EJB running on a WAS. Now in my configuration file for the portlet where I specify the remote EJB lookup url, I have specified the url(s) as a cluster since the EJB is deployed in a clustered WAS. So the url looks like :iiop://server1:port,iiop://server2:port.
Now to save resources, spring mvc caches the initial context.Now I am noticing that spring is always able to make a connection to the remote ejb so long as one of the servers is up.
This is confusing to me since the cluster is resolved(for lack of a better word) at the time of lookup of the initial context and after that if the cluster member goes down, there should be a connection exception. So how does spring know when it should automatically refresh its initial context because the old context has become stale?
I found that in the applicationContext.xml file, there is the declaration:
<jee:remote-slsb id="remoteService" jndi-name="com.business.ejb.ServiceSLRemote" business-interface="com.business.ejb.ServiceSLRemote" cache-home="true" lookup-home-on-startup="false" resource-ref="false" refresh-home-on-connect-failure="true">
<jee:environment>
java.naming.factory.initial=${JAVA.NAMING.FACTORY.INITIAL}
java.naming.provider.url=${JAVA.NAMING.PROVIDER.URL}
</jee:environment>
</jee:remote-slsb>
It is the refresh-home-on-connect-failure="true" that tells the spring container that if the initial context has become stale, then it should refresh the connection. That is how it can work so long as one cluster member is active.

Resources