1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.student.enrollment.class2.courseofferingset.service.impl;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.transaction.support.TransactionSynchronization;
21 import org.springframework.transaction.support.TransactionSynchronizationManager;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27
28
29
30
31
32 public class KSThreadRunnerAfterTransactionSynchronization implements TransactionSynchronization {
33 final static Logger LOGGER = Logger.getLogger(KSThreadRunnerAfterTransactionSynchronization.class);
34
35 public static final String THREAD_LIST_KEY ="org.kuali.student.ThreadList";
36
37 private static final KSThreadRunnerAfterTransactionSynchronization instance = new KSThreadRunnerAfterTransactionSynchronization();
38
39 @Override
40 public void suspend() {
41
42 }
43
44 @Override
45 public void resume() {
46
47 }
48
49 @Override
50 public void flush() {
51
52 }
53
54 @Override
55 public void beforeCommit(boolean readOnly) {
56
57 }
58
59 @Override
60 public void beforeCompletion() {
61
62 }
63
64 @Override
65 public void afterCommit() {
66
67 }
68
69 @Override
70
71 public void afterCompletion(int status) {
72 List<Runnable> threads = (List<Runnable>) TransactionSynchronizationManager.getResource(THREAD_LIST_KEY);
73 if(threads == null){
74 LOGGER.error("The list of threads has not been initialized.");
75 }else{
76
77 for(Iterator<Runnable> iter = threads.iterator(); iter.hasNext(); ){
78 Runnable runner = iter.next();
79 iter.remove();
80 Thread thread = new Thread(runner);
81 thread.start();
82 }
83 }
84 }
85
86 public static void runAfterTransactionCompletes(Runnable runnable){
87 instance._runAfterTransactionCompletes(runnable);
88 }
89
90 private void _runAfterTransactionCompletes(Runnable runnable){
91 if(!TransactionSynchronizationManager.isSynchronizationActive()){
92
93
94
95 Thread thread = new Thread(runnable);
96 thread.start();
97 }else{
98
99
100 TransactionSynchronizationManager.registerSynchronization(this);
101
102 List<Runnable> runnables = (List<Runnable>) TransactionSynchronizationManager.getResource(THREAD_LIST_KEY);
103
104 if(runnables == null){
105 runnables = new ArrayList<Runnable>();
106
107 TransactionSynchronizationManager.bindResource(THREAD_LIST_KEY, runnables);
108 }
109
110 runnables.add(runnable);
111 }
112 }
113 }