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