Results 1 to 6 of 6

Thread: What is an interceptor in Java EE 5?

  1. #1
    Join Date
    Apr 2010
    Posts
    88

    What is an interceptor in Java EE 5?

    I had learned that with Spring so far we can delegate the creation of objects and inject dependencies. I also know that we use interfaces to cast the objects that Spring returns or injected, so as to be able to achieve a real independence of implementations. Now I am having the question that what would happen if, at runtime, they could create classes that intercept calls and perform additional actions? So I want to know more about an interceptor in Java EE 5. I am sure that you guys will help me sooner (like always). Detailed information would be grateful.

  2. #2
    Join Date
    Mar 2008
    Posts
    258

    Re: What is an interceptor in Java EE 5?

    An interceptor is a class that meets certain interface, and its aim is to "intervene" in the implementation of a method. Thus, an interceptor is able to run before a given method, actions, and then continue with normal execution (or abort). This type of concept is known as Aspect-Oriented Programming. An interceptor is useful, for example, the log could be: something that intercepts all calls to let BO and recorded the time of the invocation, the parameters, return values, etc. Creating an interceptor is very simple, although most projects use interceptors already created.

  3. #3
    Join Date
    Apr 2008
    Posts
    193

    Re: What is an interceptor in Java EE 5?

    A transaction is an atomic operation that is, is made in full, or not do anything. For example, a method of a BO to modify two domain objects should be transactional: either modify the method successfully achieves two objects, or fails. You should never get this with an inconsistent state: that is, a state in which the first object is modified, but not the second. In databases this is accomplished by initiating the transaction: Once a transaction, no change is shocked until they are confirmed operations ("commit"), or annul the same ("rollback"). The Java API for handling transactions known as JTA (Java Transaction API). In JEE applications is very unusual to perform commit or rollback explicitly. In contrast, some techniques are often used to "infer" when you have to perform commit or rollback. Transactional methods are usually all the methods of the BO.

  4. #4
    Join Date
    Dec 2008
    Posts
    202

    Re: What is an interceptor in Java EE 5?

    One of the most used interceptors is the transaction interceptor. These interceptors are applied to all the BO, so that "if the method completed successfully, a commit is made of the connection, but a rollback." By default, it is considered that a method successfully complete if you do NOT throw a runtime exception (or inherits it). That is, if the method throws a RuntimeException will be held rollback, but commit. This transaction interceptor is already provided by Spring. You can find the transaction interceptor application-db.xml file:
    • transaction-manager="transactionManager"/> <tx:annotation-driven

    This entry will look transactional annotations in Spring beans and make his methods are transactional.

  5. #5
    Join Date
    Dec 2008
    Posts
    161

    Re: What is an interceptor in Java EE 5?

    There are different types of interceptors that intercept various aspects (exceptions, before a method after method, etc.).. Create an interceptor method is as simple as making a class that inherits from org.aopalliance.intercept.MethodInterceptor. MethodInterceptor interceptor is wider there, and very easy to implement. The following is the log interceptor class instance :
    Code:
    com.dosideas.interceptor package; 
    
      org.aopalliance.intercept.MethodInterceptor import; 
      org.aopalliance.intercept.MethodInvocation import; 
    
      public class implements LogInterceptor MethodInterceptor ( 
      public Object invoke (MethodInvocation invocation) throws Throwable ( 
      System . out. println ("Start called"); 
      Object rval = invocation. proc (); 
      System . out. println ("end call"); 
      return rval; 
      ) 
      )

  6. #6
    Join Date
    Jan 2009
    Posts
    140

    Re: What is an interceptor in Java EE 5?

    To add the interceptor to a whole group of classes could configure:
    Code:
     <aop:config> 
      id = "businessOperation" <aop:pointcut expression = "execution(* com.dosideas.business.*.*(..))" /> 
      <aop:advisor advice-ref = "interceptor.LogInterceptor" pointcut-ref = "businessOperation" /> 
      </ Aop: config> 
    
    
      <bean "interceptor.LogInterceptor" id = class = "com.dosideas.interceptor.LogInterceptor" /> 
    
      <bean "business.PaisBo" id = class = "com.dosideas.business.impl.PaisBoImpl" />
    As we see, we declare a Pointcut that refers to all classes and methods of the business package. Then, declare an Advice , indicating that the Pointcut must be intercepted by LogInterceptor.

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Java Programming using Adventnet SNMP Java API
    By ROCKING_Suhas in forum Software Development
    Replies: 5
    Last Post: 17-07-2010, 06:52 AM
  3. Comparison between core java and advanced java
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 10:57 AM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,538,863.59112 seconds with 17 queries