-
1. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 28, 2012 9:55 AM (in response to go.a.)Who tell you that you can use @PostConstruct on interceptor's method???
This annotation is ONLY for EJB.
I think you should use @AroundInvoke instead.
My interceptor:
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SessionTokenInterceptor {
Logger log = Logger.getLogger(SessionTokenInterceptor.class);
@PersistenceContext
protected EntityManager em;
@Resource
private EJBContext ejbContext;
@AroundInvoke
public Object processSessionToken(final InvocationContext invocationContext) throws Exception{
//1. session token
...
//2. JAAS username
...
//3. check
...
//4. call original method and return its result so that other interceptor can continue
try{
return invocationContext.proceed();
}finally{
}
}
-
2. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
go.a. Nov 28, 2012 3:09 PM (in response to ybxiang.china)Hi,
thank you for your reply.
My goal is to intercept the @PostConstruct methods in other Classes (in my example the class MyEJB), i.e. I am not interested in intercepting any other methods, only @PostConstruct methods.
As far as I know this is called Lifecycle Interceptor and it is defined/used as in my example, I hope - (see e.g. bottom of http://docs.oracle.com/javaee/6/api/javax/interceptor/package-summary.html ).
And as I said, the code works, the only problem is the warning, which confuses me - so why does it occur and how to (appropriately) get rid of it?
-
3. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 28, 2012 7:44 PM (in response to go.a.)How about bellow codes?
public class LifecycleInterceptor {
@AroundInvoke
private void postConstructInterceptor(InvocationContext ctx) {
try {
ctx.proceed();
} catch (Throwable e) {
// do sth.
}
}
}
and
@Singleton
public class MyEJB {
@Interceptors({LifecycleInterceptor.class})
@PostConstruct
private void init() {
// do sth.
}
// ... other methods
}
-
4. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 28, 2012 7:53 PM (in response to go.a.)(see e.g. bottom of http://docs.oracle.com/javaee/6/api/javax/interceptor/package-summary.html ).
Maybe you are right.
Maybe jboss 7 does NOT support it well?
Maybe I am wrong.
-
5. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 28, 2012 7:55 PM (in response to go.a.)I use those annotations according to the book "Enterprise.JavaBeans.3.1_6th.Edition".
They always works well in jboss because the author had tested related examples with JBoss: the package name is org.jboss.ejb3.examples.ch07...
-
6. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
nickarls Nov 29, 2012 2:14 AM (in response to ybxiang.china)Which example? https://community.jboss.org/wiki/Chapter7-SingletonRSSCache ?
-
7. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
jaikiran Nov 29, 2012 3:11 AM (in response to go.a.)WARN [org.jboss.interceptor.reader.InterceptorMetadataUtils]
That WARN is coming from a Weld class, which is strange. Do you have CDI components in your application?
-
8. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
nickarls Nov 29, 2012 3:37 AM (in response to jaikiran)It appears to be coming from CDI, yes:
public static boolean isInterceptorMethod(InterceptionType interceptionType, MethodMetadata method, boolean forTargetClass) { if (!method.getSupportedInterceptionTypes().contains(interceptionType)) { return false; } if (interceptionType.isLifecycleCallback()) { if (!Void.TYPE.equals(method.getReturnType())) { if (LOG.isWarnEnabled()) { LOG.warn(getStandardIgnoredMessage(interceptionType, method.getJavaMethod()) + "does not have a void return type"); } return false; } Class<?>[] parameterTypes = method.getJavaMethod().getParameterTypes(); if (forTargetClass && parameterTypes.length != 0) { if (LOG.isWarnEnabled()) { LOG.warn(getStandardIgnoredMessage(interceptionType, method.getJavaMethod()) + "is defined on the target class and does not have 0 arguments"); } return false; }
-
9. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 29, 2012 8:38 AM (in response to nickarls)I copied and modified example codes from chapter 18 Page326 and page 328 in Enterprise.JavaBeans.3.1_6th.Edition.pdf.
(ch18-interceptors\src\main\java\org\jboss\ejb3\examples\ch18\tuner)
1. page 326 ********************************************************************************************************************************************
public class RecordingAuditor
{
/**
* Logger
*/
private static final Logger log =
Logger.getLogger(RecordingAuditor.class.getName());
/**
* Writable store for incoming invocations; imagine this is available to us
* and that it'll either log or otherwise store persistently in a DB
*/
private final Recorder<InvocationContext> invocations ; // We made up "Recorder"
/**
* Persistently records the intercepted {@link InvocationContext} such that
* we may examine it later
*/
@AroundInvoke
public Object audit(final InvocationContext context) throws Exception
{
// Precondition checks
assert context != null : "Context was not specified";
// Record the invocation
invocations.add(context);
// Carry out the invocation, noting where we've intercepted
// before and after the call (around it)
try
{
// Log
log.info("Intercepted: " + context);
// Return
return context.proceed();
}
finally
{
// Log
log.info("Done with: " + context);
}
}
}
2. Page 328 ************************************************************************************************************************************************************************************
@Stateless
// Class-level interceptors will be run upon requests to every method of this EJB
@Interceptors(RecordingAuditor.class)
@Local(TunerLocalBusiness.class)
public class TunerBean implements TunerLocalBusiness{
...
}
Maybe what Andreas G talked about is in page 333 or 338 (I did NOT read them carefully):
3. Page 333 -- Intercepting Lifecycle Events ************************************************************************************************************************
public class JndiInjector
{
@PostConstruct
public void jndiInject(InvocationContext invocation) {
....
4. Page 338: **********************************************************************************************************************************************************
@Stateless
public class MySessionBean implements MySessionRemote {
public void businessMethod( ) {
...
}
@AroundInvoke
public Object beanClassInterceptor(InvocationContext ctx) {
try {
System.out.println("entering: " + ctx.getMethod( ));
return ctx.proceed( );
} finally {
System.out.println("leaving: " + ctx.getMethod( ));
}
}
}
-
10. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
ybxiang.china Nov 29, 2012 8:35 AM (in response to go.a.)I had read every edition of "Enterprise.JavaBeans" serial books.
(Many chapters were NOT understood completely 4 years ago, but I like this book deeply. I like jboss documentation page too)
-
11. Re: Usage of a lifecycle Interceptor produces a WARN message in AS7
go.a. Nov 30, 2012 8:02 AM (in response to go.a.)Thank you for the workaround, unfortunately it does not work for me - the @PostConstruct annotated method is not called in that case.
I did not read the specification, but I assume, that @AroundInvoke invokes only 'normal' methods but not lifecycle-methods like @PostConstruct.
@Nicklas Karlsson and @jaikiran pai
yes, we also use one or two CDI classes in our code and apart from that we inject our EJBs with @Inject instead of @EJB
###################################################
By using the following code, the WARN message disappears:
###################################################
@Interceptor // new
@LifecycleInterceptorBinding // new
public class LifecycleInterceptor {
@PostConstruct
private void postConstructInterceptor(InvocationContext ctx) {
try {
ctx.proceed();
} catch (Throwable e) {
// do sth.
}
}
}
and
@Singleton
@Interceptors({LifecycleInterceptor.class})
public class MyEJB {
@PostConstruct
private void init() {
// do sth.
}
// ... other methods
}
and the new LifecycleInterceptorBinding-Annotation:
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface LifecycleInterceptorBinding {}
This code behaves as before (i.e. it behaves as the code without the @Interceptor and @LifecycleInterceptorBinding annotations), the only difference is that the WARN message disappears.
What is the reason that for?