KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > ExampleWorkflow


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  * An example workflow
24  *
25  * @version $Revision: $
26  */

27 // START SNIPPET: workflow
28
public class ExampleWorkflow extends Workflow<ExampleWorkflow.Step> {
29     private static final Log log = LogFactory.getLog(ExampleWorkflow.class);
30
31     private int loopCount;
32     private long timeout = 500;
33     private String JavaDoc userEmailAddress;
34
35     public static enum Step {
36         startStep, afterEnteredEmailStep, loopStep, waitForUserInputStep, forkStep,
37         aCompletedStep, abcCompletedStep, stop
38     };
39
40     public ExampleWorkflow() {
41         super(Step.startStep);
42     }
43
44     // Workflow steps
45
// -------------------------------------------------------------------------
46

47     public void startStep() {
48         // lets use an explicit goTo() to tell the workflow
49
// which step to go to next; though we can just return Strings
50
addStep(Step.loopStep);
51     }
52
53     // lets use the return value to specify the next step
54
public Step loopStep() {
55         if (++loopCount > 3) {
56             return Step.waitForUserInputStep;
57         }
58         // lets keep looping
59
return Step.loopStep;
60     }
61
62     public void waitForUserInputStep() {
63         // we are going to park here until a user
64
// enters a valid email address
65
// so lets park the workflow engine
66
}
67
68     public Step afterEnteredEmailStep() {
69         // we are going to park here until a user
70
// enters a valid email address
71
log.info("User entered email address: " + userEmailAddress);
72         return Step.forkStep;
73     }
74
75     public void forkStep() {
76         // lets fork some child flows
77
TimeoutActivity a = new TimeoutActivity();
78         TimeoutActivity b = new TimeoutActivity();
79         TimeoutActivity c = new TimeoutActivity();
80
81         log.info("Forking off processes a, b, c");
82         fork(a, b, c);
83
84         // now lets add some joins
85
joinAll(Step.aCompletedStep, timeout, a);
86         joinAll(Step.abcCompletedStep, timeout, a, b, c);
87     }
88
89     public void aCompletedStep() {
90         log.info("child flow A completed!");
91     }
92
93     public Step abcCompletedStep() {
94         log.info("child flows A, B and C completed!");
95
96         // we are completely done now
97
return Step.stop;
98     }
99
100     // External events
101
// -------------------------------------------------------------------------
102
public void userEntered(String JavaDoc emailAddress) {
103         if (emailAddress != null && emailAddress.indexOf("@") > 0) {
104             this.userEmailAddress = emailAddress;
105
106             log.info("Lets re-start the suspended workflow");
107             addStep(Step.afterEnteredEmailStep);
108         }
109     }
110 }
111 // END SNIPPET: workflow
112
Popular Tags