View Javadoc

1   /*
2    * $Id: CountingRedeliveryHandlerFactory.java 20501 2010-12-08 00:52:19Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms.redelivery;
12  
13  
14  import java.util.concurrent.atomic.AtomicReference;
15  
16  /**
17   * @see CountingRedeliveryHandler
18   */
19  public class CountingRedeliveryHandlerFactory implements RedeliveryHandlerFactory
20  {
21  
22      protected AtomicReference<RedeliveryHandler> handler = new AtomicReference<RedeliveryHandler>(null);
23  
24      public RedeliveryHandler create()
25      {
26          RedeliveryHandler result;
27  
28          // initialize, accounting for concurrency
29          if (handler.get() == null)
30          {
31              final CountingRedeliveryHandler newInstance = new CountingRedeliveryHandler();
32              boolean ok = handler.compareAndSet(null, newInstance);
33              if (!ok)
34              {
35                  // someone was faster to initialize it, use this ref instead
36                  result = handler.get();
37              }
38              else
39              {
40                  result = newInstance;
41              }
42          }
43          else
44          {
45              // just re-use existing stateful handler
46              result = handler.get();
47          }
48  
49          return result;
50      }
51  
52  }
53  
54