6 Replies Latest reply on Dec 14, 2009 9:41 PM by Mark Wigmans

    How to change Synchronized.DEFAULT_TIMEOUT without recompilation

    Francisco Jose Peredo Noguez Master

      I think the subject says it all:

      How to change Synchronized.DEFAULT_TIMEOUT without recompilation?

      Someone decided that 1 second was good enought as the default value for Synchronized.DEFAULT_TIMEOUT
      and hardcoded that in to Seam.

      The needs of my application disagree, for my application X is better, the value of X is not relevant, what is relevant is that I want to play with this value globally, and I have more than 500 components in my system and I do not want to have to modify open each and everyone of this files and modify it to be able to find the best value for X.

      Any suggestions on what should can I do? (Please do not suggest to open and modify each and every .java file in my project)


        • 1. Re: How to change Synchronized.DEFAULT_TIMEOUT without recompilation
          Francisco Jose Peredo Noguez Master

          Seems like the only way to do it would be to replace org.jboss.seam.core.SynchronizationInterceptor with my own custom implementation that reads the value from components.xml.

          But since SynchronizationInterceptor is a core part of Seam I do not feel comfortable doing this...

          • 2. Re: How to change Synchronized.DEFAULT_TIMEOUT without recompilation
            Tim Evers Master

            You could try reflectively setting the value of Component.timeout at startup time or creation of a component. So intercept the creation of a component and reflectively set it's timeout value.

            Don't have time to investigate if this will be enough to get you going, but it might be.

            Based on the line in the SynchronizationInterceptor

            if ( lock.tryLock( getComponent().getTimeout(), TimeUnit.MILLISECONDS ) )

            All you need to do is make sure the call to getComponent.getTimeout() returns your X value.

            Anyways, just an idea.

            • 3. Re: How to change Synchronized.DEFAULT_TIMEOUT without recompilation
              Francisco Jose Peredo Noguez Master

              Finally found the place where the default interceptors are defined org.jboss.seam.core.Init:

              public static List<String> DEFAULT_INTERCEPTORS = new ArrayList<String>(Arrays.asList(

              This list (and the reasons for this particular order) should be part of Seam documentation but I could not find it.

              So now I am going to use:

              <core:init debug="true">

              To replace SynchronizationInterceptor with my own more flexible ConfigurableSynchronizationInterceptor class...

              Hope it works...

              • 4. Re: How to change Synchronized.DEFAULT_TIMEOUT without recompilation
                Francisco Jose Peredo Noguez Master

                Here is how, first, adjust your list of interceptors in components.xml (remove SynchronizationInterceptor, add your custom ConfigurableSynchronizationInterceptor):

                <core:init debug="true">
                   <!-- <value>org.jboss.seam.core.SynchronizationInterceptor</value>-->

                Write your own interceptor with configurable timeout:

                public class ConfigurableSynchronizationInterceptor extends AbstractInterceptor
                   private ReentrantLock lock = new ReentrantLock(true);
                   public Object aroundInvoke(InvocationContext invocation) throws Exception
                        SynchronizationConfiguration cfg=(SynchronizationConfiguration) Component.getInstance("synchronizationConfiguration");
                        if ( lock.tryLock( cfg.getSynchronizationTimeOut(), TimeUnit.MILLISECONDS ) )
                            return invocation.proceed();
                         throw new LockTimeoutException("could not acquire lock on @Synchronized component: " + 
                   public boolean isInterceptorEnabled()
                      return getComponent().isSynchronize();

                Create a class that you will use to set and store the configurable timeout value:

                public class SynchronizationConfiguration {
                     private long synchronizationTimeOut;
                     public long getSynchronizationTimeOut() {
                          return synchronizationTimeOut;
                     public void setSynchronizationTimeOut(long synchronizationTimeOut) {
                          this.synchronizationTimeOut = synchronizationTimeOut;

                Configure the timeout value in components.xml:

                  name="synchronizationConfiguration" scope="application">
                  <property name="synchronizationTimeOut">60000</property>

                And that is it, now you can change the synchronizationTimeOut for ALL the components in your application in a single place, without any recompilation.

                IMO this should be part of Seam core... why it was not implemented like this since the beginning?...

                • 6. Re: How to change Synchronized.DEFAULT_TIMEOUT without recompilation
                  Mark Wigmans Newbie
                  Greate code, just what I needed. I think that if explicit a timeout is given, you can this that as well:

                  final Method method = invocation.getMethod();
                  final Synchronized annotation = method.getDeclaringClass().getAnnotation(Synchronized.class);
                  long timeout = annotation != null ? annotation.timeout() : cfg.getSynchronizationTimeOut();
                  if (lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {