001    package com.hammurapi.flow.runtime;
002    
003    import java.util.concurrent.TimeUnit;
004    
005    import com.hammurapi.config.bootstrap.ConfigurationException;
006    import com.hammurapi.config.bootstrap.Destroyable;
007    import com.hammurapi.util.concurrent.TaskCounter;
008    
009    /**
010     * Implementations of this interface create invocation interceptors to
011     * parallelize and/or distribute computations.
012     * @author Pavel Vlasov
013     * @param <K> Connect key type.
014     * @param <S> Flow state type.
015     * @param <A> Argument type.
016     */
017    public interface SynapseFactory<K, S, A> {
018            
019            /**
020             * Synapse config to parameterize synapse.
021             * @author Pavel Vlasov
022             *
023             */
024            interface SynapseConfig {
025                    
026                    /**
027                     * @return Delay before invocation execution. Non-positive value
028                     * means no delay. 
029                     */
030                    long getDelay();
031                    
032                    /**
033                     * @return Period for repeated executions. Non-positive value means
034                     * no delay.
035                     */
036                    long getPeriod();
037                    
038                    /**
039                     * @return Time unit for delay and interval.
040                     */
041                    TimeUnit getTimeUnit();
042                    
043                    /**
044                     * If this method returns true, invocation shall occur in the same thread.
045                     * @return
046                     */
047                    boolean isInline();
048                    
049            }
050    
051            /**
052             * Default synapse config - no delay, no repeat, execute asynchronously.
053             */
054            SynapseConfig DEFAULT_SYNAPSE_CONFIG = new SynapseConfig() {
055    
056                    @Override
057                    public long getDelay() {
058                            return 0;
059                    }
060    
061                    @Override
062                    public long getPeriod() {
063                            return 0;
064                    }
065                    
066                    public TimeUnit getTimeUnit() {
067                            return TimeUnit.MILLISECONDS;
068                    }
069    
070                    @Override
071                    public boolean isInline() {
072                            return false;
073                    };
074                    
075            };
076            
077            /**
078             * Connects invoker to invocable.
079             * @param invoker Invoker.
080             * @param invocable Invocable.
081             * @param config Synapse configuration. If null, then DEFAULT_SYNAPSE_CONFIG is used.
082             * @param connectKey Connect key.
083             * @param processingPathElement Processing path element for the invocable.
084             * @param taskCounter Task counter is optionally provided for tracking when all tasks in the flow finish execution.
085             * @return Destroyable to use during flow shutdown for resource cleanup.
086             */
087            Destroyable connect(Invoker<S, A> invoker, Invocable<S, A> invocable, SynapseConfig config, K connectKey, ProcessingPathElement processingPathElement, TaskCounter taskCounter) throws ConfigurationException;
088    
089    }