• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AroundInvoke

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package pack;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class AccountCreatorLogger {


@PostConstruct
public void initialize(InvocationContext ic) throws Exception{
System.out.println("Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
ic.proceed();
}

@PreDestroy
public void cleanUp(InvocationContext context){

}




@AroundInvoke
public Object logMethodEntry(InvocationContext ic)throws Exception{
System.out.println("Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
return ic.proceed();


}
}

-------------
my doubt here is why do the compiler complain's if i add annotated method @AroundInvoke before @PostConstruct or @PreDestroy.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my doubt here is why do the compiler complain's if i add annotated method @AroundInvoke before @PostConstruct or @PreDestroy.

Are you using an IDE ? It should not be a problem to declare it before the others.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i am using an Eclipse IDE.

Christophe how you do normally run in your code in IDE or where ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Eclipse, and have no compile error with this interceptor, whether AroundInvoke is before or after PostConstruct. What compile error do you have ?
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe,

if i use like this in order

-------------
@AroundInvoke
public Object logMethodEntry(InvocationContext ic)throws Exception{
System.out.println("Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());

return ic.proceed();

@PostConstruct
public void initialize(InvocationContext ic) throws Exception{
System.out.println("@PostConstruct Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
ic.proceed();
}

@PreDestroy
public void cleanUp(InvocationContext ic) throws Exception{
System.out.println("@PreDestroy Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
ic.proceed();
}
-------------------------

the complier complains about the following error's


1) public void initialize(InvocationContext ic) throws Exception{

void is an invalid type for the variable initialize

2) public void cleanUp(InvocationContext ic) throws Exception{

Syntax error on token "(", ; expected
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amandeep Singh:
Christophe,

if i use like this in order

-------------
@AroundInvoke
public Object logMethodEntry(InvocationContext ic)throws Exception{
System.out.println("Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());

return ic.proceed();

@PostConstruct
public void initialize(InvocationContext ic) throws Exception{
System.out.println("@PostConstruct Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
ic.proceed();
}

@PreDestroy
public void cleanUp(InvocationContext ic) throws Exception{
System.out.println("@PreDestroy Entering the method name " + ic.getMethod().getName());
System.out.println("Getting the parameters " + ic.getParameters());
System.out.println("Getting the target " + ic.getTarget());
System.out.println("Getting the context data " + ic.getContextData());
ic.proceed();
}
-------------------------

the complier complains about the following error's


1) public void initialize(InvocationContext ic) throws Exception{

void is an invalid type for the variable initialize

2) public void cleanUp(InvocationContext ic) throws Exception{

Syntax error on token "(", ; expected


I dont see the method close of AroundInvoke. may be you have missed it when you copy to here.

I tested in my end without any issue. post the whole code and full stactrace.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to close the logMethodEntry method.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, i did such a silly mistake, as everytime i copied the logMethodEntry method evrytime, it's curly brace was down over the screen, i forget to copy.

Can you imagine such a silly mistake can be done by a person who has cleared the SCWCD and is doing preparation for the SCBCD.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A certificate can't face to realworld problem. Only experience can

i passed both scjp and wcd without writing any code at all. i wanted do that for bcd as well. but transaction and exception was doubtful, so i had to write some test code.

and mistakes everywhere whether certified or experienced.. so dont worry abt those..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic