Spring #Async not working on an anonymous class - spring-scheduled

I am using #Async tag inside a method in Anonymous class. Does Spring AOP support this:
private void scheduleProcessing(final SomePojo somepojo) {
taskScheduler.schedule(new Runnable() {
#Override
#Async("myThreadPoolTaskExecutor")
public void run() {
// biz logic
}
}, 20, TimeUnit.SECONDS);
}

Apparently methods denoted with spring annotations like #Async, #Transactional etc should be called from outside the class (AOP Proxy). Here run will be called somewhere internally when the Thread starts hence not applying the async-ness to the method.

Related

Servlets, init and extends

I'm thinking about how to create a main servlet that the rest of the servlets extends from this. I have some properties that I would like to be accesible for the whole servlets in my app and I want to init them. I think it could be something like this:
public abstract class MainServlet extends HttpServlet {
protected String errorURL = null;
protected String adminMenuURL = null;
protected String AdminLoginServlet = null;
protected String ValidateServlet = null;
// here, more properties and methods...
#Override
public void init() {
errorURL = context.getInitParameter("errorURL");
adminMenuURL = context.getInitParameter("adminMenuURL");
AdminLoginServlet = context.getInitParameter("AdminLoginServlet");
ValidateServlet = context.getInitParameter("ValidatePicsServlet");
// here, some more inits...
}
}
When I create a new Servlet like the following...
public class AdminLoginServlet extends MainServlet {
}
If I forward to AdminLoginServlet, would the parameters (errorURL, adminMenuURL, etc.) be assigned again?
If i override the init method in AdminLoginServlet (and others servlets that extends from MainServlet)... these properties won't be never assigned, isn't it?
How would you do what I pretend to do?
Thanks.
the members adminMenuUrl and so on are assigned when the servlet is first created by the engine, because this is the moment when init() is invoked. Every user request is handled by the same servlet instance, in different threads. When you forward a request to a servlet, the method init() won't be invoked.
you're right, because there can be no instance of MainServlet
I'm not sure how your web app should look like
You should rename the fields AdminLoginServlet and ValidateServlet, because it's bad practice to start fieldnames with upper case letters.

How can I write custom servlet context init method

I wish to set up a few application wide variables with servletContext.setAttributes on servlet context initialization phase .How can I achieve this.
Implement javax.servlet.SevletContextListener which gets a callback when javax.servlet.ServletContext is initialized.
Here is the example:
public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
ServletContext sc = sce.getServletContext();
//do your initialization here.
sc.setAttribute(.....);
}
public void contextDestroyed(ServletContextEvent sce)
{
ServletContext sc = sce.getServletContext();
//do your cleanup here
}
}
If you would like to tie your logic closer to the servlet (and not use a listener), you can override the servlets init method. Like so:
#Override
public void init() throws ServletException {
ServletContext sc = getServletContext();
// Store our attribute(s)!
// Check first to make sure it hasn't already been set by another Servlet instance.
if (sc.getAttribute("key") == null)
sc.setAttribute("key", "value");
}
And you don't have to call through to super.init(config). See docs.

Spring Async Reference

#Bean
public EventHandler eventHandler(){
EventHandler handler= new EventHandler(session());
session().registerEventListener(handler);
return handler;
}
public class EventHandler implements EventListener{
#Override
#Async
public void notify(Event event) {
//do work
}
}
I'm trying to create an async handler and register it...Doesnt seem to work as I believe Spring will only create the async wrappers after it creates a proxy object around the bean after the bean has been created....how do I work around this?
I had a similar problem where I couldn't call an #Override method with #Async - it would always be called synchronously.
My workaround was to declare another #Service bean with a direct (non-#Override) interface to forward the call to the bean I was calling via an #Override interface.
I figure the proxying just doesn't work via base class calls.

ASP.NET MVC - Unit Testing Override Initialize Method

I've got an abstract class shown below which gets inherited by all the other controllers. Is it possible to test this method at all? Btw, I'm trying to use MOQ but no luck. If you could help me will be much appreciated:
public abstract class ApplicationController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
//do some stuff here
}
}
If you take a look at the source code of base Initialize method you will find out that what it does is that it sets up ControllerContext and url stuff. Now, download MvcContrib TestHelper and check out TestControllerBuilder . The builder sets up everything you need in order to have controller context and other stuff which you depend upon.
Ok, we are not over yet - you wanted to test your own override of Initialize right?
TestControllerBuilder doesnt call your Initialize because it does initialization in different way. I suggest you to factor out your custom Initialize() logic out into different method. Then create fake (stub) subclass with public method that calls this factored out protected Initialize. Are you with me?
something like:
public abstract class ApplicationController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
MyInitialzie()
}
protected void MyInitialize()
{
ControllerContext.XXX // do whatewer you want here. Context is already setted up
}
}
class FakeController: ApplicationController
{
public void CallMyInitialize()
{
MyInitialize();
}
}
Later in test class:
[Test]
public void MyInitializeTest()
{
TestControllerBuilder builder = new TestControllerBuilder();
FakeController controller = new FakeController();
builder.InitializeController(controller);
controller.CallMyInitialize();
//TODO: verification of MyInitialize assumptions
}
Is that clear?

ASP.NET: Implementing Init and Dispose methods

Can an ASP.NET web application have only one Init and one Dispose method or can I implement these per class for those which I want to associate such methods?
More specifically I have Customer component and a CustomerRecord classes and would like to implement Init and Dispose methods in both of them.
What is the proper way to do this?
Requirement:
I want to have independent Init and Dispose methods for each aforementioned class.
For classes that should be disposable, by exposing a public Dispose method, the IDispsable interface must be implemented for 'disposability' to be effective out of the scope of explicit user disposal. This has been covered many times in many places, including here, for example:
public class Customer : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SupressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//dispose of managed resources
}
//dispose of unmanaged resources
}
~Customer()
{
Dispose(false);
}
}
Note that the destructor (the method starting with the tilde ~) may not be necessary, but read the details from the answer I linked above for clarity on the situation of what and why - this just answers your question directly.
As for an Init method, are you referring to a constructor?
If so, then look at the destructor in the above example; a constructor (or initialiser) can be defined in the same way minus the tilde and, generally, plus an explicit access modifier (public, private, et cetera), for example:
public class Customer
{
public Customer()
{
}
}
You can create a base class with the Init and Dispose method as you wish and then make the other classes to inherit from it. For example:
public class BaseClass
{
public void Init()
{
//Some code
}
public void Dispose()
{
//Some code
}
}
public class Customer : BaseClass
{
//Some code
}
That might help you.

Resources