KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > util > ParallelBean


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.util;
18
19 import org.apache.servicemix.beanflow.JoinAll;
20 import org.apache.servicemix.beanflow.JoinSupport;
21 import org.apache.servicemix.beanflow.ParallelActivity;
22 import org.apache.servicemix.beanflow.annotations.Parallel;
23
24 import java.util.concurrent.Executor JavaDoc;
25 import java.util.concurrent.Executors JavaDoc;
26
27 /**
28  * Represents a POJO which contains {@link Parallel} annotations to specify the
29  * methods to be executed concurrently. Each parallel thread can then
30  * synchronize with the group using the {@link #sync()} or {@link #sync(long)}
31  * methods
32  *
33  * @version $Revision: $
34  * @param <T>
35  */

36 public abstract class ParallelBean {
37
38     private ParallelActivity activity;
39     private Executor JavaDoc executor;
40     private JoinSupport join;
41     private int maxThreadPoolSize = 20;
42
43     public ParallelBean() {
44     }
45
46     public ParallelBean(Executor JavaDoc executor) {
47         this.executor = executor;
48     }
49
50     public ParallelBean(Executor JavaDoc executor, JoinSupport join) {
51         this.executor = executor;
52         this.join = join;
53     }
54
55     @SuppressWarnings JavaDoc("unchecked")
56     public void start() {
57         getActivity().start();
58     }
59
60     @SuppressWarnings JavaDoc("unchecked")
61     public void sync() {
62         getActivity().sync();
63     }
64
65     public boolean sync(long millis) {
66         return getActivity().sync(millis);
67     }
68
69     // Properties
70
// -------------------------------------------------------------------------
71

72     public ParallelActivity getActivity() {
73         if (activity == null) {
74             activity = createActivity();
75         }
76         return activity;
77     }
78
79     public Executor JavaDoc getExecutor() {
80         if (executor == null) {
81             executor = createExecutor();
82         }
83         return executor;
84     }
85
86     public void setExecutor(Executor JavaDoc executor) {
87         this.executor = executor;
88     }
89
90     public JoinSupport getJoin() {
91         if (join == null) {
92             join = createJoin();
93         }
94         return join;
95     }
96
97     public void setJoin(JoinSupport join) {
98         this.join = join;
99     }
100
101     // Factory methods
102
// -------------------------------------------------------------------------
103
protected ParallelActivity createActivity() {
104         return ParallelActivity.newParallelMethodActivity(getJoin(), getExecutor(), this);
105     }
106
107     protected Executor JavaDoc createExecutor() {
108         return Executors.newFixedThreadPool(maxThreadPoolSize);
109     }
110
111     protected JoinSupport createJoin() {
112         return new JoinAll();
113     }
114
115     protected void sleep(int timeout) {
116         try {
117             Thread.sleep(3000);
118         }
119         catch (InterruptedException JavaDoc e) {
120             Thread.currentThread().interrupt();
121         }
122     }
123 }
124
Popular Tags