Date: Fri, 29 Mar 2024 04:59:33 -0700 (PDT) Message-ID: <1209890598.469.1711713573361@aries> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_468_696369544.1711713573361" ------=_Part_468_696369544.1711713573361 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
When you desire to start and stop a strategy from within another= strategy, follow these steps. You may wish to do this to automatically con= trol the lifecycle of a strategy.
Create a command file as normal that creates (but does not start) al= l your strategies: master and slave (or slaves, this sample shows only one,= but there could be more)
# # Create the two strategies # createModule;metc:strategy:system;slave,Slave,JAVA,src/Slave.java,,true,met= c:remote:receiver # createModule;metc:strategy:system;master,Master,JAVA,src/Master.java,,true,= metc:remote:receiver # # start Master only from Photon (may instead start here, if you'd like by u= n-commenting the next line) # #startModule;metc:strategy:system:master
From the Master strategy, when the time is right, start the slave st=
rategy. In this sample, look at onCallback
for where the =
slave is started and stopped.
import org.marketcetera.module.ModuleException; import org.marketcetera.module.ModuleManager; import org.marketcetera.module.ModuleURN; import org.marketcetera.strategy.StrategyModuleFactory; import org.marketcetera.strategy.java.Strategy; /* $License$ */ /** * Controls starting and stopping another strategy. * * @author <a href=3D"mailto:colin@marketcetera.com">Colin DuPlantis&= lt;/a> * @version $Id$ * @since $Release$ */ public class Master extends Strategy { /* (non-Javadoc) * @see org.marketcetera.strategy.java.Strategy#onStart() */ @Override public void onStart() { moduleManager =3D ModuleManager.getInstance(); strategyURN =3D new ModuleURN(StrategyModuleFactory.PROVIDER_URN, "slave"); warn("Master strategy started"); requestCallbackAfter(10000, true); } /* (non-Javadoc) * @see org.marketcetera.strategy.java.Strategy#onCallback(java.lang.Ob= ject) */ @Override public void onCallback(Object inData) { boolean startFlag =3D (Boolean)inData; try { if(startFlag) { warn("Starting slave strategy"); moduleManager.start(strategyURN); requestCallbackAfter(10000, false); } else { warn("Stopping slave strategy"); moduleManager.stop(strategyURN); } } catch (ModuleException e) { throw new RuntimeException(e); } } /** * handle to module manager instance */ private ModuleManager moduleManager; /** * handle to strategy to start/stop */ private ModuleURN strategyURN; }
Dynamically Creating the Strategy
<= span class=3D"aui-icon aui-icon-small aui-iconfont-info confluence-informat= ion-macro-icon">You can create the slave strategy completely from within Master rather t= han the command file, if desired. This would allow you to dynamically creat= e, start, stop, and destroy slave strategy instances
Inter-Strategy Communication
You can communicate between strategies with the common key/value propert=
ies available from getProperties
. All strategies in the same p=
rocess share this space.