8
8
import com .cloudbees .jenkins .GitHubWebHook ;
9
9
import hudson .Extension ;
10
10
import hudson .model .PeriodicWork ;
11
+ import hudson .util .SequentialExecutionQueue ;
11
12
import hudson .util .TimeUnit2 ;
12
13
13
14
import java .util .List ;
15
+ import java .util .concurrent .Executors ;
14
16
import java .util .logging .Logger ;
15
17
16
18
/**
@@ -23,20 +25,45 @@ public class SqsQueueHandler extends PeriodicWork {
23
25
24
26
private static final Logger LOGGER = Logger .getLogger (SqsQueueHandler .class .getName ());
25
27
28
+ private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue (Executors .newFixedThreadPool (2 ));
29
+
26
30
@ Override
27
31
public long getRecurrencePeriod () {
28
- return TimeUnit2 .SECONDS .toMillis (30 );
32
+ return TimeUnit2 .SECONDS .toMillis (2 );
29
33
}
30
34
31
35
@ Override
32
36
protected void doRun () throws Exception {
33
- List <SqsProfile > profiles = SqsBuildTrigger .DescriptorImpl .get ().getSqsProfiles ();
34
- for (SqsProfile profile : profiles ) {
37
+ if (queue .getInProgress ().size () == 0 ) {
38
+ List <SqsProfile > profiles = SqsBuildTrigger .DescriptorImpl .get ().getSqsProfiles ();
39
+ queue .setExecutors (Executors .newFixedThreadPool (profiles .size ()));
40
+ for (final SqsProfile profile : profiles ) {
41
+ queue .execute (new SQSQueueReceiver (profile ));
42
+ }
43
+ } else {
44
+ logger .fine ("Currently Waiting for Messages from Queues" );
45
+ }
46
+ }
47
+
48
+ public static SqsQueueHandler get () {
49
+ return PeriodicWork .all ().get (SqsQueueHandler .class );
50
+ }
51
+
52
+ private class SQSQueueReceiver implements Runnable {
53
+
54
+ private SqsProfile profile ;
55
+
56
+ private SQSQueueReceiver (SqsProfile profile ) {
57
+ this .profile = profile ;
58
+ }
59
+
60
+ public void run () {
35
61
LOGGER .fine ("looking for build triggers on queue:" + profile .sqsQueue );
36
62
AmazonSQS sqs = profile .getSQSClient ();
37
63
String queueUrl = profile .getQueueUrl ();
38
64
TriggerProcessor processor = profile .getTriggerProcessor ();
39
65
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest (queueUrl );
66
+ receiveMessageRequest .setWaitTimeSeconds (20 );
40
67
List <Message > messages = sqs .receiveMessage (receiveMessageRequest ).getMessages ();
41
68
for (Message message : messages ) {
42
69
//Process the message payload, it needs to conform to the GitHub Web-Hook JSON format
@@ -52,8 +79,4 @@ protected void doRun() throws Exception {
52
79
}
53
80
}
54
81
}
55
-
56
- public static SqsQueueHandler get () {
57
- return PeriodicWork .all ().get (SqsQueueHandler .class );
58
- }
59
82
}
0 commit comments