1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.sf.urlchecker.commands;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.TimeUnit;
28
29 import net.sf.urlchecker.events.BasicChainEvent;
30 import net.sf.urlchecker.events.EventTypes;
31
32 import org.apache.commons.configuration.ConfigurationException;
33 import org.apache.commons.httpclient.HttpClient;
34 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class CheckUrlsCommand extends CheckUrlsGeneric<HttpClient> {
51
52
53
54
55
56
57
58
59
60
61 public CheckUrlsCommand(Command successor, HttpClient newClient) {
62 super(successor, newClient);
63 }
64
65
66
67
68
69
70 @Override
71 public boolean isMultithreaded() {
72 return client.getHttpConnectionManager().getClass()
73 .isAssignableFrom(MultiThreadedHttpConnectionManager.class);
74 }
75
76
77
78
79
80
81 @Override
82 protected void multithreadedExecution(final Context context,
83 Iterator<Result> iter) throws ConfigurationException {
84 final ExecutorService service = Executors.newFixedThreadPool(context
85 .getResults().size());
86 final List<Future<CheckUrlsProcess>> tasks = new ArrayList<Future<CheckUrlsProcess>>();
87
88 while (iter.hasNext()) {
89 fireEvent(new BasicChainEvent(this, context, EventTypes.FORK));
90 final CheckUrlsProcess process = new CheckUrlsProcess(client,
91 iter.next());
92 final Future<CheckUrlsProcess> future = service.submit(process,
93 process);
94 tasks.add(future);
95 }
96
97 try {
98 for (final Future<CheckUrlsProcess> f : tasks) {
99 final Result result = f.get().getResult();
100
101 if (LOGGER.isDebugEnabled()) {
102 LOGGER.debug(result.getResult() + " for " + result.getURI()
103 + " with " + result.getUserData());
104 }
105 }
106
107 service.shutdown();
108 } catch (final InterruptedException e) {
109 LOGGER.error(e);
110 fireEvent(new BasicChainEvent(this, context, EventTypes.EXCEPTION));
111 } catch (final ExecutionException e) {
112 LOGGER.error(e);
113 fireEvent(new BasicChainEvent(this, context, EventTypes.EXCEPTION));
114 } finally {
115 try {
116 if (!service.isShutdown()) {
117 service.awaitTermination(terminationTime, TimeUnit.SECONDS);
118 }
119 } catch (final InterruptedException e) {
120 LOGGER.error(e);
121 fireEvent(new BasicChainEvent(this, context,
122 EventTypes.EXCEPTION));
123 } finally {
124 if (!service.isShutdown()) {
125 service.shutdownNow();
126 }
127 }
128 }
129 }
130
131
132
133
134
135
136 @Override
137 protected void singlethreadedExecution(final Iterator<Result> iter)
138 throws ConfigurationException {
139
140 while (iter.hasNext()) {
141 final CheckUrlsProcess process = new CheckUrlsProcess(client,
142 iter.next());
143 process.run();
144 }
145 }
146 }