Spring #Async on implementation method not executing - java

I have a service interface and implementation class. When I annotate an interface method with #Async, it works properly. However, when I annotate a method on the implementing class, the call to the method returns immediately, but no code in the method actually gets run. Spring's documentation examples show the annotation used on implemented methods, but it's not working that way for me.

I think this is how it is supposed to work that is why it asyncronous. Did you put any break point to your Async annotated class. Put a system out log and make sure that it is not being called actually.

If you have done everything correctly you may have this issue: https://jira.springsource.org/browse/SPR-10715

Related

Is it possible to use #Transactional annotation on aspect method instead of service method?

I have a micro-service project and I am adding a common feature to every service. For that I wrote code in my common service and it works fine as long as I put #Transactional annotation on every method in service classes of all my services. This is very annoying cause I have to annotate lots of methods in many places. Is it possible to use annotation just once and it will be applied to other methods? Let's say to annotate a method of aspect that intercepts all necessary methods?
I tried to use annotation in my aspect but it does not work...But I still think that there is should be some way to solve this problem.

java annotation for runtime preprocessing on class already coded and in use

I would like to define an annotation for methods that could execute some code before the method starts.
As I understand, it is possible to use proxy and invocation handler to perform this kind of task ( like for instance in : http://www.concretepage.com/java/dynamic-proxy-with-proxy-and-invocationhandler-in-java). However this means first defining an interface, then a class that implements that interface, and then use the proxy.
so if there is some existing code, already using an class, this technique will not allow me to add the annotation and leave everything else unchanged..
is there a way to make such annotations in java ?
thanks

Check if the method is invoked

I am trying to create a logging OSGi bundle, which would be able to log input, output, exception (if any) parameters of all the methods which are marked with specific annotation.
I have tried to set up the Spring AOP logging in OSGi but I was unsuccessful.
All I need to know is how to determine when the method is being called? For example I have scanned all methods in a class which are annotated with my annotation and I have those methods in array or list.
For example there are three methods: getStatus, getDetails, getSomething. And the user invokes getDetails method, so how should I know when this method is invoked?
Detecting method calls requires you to instrument your code. If you want to do this at runtime this requires byte-code manipulation (which is what Spring AOP does, as far as I know). Alternatively you could instrument your code at compile-time using a custom preprocessing step that generates the instrumented Java-code, but I don't know if that is much easier.
I guess your best bet is to try and Spring AOP working.

#Asynchronous private method in EJB

I have an asynchronous method in my EJB singleton that's called from another method in the same class. I already know that I can't call the asynchronous method directly, I have to obtain an EJB proxy. The problem is, I don't want the asynchronous method to be visible outside the class; but when I make it private, it's not executed asynchronously. (I'm using Glassfish v3.)
The javadocs don't say anything about the required access level. So should this be considered a bug in Glassfish?
method annotation cannot be used in private methods. When Glassfish is compiling your EJB it will basically convert your annotation into a piece of code that will surround your code in a proxy. If your method is private it will bypass the proxy Glassfish created... So, in your case I suggest to create a new EJB with your asynchronous method in inject it in your current EJB
That's a very interesting bit of feedback. I can see the value in what you are trying to do. Try marking your bean as an #LocalBean and annotating your #Asynchronous method as protected.
As #LocalBean support is basically done via subclassing the bean class (dynamically or statically), it isn't really possible for the container to override the private method. But I can certainly see your use case. If the protected method approach doesn't work, we can probably add this as an enhancement to EJB.next.
Would still give access to other beans in the same package, but it's at least less public. I've often wished Java had an 'only subclasses' scope. I've almost never used protected and thought, "great, now everyone in my package can access this too."

Java Web Service operation

I have a set of statements to be executed repeatedly every time a web method is called with a new service.I tried writing in the constructor but, the constructor gets invoked only once when the server starts. Instead, I need the set of stmts to be executed each time a Service is created at the client.
As per your other quuestion: maybe interceptors will help
You're looking for #PostConstruct:
#javax.annotation.PostConstruct
public void postConstruct(){
// initialization code
}
A method annotated with #PostConstruct is called by the container once after the bean is instantiated.
EDIT: I think, I misunderstood your question. You're probably looking for interceptors as suggested by djna, i.e., javax.ejb.Interceptors annotation. You can find a good introduction here (as soon as maintenance of java.net is over).

Categories

Resources