Class SimpleAsyncProcessor
- java.lang.Object
-
- org.marketcetera.module.Module
-
- org.marketcetera.modules.async.SimpleAsyncProcessor
-
- All Implemented Interfaces:
DynamicMBean,DataEmitter,DataReceiver
public class SimpleAsyncProcessor extends Module implements DataEmitter, DataReceiver, DynamicMBean
A processor module that can be inserted between any two modules within a data flow to decouple the delivery of data from the emitter module and processing of that data in the receiver module.For each data flow this module is participating in, the module adds every received data item into a queue. Hence the emitter module from which this module is receiving data is not blocked when delivering data. The module creates a separate thread for the data flow that removes the data items from this queue and delivers it to the next receiver module in the data flow.
Do note that the if the module that is receiving data emitted by this module is not able to keep up with the module that is emittin data into this module, the data items will keep on accumulating in the queue. If this condition continues on for a while, this may lead to JVM running out of memory. When using this module, be sure to size the heap of the process to ensure that the process does not run out of memory, when there's a disparity in the data emit rate and data receive rate of the modules before and after this module in the data flow.
The module exposes an DynamicMBean interface to monitor the current queue sizes for every flow. The MBean offers list of attributes, one per data flow, that it is participating in. Each attribute has name of the form "
Flowdata_flow_id" where data_flow_id is the flowID of the data flow that the attribute represents. The value of the attribute is the current size of the queue for that data flow. The attribute value can be monitored to observe if the module receiving data from this module is able to keep up with the module emitting data into this module in the data flow represented by the attribute.Note that when the data flow is canceled, the module does not wait for the all the data in the queue to be delivered, it interrupts the delivery thread right away and allows the flow to be canceled.
Note that this module is not designed such that the same instance can be used more than once in a single data flow. If you need to use this module more than once in the same data flow make sure that you use different instances of this module in the flow such that the same instance doesn't appear more than once in the same data flow.
This module is meant to be used as an intermediate module between two modules in a data flow. Although this module can be used as the first or the last module in a data flow, the features provided by this module offer little merit in such a scenario.
Usage:
The following strategy agent command demonstrates how this module can be inserted between two modules to decouple the data flow between them.
# Start the bogus feed module startModule;metc:mdata:bogus:single # Create data flow createDataFlow;metc:mdata:bogus;single:symbols=AAPL:content=latest_tick,top_of_book^metc:async:simple:myinstance
The command above will create a data flow where the market data events from the bogus feed are fed into the simple async module. The async module adds all the received market data events to the queue and has a separate thread that removes them from the queue and delivers them to the sink module. Since the market data events are always added to the queue, market data delivery is very fast and the market data delivery thread is not blocked while the sink module processes the event (for example, logs it).Module Features
Describes the module attributes Capabilities Data Emitter, Data Reciever DataFlow Request Parameters None. Stops data flows No. Start Operation Initializes the thread pool for emitting data. Stop Operation Shuts down the thread pool. Management Interface dynamic: see above Factory SimpleAsyncProcessorFactory- Since:
- 2.0.0
- Version:
- $Id$
- Author:
- anshul@marketcetera.com
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSimpleAsyncProcessor.DataFlowHandlerInstances of this class keep track of the queue of data items accumulated for the data flow and the thread that is responsible for processing those items.
-
Field Summary
Fields Modifier and Type Field Description (package private) static StringASYNC_THREAD_NAME_PREFIXName prefix for all threads created by this module.(package private) static StringATTRIB_PREFIXThe name prefix for JMX attribute used to communicate the queue sizes for each data flow.private Map<DataFlowID,SimpleAsyncProcessor.DataFlowHandler>mFlowsThe map of data flows and their handlers.private ExecutorServicemServiceThe thread pool used for creating threads to publish received data asynchronously.
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleAsyncProcessor(ModuleURN inURN)Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddFlow(DataEmitterSupport inSupport, SimpleAsyncProcessor.DataFlowHandler inFlowHandler)Adds the flow handler for the data flow the table of requests and flows.voidcancel(DataFlowID inFlowID, RequestID inRequestID)ObjectgetAttribute(String attribute)AttributeListgetAttributes(String[] attributes)private SimpleAsyncProcessor.DataFlowHandlergetHandler(DataFlowID inFlowID)Fetches the flow handler for the supplied flowID.MBeanInfogetMBeanInfo()private Map<DataFlowID,Integer>getQueueSizes()Returns the queue sizes for all the data flows that this module is currently participating in.Objectinvoke(String actionName, Object[] params, String[] signature)protected voidpreStart()protected voidpreStop()voidreceiveData(DataFlowID inFlowID, Object inData)private Future<?>removeFlow(DataFlowID inFlowID)Removes the flow from the table of flows.voidrequestData(DataRequest inRequest, DataEmitterSupport inSupport)voidsetAttribute(Attribute attribute)AttributeListsetAttributes(AttributeList attributes)-
Methods inherited from class org.marketcetera.module.Module
getCreated, getLastStartFailure, getLastStopFailure, getStarted, getState, getStopped, getURN, isAutoCreated, isAutoStart
-
-
-
-
Field Detail
-
ATTRIB_PREFIX
static final String ATTRIB_PREFIX
The name prefix for JMX attribute used to communicate the queue sizes for each data flow.- See Also:
- Constant Field Values
-
ASYNC_THREAD_NAME_PREFIX
static final String ASYNC_THREAD_NAME_PREFIX
Name prefix for all threads created by this module.- See Also:
- Constant Field Values
-
mService
private ExecutorService mService
The thread pool used for creating threads to publish received data asynchronously.
-
mFlows
private final Map<DataFlowID,SimpleAsyncProcessor.DataFlowHandler> mFlows
The map of data flows and their handlers.
-
-
Constructor Detail
-
SimpleAsyncProcessor
protected SimpleAsyncProcessor(ModuleURN inURN)
Creates an instance.- Parameters:
inURN- the module's instance URN.
-
-
Method Detail
-
requestData
public void requestData(DataRequest inRequest, DataEmitterSupport inSupport) throws IllegalRequestParameterValue
- Specified by:
requestDatain interfaceDataEmitter- Throws:
IllegalRequestParameterValue
-
cancel
public void cancel(DataFlowID inFlowID, RequestID inRequestID)
- Specified by:
cancelin interfaceDataEmitter
-
receiveData
public void receiveData(DataFlowID inFlowID, Object inData)
- Specified by:
receiveDatain interfaceDataReceiver
-
getAttribute
public Object getAttribute(String attribute) throws AttributeNotFoundException
- Specified by:
getAttributein interfaceDynamicMBean- Throws:
AttributeNotFoundException
-
setAttribute
public void setAttribute(Attribute attribute) throws AttributeNotFoundException
- Specified by:
setAttributein interfaceDynamicMBean- Throws:
AttributeNotFoundException
-
getAttributes
public AttributeList getAttributes(String[] attributes)
- Specified by:
getAttributesin interfaceDynamicMBean
-
setAttributes
public AttributeList setAttributes(AttributeList attributes)
- Specified by:
setAttributesin interfaceDynamicMBean
-
invoke
public Object invoke(String actionName, Object[] params, String[] signature) throws ReflectionException
- Specified by:
invokein interfaceDynamicMBean- Throws:
ReflectionException
-
getMBeanInfo
public MBeanInfo getMBeanInfo()
- Specified by:
getMBeanInfoin interfaceDynamicMBean
-
addFlow
private void addFlow(DataEmitterSupport inSupport, SimpleAsyncProcessor.DataFlowHandler inFlowHandler)
Adds the flow handler for the data flow the table of requests and flows.- Parameters:
inSupport- the data flow support instance for the flow.inFlowHandler- the flow handler for the flow.
-
removeFlow
private Future<?> removeFlow(DataFlowID inFlowID)
Removes the flow from the table of flows.- Parameters:
inFlowID- the flowID of the flow being canceled.- Returns:
- the future value representing the thread delivering the data for the flow.
-
getHandler
private SimpleAsyncProcessor.DataFlowHandler getHandler(DataFlowID inFlowID)
Fetches the flow handler for the supplied flowID.- Parameters:
inFlowID- the data flowID.- Returns:
- the handler for the supplied flowID. Null, if no handler was found for the specified flowID.
-
getQueueSizes
private Map<DataFlowID,Integer> getQueueSizes()
Returns the queue sizes for all the data flows that this module is currently participating in.- Returns:
- a map of queue sizes for all the flows.
-
-