1 package com.atlassian.core.task;
2
3 import org.apache.log4j.Logger;
4
5 import javax.mail.MessagingException;
6 import java.io.Serializable;
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10
11
12
13
14
15
16
17
18 public class AbstractErrorQueuedTaskQueue extends AbstractTaskQueue implements TaskQueueWithErrorQueue
19 {
20 private static final transient Logger log = Logger.getLogger(AbstractErrorQueuedTaskQueue.class);
21
22 private TaskQueue errorQueue;
23
24 private int retryCount = 5;
25
26 private List failed;
27
28 public AbstractErrorQueuedTaskQueue(TaskQueue errorQueue, FifoBuffer buffer)
29 {
30 super(buffer);
31 this.errorQueue = errorQueue;
32 }
33
34 public void flush()
35 {
36 failed = new ArrayList();
37 super.flush();
38 for (Iterator iterator = failed.iterator(); iterator.hasNext();)
39 {
40 addTask((Task)iterator.next());
41 }
42 }
43
44 protected void handleException(Task task, Exception rootException)
45 {
46 TaskDecorator theTask = (TaskDecorator)task;
47
48 if (theTask.getExecutionCount() > retryCount) {
49
50 errorQueue.addTask(theTask.getTask());
51 }else {
52
53 failed.add(task);
54 }
55 if (rootException instanceof MessagingException)
56 {
57 Exception e = rootException;
58 while (e instanceof MessagingException)
59 {
60 MessagingException me = (MessagingException)e;
61 log.error(me.getMessage(), me);
62 e = me.getNextException();
63 }
64 }
65 else
66 log.error(rootException, rootException);
67 }
68
69 public void addTask(Task task)
70 {
71 if(task instanceof TaskDecorator)
72 {
73 super.addTask(task);
74 } else {
75 super.addTask(new TaskDecorator(task));
76 }
77 }
78
79 public TaskQueue getErrorQueue()
80 {
81 return errorQueue;
82 }
83
84 public int getRetryCount()
85 {
86 return retryCount;
87 }
88
89 public void setRetryCount(int retryCount)
90 {
91 this.retryCount = retryCount;
92 }
93
94 public static class TaskDecorator implements Task, Serializable
95 {
96 private Task task;
97 private int executionCount = 0;
98
99 public TaskDecorator(Task task)
100 {
101 this.task = task;
102 }
103
104 public void execute() throws Exception
105 {
106 executionCount++;
107 task.execute();
108 }
109
110 public int getExecutionCount()
111 {
112 return executionCount;
113 }
114
115 public Task getTask()
116 {
117 return task;
118 }
119 }
120 }