Skip to content

Instantly share code, notes, and snippets.

@reikje
Created April 20, 2012 13:30
Show Gist options
  • Select an option

  • Save reikje/2428573 to your computer and use it in GitHub Desktop.

Select an option

Save reikje/2428573 to your computer and use it in GitHub Desktop.
Don't let these ThreadLocals escape
/**
* A aspect which has the same functionality as the ServletRequestCleanupFilter. The {@link NonServletRequestCleanupAspect}
* is useful when code is executed which is not wrapped by the servlet filter, i.e. the handling of events in the
* thread pools.
*
* @author reik, 4/19/12
*/
@Aspect
public class NonServletRequestCleanupAspect {
@Pointcut("execution(void package.listeners.PartnerNotificationListener+.onApplicationEvent(..))")
public void onPartnerNotification() { }
@Pointcut("execution(void package.listeners.SaveUserEventListener+.onApplicationEvent(..))")
public void onSaveUserEvent() { }
@Pointcut("onPartnerNotification() || onSaveUserEvent()")
public void onApplicationEventWithDatabaseAccess() { }
@Around("package.NonServletRequestCleanupAspect.onApplicationEventWithDatabaseAccess()")
public Object addCleaner(final ProceedingJoinPoint pjp) throws Throwable {
try {
CleanupService.setCleaner(this);
return pjp.proceed();
} finally {
CleanupService.cleanup(this);
}
}
}
<filter-mapping>
<filter-name>servletRequestCleanupFilter</filter-name>
<url-pattern>/rpc/*</url-pattern>
<url-pattern>/fb/*</url-pattern>
</filter-mapping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment