Results 1 to 4 of 4

Thread: Help regarding making an AOP programming thread safe

  1. #1
    Join Date
    Jul 2006
    Posts
    6

    Default Help regarding making an AOP programming thread safe

    Hi,

    I am a newbee in the Spring concept. I wanted to create an AOP programming using Spring's AOP concept. I created the bean reference as org.springframework.aop,framework.ProxyFactoryBean .

    The example is working perfectly. NiwI want to make my "before" method thread safe, so that multiple users can access my application by invoking my "before" method safely.

    Google uncle says it can be done by using Cglib2AopProxy. But cannot understand what changes are to be made if I use the Cglib2AopProxy instead of ProxyFactoryBean:

    a) What more changes are to be made apart from this?
    b) How do I know if my application has been thread safe by using Cglib2AopProxy?

    TIA,
    Samik

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    I want to make my "before" method thread safe
    Can you post the code. Is this method storing state? If not, you don't need to do anything. If so, you make it thread safe by specifying certain regions of code as synchronized - to prevent multiple threads entering where only one thread should.

    Google uncle says it (thread safety) can be done by using Cglib2AopProxy.
    Have you got a link to that? CGLIB just allows you to proxy objects that don't implement an interface.

    what changes are to be made if I use the Cglib2AopProxy instead of ProxyFactoryBean
    Again, not sure this is what you want, but you add the CGLIB JAR to your classpath and specify a target that doesn't implement an interface, or set setProxyTargetClass to true.

    How do I know if my application has been thread safe by using Cglib2AopProxy?
    This isn't what CGLIB does. See 2nd comment above.

    How do I know if my application has been thread safe
    See first comment above. Thread safety can be difficult to test, and is probably best ensured by code inspection.

  3. #3
    Join Date
    Jul 2006
    Posts
    6

    Default Thread safe using AOP

    Hi,

    Posting some sample code ...

    In the configuration file, I wrote as following:

    =============================================
    <bean id="somedaoimpl" class="com.netg.referenceware.dao.impl.hibernate.S omeDaoImpl" parent="HibernateDAOSupport" />
    <bean id="refware.dao.someDao" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces"> <value>com.netg.referenceware.dao.SomeDao</value>
    </property>
    <property name="target">
    <ref local="somedaoimpl"/>
    </property>
    <property name="interceptorNames">
    <value>theAuditBeforeAdvisor</value>
    </property>
    </bean>
    <bean id="theAuditBeforeAdvisor" class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice">
    <ref local ="theAuditBeforeAdvice"/>
    </property>
    <property name="patterns">
    <list>
    <value>.*save.*</value>
    <value>.*update.*</value>
    </list>
    </property>
    </bean>
    <bean id="theAuditBeforeAdvice" class="com.netg.referenceware.dao.impl.hibernate.A uditBeforeAdvice">
    </bean>
    ============================================

    In the Before Advice method, I wrote as follows ...

    ===================================
    public class AuditBeforeAdvice implements MethodBeforeAdvice {
    private AuditRecordImpl audit = new AuditRecordImpl();

    public synchronized void before(final Method m, final Object[] arg, final Object target) throws Exception {
    //check for each object
    for (int iCounter = 0; iCounter < arg.length; iCounter++) {
    Object lObject = (Object) arg[iCounter];
    // check if the object implements the Auditable interface
    if (lObject instanceof Auditable) {

    //Do some business logic
    }
    } //end of for loop
    }
    }
    ==============================================

    I just used the synchronized keyword, but the client seems unhappy with this solution. By the way, this AOP is working perfectly so far.

    Is there any other specific way to make an AOP programming thread safe?

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    OK, so both you service and advice are singletons. Your advice doesn't maintain any state, so there is no threading issue - unless "//Do some business logic" needs to be sequential (e.g. if method calls within here should not be interleaved between threads - but your synchronized keyword will prevent that).

    Code:
    private AuditRecordImpl audit = new AuditRecordImpl();
    Just a note, you can use DI to set this.

    I just used the synchronized keyword, but the client seems unhappy with this solution
    Is it a concern with performance. If so you should set up some performance tests to prove that whatever solution you end up with is OK (and help demonstrate that the advice is behaving/thread-safe as expected).

    Is there any other specific way to make an AOP programming thread safe?
    You can try pooling (see reference documentation), but usually this isn't necessary for stateless objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •