1 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 ; 25 import java.util.concurrent.Executors ; 26 27 36 public abstract class ParallelBean { 37 38 private ParallelActivity activity; 39 private Executor executor; 40 private JoinSupport join; 41 private int maxThreadPoolSize = 20; 42 43 public ParallelBean() { 44 } 45 46 public ParallelBean(Executor executor) { 47 this.executor = executor; 48 } 49 50 public ParallelBean(Executor executor, JoinSupport join) { 51 this.executor = executor; 52 this.join = join; 53 } 54 55 @SuppressWarnings ("unchecked") 56 public void start() { 57 getActivity().start(); 58 } 59 60 @SuppressWarnings ("unchecked") 61 public void sync() { 62 getActivity().sync(); 63 } 64 65 public boolean sync(long millis) { 66 return getActivity().sync(millis); 67 } 68 69 72 public ParallelActivity getActivity() { 73 if (activity == null) { 74 activity = createActivity(); 75 } 76 return activity; 77 } 78 79 public Executor getExecutor() { 80 if (executor == null) { 81 executor = createExecutor(); 82 } 83 return executor; 84 } 85 86 public void setExecutor(Executor 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 protected ParallelActivity createActivity() { 104 return ParallelActivity.newParallelMethodActivity(getJoin(), getExecutor(), this); 105 } 106 107 protected Executor 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 e) { 120 Thread.currentThread().interrupt(); 121 } 122 } 123 } 124 | Popular Tags |