Spring Session with hazelcast session events firing - spring

I am using Spring Session with Hazelcast and Spring Websockets. As I don't need clustarization I use hazelcast with MapSessionRepository. But it doesn't fire event on session expiring or session deleting. What I want is to listen to SessionExpiredEvent and then disconnect user via websocket immediately. So I have two problems:
MapSessionRepository does not firing needed events (SessionExpiredEvent and etc.)
I don't realize how to send websocket notification using expired http session. I need something like simpMessageTemplate.convertAndSendToUser().
But how I can get the user?
So the only one variant I can see is to write own implementation for SessionRepository<ExpiringSession> with events firing. I hope you understood my question. Thanks in advance.

Spring Session Hazelcast support does provide publishing of session events. The functionality itself is implemented using SessionEntryListener so make sure you use #EnableHazelcastHttpSession annotation which configures all the necessary components for Hazelcast integration.
It might also be of your interest that the upcoming Spring Session 1.3 (currently at 1.3.0.M2) will provide first-class Hazelcast support with new HazelcastSessionRepository (which will, among other things, replace SessionEntryListener).

Related

Spring Integration with Spring Boot - High Availability

A spring integration project pulls emails from Exchange Server using imap-idle-channel-adapter; it transforms the message; it invokes some SOAP webservices and persists data in DB using Spring Boot and JPA. All works fine.
This needs to be deployed in a four-weblogic-server cluster environment.
Could someone please help with some hints on what needs to be done? Is there any configuration needed?
As long as your logic is just like you show and there is no any more endpoints polling shared resource, your are good so far do nothing more. The mail API has built-in feature to mark messages in the box as read or at least seen, so other concurrent session won’t poll those messages again.

Thread model for Async API implementation using Spring

I am working on the micro-service developed using Spring Boot . I have implemented following layers:
Controller layer: Invoked when user sends API request
Service layer: Processes the request. Either sends request to third-part service or sends request to database
Repository layer: Used to interact with the
database
.
Methods in all of above layers returns the CompletableFuture. I have following questions related to this setup:
Is it good practice to return Completable future from all methods across all layers?
Is it always recommended to use #Async annotation when using CompletableFuture? what happens when I use default fork-join pool to process the requests?
How can I configure the threads for above methods? Will it be a good idea to configure the thread pool per layer? what are other configurations I can consider here?
Which metrics I should focus while optimizing performance for this micro-service?
If the work your application is doing can be done on the request thread without too much latency, I would recommend it. You can always move to an async model if you find that your web server is running out of worker threads.
The #Async annotation is basically helping with scheduling. If you can, use it - it can keep the code free of the references to the thread pool on which the work will be scheduled. As for what thread actually does your async work, that's really up to you. If you can, use your own pool. That will make sure you can add instrumentation and expose configuration options that you may need once your service is running.
Technically you will have two pools in play. One that Spring will use to consume the result of your future, and another that you will use to do the async work. If I recall correctly, Spring Boot will configure its pool if you don't already have one, and will log a warning if you didn't explicitly configure one. As for your worker threads, start simple. Consider using Spring's ThreadPoolTaskExecutor.
Regarding which metrics to monitor, start first by choosing how you will monitor. Using something like Spring Sleuth coupled with Spring Actuator will give you a lot of information out of the box. There are a lot of services that can collect all the metrics actuator generates into time-based databases that you can then use to analyze performance and get some ideas on what to tweak.
One final recommendation is that Spring's Web Flux is designed from the start to be async. It has a learning curve for sure since reactive code is very different from the usual MVC stuff. However, that framework is also thinking about all the questions you are asking so it might be better suited for your application, specially if you want to make everything async by default.

Configure spring integration poller based on some event

I am using jdbc-inbound-channel-adapter to poll data from database and with a poller having fixed-rate. Now I don't want to constantly look into the database. I want to have the database change notification in place to send the notification on our application and depending on that notification/event we need to invoke the poller for retrieving data.
Can you please help me what could be the configuration for the same? It would be much helpful if we can configure an event based poller for polling the data.
Thanks in advance,
Sandip
Pollers are based on the Spring Framework Trigger; you can't change the poll until the next trigger occurs.
Spring Integration provides a Conditional Pollers which allow you to ignore a poll until some condition is true. Not really event-driven, but closer to what you want.
You could also use the jdbc outbound gateway for event-driven processing.

How do Applications are being notified by Gemfire

In a web application that uses Gemfire to store their Session (Spring Session - an implementation of HTTPSession), how does Gemfire notify the changes (like cache expiry, cache change, cache destry..events) ? especially when Gemfire is deployed in a Client-Server model or Peer-to-Peer model..etc. the event might get triggered in any one node in the entire distributed environment, we might not want to keep listening to all the nodes..etc.
I see Gemfire has MBeans which emits notifications, should our application hook listeners to these MBean notifications or is there any other better way ?
The purpose is to put some clean-up code during such events.
you could use the GemFire event listener model approach whereby each node would listen for events (poll - sorta), rather than have the source 'push' to each node. in GemFire, have a look at the ContinuousQueryListener (Spring Integration Example or Spring Data GemFire docs) to enable you to subscribe to events.

Detecting disconnects using Spring messaging (websockets / STOMP)

We're using spring-messaging websocket with STOMP. Now we've defined multiple #SubscribeMapping methods, but is there also some sort of callback that gets fired when a client application disconnects from the socket?
We got a list of connect clients and some additional properties, like status. Status field needs to be updated when the client application disconnects.
Any suggestions on how to achieve this in Spring? I've tried implementing ApplicationListener however, there's no usefull information in the OnSessionDisconnect object.

Resources