SyntaxHighlighter

Showing posts with label Aspects. Show all posts
Showing posts with label Aspects. Show all posts

Wednesday, April 24, 2013

Spring Security - logging the Authentication Object Aspect

Spring Security - logging the user Aspect

here's one inspired by StackOverflow(http://stackoverflow.com/questions/16186087/capture-successful-login-with-aspectj-and-spring-security/16187405)

to log the results of an authenticationManagers.authenticate when using in a web context, you need to use CGLIB.  here's an example;

here's the XML



 
  
 
 
 
  
   
    
   
  
 

 

 



and here's the aspect

package de.incompleteco.spring.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.core.Authentication;

@Aspect
public class AuthenticationManagerAspect {

 @AfterReturning(pointcut="execution(* org.springframework.security.authentication.AuthenticationManager.authenticate(..))"
   ,returning="result")
 public void after(JoinPoint joinPoint,Object result) throws Throwable {
  System.out.println(">>> user: " + ((Authentication) result).getName());
 }
 
}


Thursday, March 28, 2013

Spring Integration - Intercepting to Raise Events

Spring Integration - Intercepting to Raise Events

in Spring Integration, it is possible to use it to raise events to external systems and leverage all the existing components.  to do this, we can leverage the publishing-interceptor, a component in Spring Integration that uses AOP to capture a method and send off information about it.

the neat thing about this is that you can add a Message Driven Architecture layer to your existing application, allowing secondary or tertiary events to be raised from existing Spring Applications (such as audit).  this can be done either with annotations (@Publisher) or through xml

here's the link to the documentation for it

http://static.springsource.org/spring-integration/reference/html/message-publishing.html

Friday, June 22, 2012

Thread Pool Issues and JobLauncher

Clustered Spring Batch and Thread Pools

Scenario

in a scenario where multiple instances of a batch job are distributed in a cluster, or even multiple threads in a single instance may be able to run a job, we need some way of indicating that a given instance is 'tapped out' of resources and can't run a job.  The JobLauncher interface currently provides four possible exceptions, but doesn't throw an exception for when the thread pool is done.

to enable a given cluster node to reject a job start request, allowing another node to process it instead, we need to manage a TaskRejectedException.  in the current SimpleJobLauncher class in org.springframework.batch.launch.support, this exception is caught, logged, and the job is marked as having immediately failed.   this can be seen as correct behavior (the job failed because it couldn't start), but how do we note that in such a way that we can 'load balance' the job start to another node?

One Solution


one solution would be to intercept the return of the job failure and throw an exception as a consequence, allowing the job start request to be handled as a 'failed to process the request' and then load balanced to another node.  the components of this are as follows;

- around aspect tied to org.springframework.batch.launch.JobLauncher.run
- aspect intercepts the returned JobExecution object and verifies for status
- if the status is 'failed', checks the exit status description fo the TaskRejectedException
- if it's there, throws the TaskRejectedException up to the caller

when coupled with Spring Integration, this allows for the message transaction to be rolled back to the adapter (e.g. JMS) causing the original request to be placed back on the queue for consumption by either another node, or the same node at a later interval.

Considerations

we may see a handler built into the Spring Batch Framework in subsequent releases, hence the aspect approach would allow for us to 'unwind' this change in a subsequent upgrade.  personally, this change is unlikely to be included except for a major release due to changing the signature on the JobLauncher interface.

Tuesday, June 19, 2012

Spring Batch Pause (Part 1)

Spring Batch Pause - Not Implemented (yet!) 2.1.8

the documentation for Spring Batch 2.1.8 makes a couple of references to the 'pause' state.  However, pause is not implemented yet.  the use case is a bit of departure from the existing Spring Batch structure in that instead of creating a new Job Execution for the Job Instance, it needs to resume the existing Job Execution.  This requires a new or additional Job Launcher and some logic to 'fast forward' through the steps.

The solution I've put together so far uses the following;
- LTW to wrap the existing shouldStart private method allowing the logic to 'fast forward' through the steps --> defaults to 'false' if the step execution has already completed
- simple 'around' aspect to monitor 'pausing' of a step/job and 'stop' a job if it hits a 'paused' step
- independent persistence of step/job execution pausing implemented in the batch tables

more soon as the solution develops