Meaningless Drivel is fun!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes AroundInvoke Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "AroundInvoke" Watch "AroundInvoke" New topic
Author

AroundInvoke

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
yes, i am using an Eclipse IDE.

Christophe how you do normally run in your code in IDE or where ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jul 17, 2008
Posts: 844
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

Joined: May 17, 2006
Posts: 402
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You forgot to close the logMethodEntry method.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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

Joined: May 17, 2006
Posts: 402
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..
 
GeeCON Prague 2014
 
subject: AroundInvoke