View Javadoc

1   /*
2    * $Id: QueueProfile.java 22175 2011-06-14 13:54:58Z dirk.olmes $
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.config;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.context.MuleContextAware;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.store.ListableObjectStore;
18  import org.mule.util.queue.QueueConfiguration;
19  import org.mule.util.queue.QueueManager;
20  
21  import java.io.Serializable;
22  
23  /**
24   * <code>QueueProfile</code> determines how an internal queue for a service will
25   * behave
26   */
27  
28  public class QueueProfile
29  {
30      private int maxOutstandingMessages = 0;
31      private ListableObjectStore<Serializable> objectStore;
32      
33      public static QueueProfile newInstancePersistingToDefaultMemoryQueueStore(MuleContext muleContext)
34      {
35          ListableObjectStore<Serializable> defaultMemoryObjectStore =
36              muleContext.getRegistry().lookupObject(MuleProperties.OBJECT_STORE_DEFAULT_IN_MEMORY_NAME);
37          return new QueueProfile(defaultMemoryObjectStore);
38      }
39  
40      public QueueProfile(ListableObjectStore<Serializable> objectStore)
41      {
42          this.objectStore = objectStore;
43      }
44  
45      // TODO DO: is this ctor required at all? It's not used anywhere in the code base
46      public QueueProfile(QueueProfile queueProfile)
47      {
48          this.maxOutstandingMessages = queueProfile.getMaxOutstandingMessages();
49          this.objectStore = queueProfile.objectStore;
50      }
51  
52      public QueueProfile(int maxOutstandingMessages, ListableObjectStore<Serializable> objectStore)
53      {
54          this.maxOutstandingMessages = maxOutstandingMessages;
55          this.objectStore = objectStore;
56      }
57      
58      /**
59       * This specifies the number of messages that can be queued before it starts
60       * blocking.
61       * 
62       * @return the max number of messages that will be queued
63       */
64      public int getMaxOutstandingMessages()
65      {
66          return maxOutstandingMessages;
67      }
68  
69      /**
70       * This specifies the number of messages that can be queued before it starts
71       * blocking.
72       * 
73       * @param maxOutstandingMessages the max number of messages that will be queued
74       */
75      public void setMaxOutstandingMessages(int maxOutstandingMessages)
76      {
77          this.maxOutstandingMessages = maxOutstandingMessages;
78      }
79  
80      public QueueConfiguration configureQueue(MuleContext context, String component, QueueManager queueManager) throws InitialisationException
81      {
82          if (objectStore instanceof MuleContextAware)
83          {
84              ((MuleContextAware) objectStore).setMuleContext(context);
85          }
86          QueueConfiguration qc = new QueueConfiguration(context, maxOutstandingMessages, objectStore);
87          queueManager.setQueueConfiguration(component, qc);
88          return qc;
89      }
90  
91      public ListableObjectStore<Serializable> getObjectStore()
92      {
93          return objectStore;
94      }
95  
96      public void setQueueStore(ListableObjectStore<Serializable> objectStore)
97      {
98          this.objectStore = objectStore;
99      }
100 
101     public void addQueueStore(ListableObjectStore<Serializable> objectStore)
102     {
103         this.objectStore = objectStore;
104     }
105 
106     @Override
107     public String toString()
108     {
109         return "QueueProfile{maxOutstandingMessage=" + maxOutstandingMessages + ", storeType="
110                + objectStore.getClass() + "}";
111     }
112 }