Manage events in Spring app - spring

I have a Spring MVC app that needs to send email notification each time and event occur. Those events are detected in three different classes so I need my email service to watch for them. How can I control if an event have occurred? I was thinking in use an observer pattern but I'm not sure if this is a good idea and I don't know how to know what event I'm processing.

I guess whether using an observer is a good idea depends on your application architecture and your design preferences. If you do choose to go with the event approach, Spring provides infrastructure to support that.
In order to implement the event approach with Spring you will need a publish, a listener and an event definition. You will achieve that with classes that implement ApplicationEventPublisherAware, ApplicationListener and ApplicationEvent
You can find a detailed explanation at http://techighost.com/event-handling-with-spring-framework/

Related

Different polling delay for different suppliers in Spring Cloud Stream Function

I'm trying to implement suppliers using Spring Cloud Function and Kafka. I need that one supplier should publish after every 10 secs and other should publish after every 30 secs. I could see from documentation, I can change delay using spring.cloud.stream.poller.fixed-delay property. Reference
But I need to set different delay for each topic. Is there any way to do it?
From the spring-cloud-function perspective there isn't any kind of polling as it is not the responsibility of the framework.
From the spring-cloud-stream perspective that uses spring-cloud-function indeed there is a mechanism that you have described. However, keep in mind that spring-cloud-stream is primarily designed to support concept of microservices (not your general messaging framework) and in microservices we embrace do one thing but do it well without affecting others approach. So having more then one supplier kind of goes against this model.
If you are building a general purpose messaging app, then i'd suggest to use Spring Integration framework which provides all the necessary hooks to accomplish what you need, but will require a bit more configuration details.

What is the typical use case of Spring Application Events?

There is an amazing mechanism is spring: Spring Application Events, I see it helps to build loosely coupled application, implement observer pattern, reactor pattern.
My question is, what is the trigger in Spring application architecture when Spring Application Events is absolutely inevitable? Actually any application classes relationship can be build using events only as well as using class associations and hierarchy only (i'm talking about monolith service now).
May be it's more architectural question, but what is the threshold when it's need to consider events between objects inside a spring application?
Could it be definitely seen the cases when Spring Application Events absolutely needed?
Spring Security pushes events on certain event occurrences. See list of events
In some cases, it's much better to push message instead invoke a method in a classical way. Eg if you need to invoke the same function I multiple places in the code, just to notify another object.
It allows us to build a system which utilizes event-driven architecture. Read more
Helps in solving a producer-consumer problem
Send emails by pushing event object to ApplicationEventPublisher See Spring Higher-Order Components and #EnableEmailSending

SDN4 - Entity lifecycle event handlers compatible with GraphRepository

I am using Spring Data Neo4j 4.0.0.RELEASE and would like to take advantage of the built-in data manipulation events to insert some audit information on the fly (e.g. timestamps). The documentation seems to suggest that this is only available to me if I am directly using Neo4jTemplate.
Are there any similar hooks available for the GraphRepository abstraction? That is, is there an out of box way for me to hook into graph repository operations (a la Spring DataJPA?) I've written some tests and can confirm that the documented events don't fire when I'm just using the GraphRepository.
AbstractGraphRepository is from the 3.x codebase, so is not directly relevant here.
As noted, SDN 4 does not yet provide automatic support for Spring's RepositoryEventListener interfaces. Implementing event listeners correctly in SDN 4.0 is complicated because of the nature of the underlying save mechanism, which persists an entire tree of "dirty" objects rather than just a single top-level entity. If the object you want to intercept is not the top-level entity being saved, the event listener for it won't fire.
The SDN development team is currently considering the best way to enable event handlers to fire for objects that may be persisted at any depth in the save tree.
In the meantime, the solution suggested by simonl should work.

Spring Context Event

I am currently studying Spring.
While reading a Spring book, I met a part regarding Event.
By using context.publishEvent(..), I could trigger the event.
But I don't know what It's exactly for.
I can use other Method instead of using complicated publishEvent.
Please, tell me. thank you.
Spring Events are used to implement publish-subscribe model (or observer pattern) where two not-related parts of code must be somehow connected.
Think of the analogy of web applications where servlet container creates http sessions and your code is informed about this using javax.servlet.http.HttpSessionListener.
Spring uses this mechanism internally. It's much more visible in Spring Security where several parts of the code are informed about e.g., successfull authentication.

Event handling mechanism between ManagedBeans in JSF2?

Is there a way to decouple ManagedBeans from each other in a way that it is possible to send and receive custom events - probably over the (cool) FacesContext?! I do not want to inject Beans as ManagedProperty, to reduce direct dependencies. Unfortunately #ListenerFor and all that new stuff does only work for components and renderers and seems completely the wrong approach.
Those of you who are familiar with Adobe Flex' event mechanism know what I mean and what I expect from a standardized web UI framework.
Please let me know an elegant way that is included in the JSF specification without the need to implement another framework around.
Is there a way to decouple ManagedBeans from each other in a way that it is possible to send and receive custom events - probably over the (cool) FacesContext?!
Not without adding the event to a component, and you would have to add it before the Event phase of the JSF lifecycle.
I do not want to inject Beans as ManagedProperty, to reduce direct dependencies
Just because you are not injecting needed dependencies into your bean, doesn't mean that those dependencies wouldn't exist anyway if you are trying to go with an event driven model. At least by injecting the dependencies you explicitly declare what the managed bean depends on. This seems like a much more maintainable solution than what you are proposing.
Those of you who are familiar with Adobe Flex' event mechanism know what I mean and what I expect from a standardized web UI framework.
You expect a desktop based event driven model in a web application framework? This is apples to oranges. Adobe Flex is a Rich Internet Application that behaves like a desktop application while communicating with outside web services. JSF is a web application framework standard for web based components powered by javascript and ajax, with reusable server components and a server lifecycle which includes an event phase for components.
Please let me know an elegant way that is included in the JSF specification without the need to implement another framework around.
Your language implies that you do not find JSF elegant and that you are actively trying to make it something that it is not. Please do not do this, you will create a nightmare for yourself and anybody who has to maintain your application.
JSF requires a different way of thinking about web application development than what you are used to. If you find it this unpalatable then I suggest abandoning it for a web application framework that fits your comfortability level. You mentioned Flex, there is also Silverlight with .NET.

Resources