001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.jbi.nmr.flow.seda;
018
019 import java.util.concurrent.atomic.AtomicBoolean;
020
021 import javax.jbi.JBIException;
022 import javax.jbi.messaging.MessagingException;
023 import javax.management.JMException;
024 import javax.management.MBeanAttributeInfo;
025 import javax.management.ObjectName;
026
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.apache.servicemix.executors.Executor;
030 import org.apache.servicemix.jbi.framework.ComponentNameSpace;
031 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
032 import org.apache.servicemix.jbi.management.BaseLifeCycle;
033 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
034
035 /**
036 * A simple Straight through flow
037 *
038 * @version $Revision: 564607 $
039 */
040 public class SedaQueue extends BaseLifeCycle {
041
042 private static final Log LOG = LogFactory.getLog(SedaQueue.class);
043
044 protected SedaFlow flow;
045 protected ComponentNameSpace name;
046 protected AtomicBoolean started = new AtomicBoolean(false);
047 protected AtomicBoolean running = new AtomicBoolean(false);
048 protected ObjectName objectName;
049 protected String subType;
050 protected Thread thread;
051 protected Executor executor;
052
053 /**
054 * SedaQueue name
055 *
056 * @param name
057 */
058 public SedaQueue(ComponentNameSpace name) {
059 this.name = name;
060 }
061
062 /**
063 * Get the name
064 *
065 * @return name
066 */
067 public String getName() {
068 return name.getName();
069 }
070
071 public String getType() {
072 return "SedaQueue";
073 }
074
075 /**
076 * @return Return the name
077 */
078 public ComponentNameSpace getComponentNameSpace() {
079 return this.name;
080 }
081
082 /**
083 * Get the description
084 *
085 * @return description
086 */
087 public String getDescription() {
088 return "bounded worker Queue for the NMR";
089 }
090
091 /**
092 * Initialize the Region
093 *
094 * @param seda
095 */
096 public void init(SedaFlow seda) {
097 this.flow = seda;
098 }
099
100 /**
101 * @return the capacity of the Queue
102 */
103 public int getCapacity() {
104 if (executor == null) {
105 return -1;
106 }
107 return this.executor.capacity();
108 }
109
110 /**
111 * @return size of the Queue
112 */
113 public int getSize() {
114 if (executor == null) {
115 return -1;
116 }
117 return this.executor.size();
118 }
119
120 /**
121 * Enqueue a Packet for processing
122 *
123 * @param packet
124 * @throws InterruptedException
125 * @throws MessagingException
126 */
127 public void enqueue(final MessageExchangeImpl me) throws InterruptedException, MessagingException {
128 executor.execute(new Runnable() {
129 public void run() {
130 try {
131 if (LOG.isDebugEnabled()) {
132 LOG.debug(this + " dequeued exchange: " + me);
133 }
134 flow.doRouting(me);
135 } catch (Throwable e) {
136 LOG.error(this + " got error processing " + me, e);
137 }
138 }
139 });
140 }
141
142 /**
143 * start processing
144 *
145 * @throws JBIException
146 */
147 public void start() throws JBIException {
148 this.executor = flow.getExecutorFactory().createExecutor("flow.seda." + getName());
149 super.start();
150 }
151
152 /**
153 * stop processing
154 *
155 * @throws JBIException
156 */
157 public void stop() throws JBIException {
158 super.stop();
159 this.executor.shutdown();
160 }
161
162 /**
163 * shutDown the Queue
164 *
165 * @throws JBIException
166 */
167 public void shutDown() throws JBIException {
168 stop();
169 super.shutDown();
170 }
171
172 /**
173 * @return pretty print
174 */
175 public String toString() {
176 return "SedaQueue{" + name + "}";
177 }
178
179 /**
180 * Get an array of MBeanAttributeInfo
181 *
182 * @return array of AttributeInfos
183 * @throws JMException
184 */
185 public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
186 AttributeInfoHelper helper = new AttributeInfoHelper();
187 helper.addAttribute(getObjectToManage(), "capacity", "The capacity of the SedaQueue");
188 helper.addAttribute(getObjectToManage(), "size", "The size (depth) of the SedaQueue");
189 return AttributeInfoHelper.join(super.getAttributeInfos(), helper.getAttributeInfos());
190 }
191
192 /**
193 * @return Returns the objectName.
194 */
195 public ObjectName getObjectName() {
196 return objectName;
197 }
198
199 /**
200 * @param objectName The objectName to set.
201 */
202 public void setObjectName(ObjectName objectName) {
203 this.objectName = objectName;
204 }
205
206 }