View Javadoc

1   /*
2    * $Id: CountingRedeliveryHandlerFactory.java 19191 2010-08-25 21:05:23Z 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;
12  
13  import java.util.concurrent.atomic.AtomicReference;
14  
15  /**
16   * @see CountingRedeliveryHandler
17   */
18  public class CountingRedeliveryHandlerFactory implements RedeliveryHandlerFactory
19  {
20  
21      protected AtomicReference<RedeliveryHandler> handler = new AtomicReference<RedeliveryHandler>(null);
22  
23      public RedeliveryHandler create()
24      {
25          RedeliveryHandler result;
26  
27          // initialize, accounting for concurrency
28          if (handler.get() == null)
29          {
30              final CountingRedeliveryHandler newInstance = new CountingRedeliveryHandler();
31              boolean ok = handler.compareAndSet(null, newInstance);
32              if (!ok)
33              {
34                  // someone was faster to initialize it, use this ref instead
35                  result = handler.get();
36              }
37              else
38              {
39                  result = newInstance;
40              }
41          }
42          else
43          {
44              // just re-use existing stateful handler
45              result = handler.get();
46          }
47  
48          return result;
49      }
50  
51  }
52  
53