Symfony2 Handler vs Listener what the difference? - symfony

I need to create listener or handler to provide some actions for AccessDenied exception.
I've looked some sources of standard handlers and listeners, so looks like they can be use for same task.
I also read this:
What's the difference between Event Listeners & Handlers in Java?
JavaScript - What's the difference between event handlers & listener?
Using Symfony2's AccessDeniedHandlerInterface
But only difference I've seen - that handlers often use for handle exceptions.
So, what the real difference between handler and listener in Symfony ?

Listeners are registered and called when an event occurs. Observer or PubSub patterns are used.
The Handler is more of a strategy pattern that delegates implementation details to class. Which can then be substituted. (composition over inheritance)
The patterns could maybe be interchangeable in some cases, and it's more the intention of the code that calls for one or the other then.

Related

Different handlers for ejb Timer

I'm trying to set different handlers for different types of tasks created via TimerService , ejb.
I need to figure out a way where I can create a schedule tasks with extra information including the handler type so when a timeout occurs different handlers should be fired according to the timer identification.
Eventually the solution to the problem as i tried to explain is by adding Serialized handler, which is set when you create a timer event.
after you set a handler you can get it from the timer object, so when a timeout occurs we can execute an action according to the relevant handler.

What is the correct way to add an EventListener to an AtmosphereResource?

I am using Atmosphere Framework 2.0.8.
I have implemented an AtmosphereHandler in my application and have two way communication occurring correctly over WebSockets and Long Polling.
I am trying to add some handling code for when the client disconnects to clean up some resources specific to that client (ie. I have an entry in a table I want to remove).
I have read the following wiki entries:
OnDisconnect Tricks: https://github.com/Atmosphere/atmosphere/wiki/onDisconnect-tricks
Configuring Atmosphere Listener: https://github.com/Atmosphere/atmosphere/wiki/Configuring-Atmosphere-Listener
The thing I am not clear on is where I should add the call to
atmosphereResource.addEventListener( new AtmosphereResourceEventListenerAdapter() {} );
I eventually found some example code in the JavaDoc for the AtmosphereHandler that registers the EventListener in the onRequest() method. http://atmosphere.github.io/atmosphere/apidocs/org/atmosphere/cpr/AtmosphereHandler.html
What I would like to know is if this is the correct way to go about it?
It is my understanding that the AtmosphereResource represents the connection between a client and the server for the life of that connection. The uuid stays consistent for the object on multiple calls through the onRequest() method from the same client. As such, the same AtmosphereResource object will get the EventListener added every time the onRequest method is called.
This seems wrong. Wouldn't this lead to thousands of EventListeners being registered for each AtmosphereResource?
It seems that the EventLister should only be registered once for each AtmosphereResource.
I feel like I am missing something fundamental here. Could someone please explain?
Here's an example using MeteorServlet, so it won't look exactly like what you'll have to do, but it should get you started. I add the listener to a Meteor instance, and you'll add yours to an AtmosphereResource. Each resource gets just one listener.
The overridden onDisconnect() method calls this Grails service method that handles the event. Of course, you'll want to call something that cleans up your database resource.
Note that the servlet is configured using these options. I think you might need the org.atmosphere.interceptor.HeartbeatInterceptor, but it's been so long since I initially set it up, I can't remember if it's necessary.

CDI Like Events in ASP.NET

I am primarily a JEE6 developer. I did like to know if there is a way to implement Fire and forget functionality of CDI events in asp.net. and the event observers get to receive a specific data payload with which to respond to the event
Thank you
Sure, you'd have to have a registration object always available so your listeners can register thir interest. Then that same object will have a fire method, or an event object would call that fire method, then you just iterate through the listeners and call their method. Use interfaces to keep it simple.

MVC 3/4 HttpModule or ActionFilter

I need to check some stuff (Cookies) for each request coming to my application.
In ASP.NET we've used HttpModule for this task , the question what should be used in MVC ? Some Global Filter , or I can Use HttpModuler as well, is there Any difference in Request PipeLine between MVC and regular ASP.NET ?
MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:
OnActionExecuting – This method is called before a controller action is executed.
OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
BeginRequest - Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event.
AuthenticateRequest - If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to.
AuthorizeRequest - This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so.
PreRequestHandlerExecute - This event occurs before the HTTP handler is executed.
PostRequestHandlerExecute - This event occurs after the HTTP handler is executed.
EndRequest - Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page.
So it really depends on when you need to hook in your event and which events you need.
If the HttpModule worked well for you before then it will continue to with Mvc.
The other parts of your question are quite broad in scope and think you'd be as well reading a good article on asp.net-mvc pipeline and extensibility.
I've done similar things using a global action filter. It works quite well, and keeps your code integrated within your application.
An HTTP module works as well, of course, but this will mean seperating the code from your main application and maintaining it seperately. Unless your code spans multiple sites or is used in multiple applications, or needs to work with web forms sites, then I would use a global filter.

Handling Login.Authenticate event

A) Book I’m learning from says that if we handle Login.Authenticate event, then we have to authenticate users on our own. Thus control won’t automatically validate username and password. I thought book suggested this would only happen if we override Login.OnAuthenticate() method, but it appears that even if only add an event handler for Authenticate event, the automatic authentication doesn’t happen.
But why is that? Why doesn’t event handling work like it does with Init or Load events, where you essentially have to override Page.OnInit() and Page.OnLoad() in order gain control over event handling?
B) I checked MSDN site and it basically recommends that if we do override Login.OnAuthenticate(), we should also call base.OnAuthenticate(). But then, why would we ever need to override Login.OnAuthenticate(), if we get the same effect with simply declaring an event handler for Login.Authenticate?
thanx
You should not override OnAuthenticate. This method is only used internally to either raise the Authenticate event to registered handlers if there are any or authenticate via the current MembershipProvider if there are no handlers registered. So, to implement custom authentication for the Login control, you simply register a handler for the Authenticate event and set the AuthenticateEventArgs.Authenticated property.
Event handling for the Authenticate event works exactly the same as for other events. The only difference is that the OnAuthenticate method has some logic that determines whether to use a MembershipProvider or a registered event handler for authentication.
If you do create a subclass of Login and override Login.OnAuthenticate, it is wise to call base.OnAuthenticate(...) because it contains the logic that calls the registered event handlers. If you do not call base.Authenticate(...), you should call the registered event handlers yourself. But creating a subclass of Login is probably not necessary in your situation.

Resources