|
1 |
| -/* |
2 |
| - * Copyright 2010-2013 the original author or authors. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| -package org.mybatis.spring.asyncsynchronization; |
17 |
| - |
18 |
| -import java.lang.reflect.InvocationHandler; |
19 |
| -import java.lang.reflect.InvocationTargetException; |
20 |
| -import java.lang.reflect.Method; |
21 |
| -import java.lang.reflect.Proxy; |
22 |
| -import java.util.HashSet; |
23 |
| -import java.util.Set; |
24 |
| - |
25 |
| -import org.springframework.transaction.support.TransactionSynchronization; |
26 |
| - |
27 |
| -/** |
28 |
| - * For use as ByteMan helper |
29 |
| - * |
30 |
| - * @author Alex Rykov |
31 |
| - * |
32 |
| - */ |
33 |
| -public class AsyncAfterCompletionHelper { |
34 |
| - /** |
35 |
| - * |
36 |
| - * Invocation handler that performs afterCompletion on a separate thread |
37 |
| - * See Github issue #18 |
38 |
| - * |
39 |
| - * @author Alex Rykov |
40 |
| - * |
41 |
| - */ |
42 |
| - static class AsynchAfterCompletionInvocationHandler implements |
43 |
| - InvocationHandler { |
44 |
| - |
45 |
| - private Object target; |
46 |
| - |
47 |
| - AsynchAfterCompletionInvocationHandler(Object target) { |
48 |
| - this.target = target; |
49 |
| - } |
50 |
| - |
51 |
| - public Object invoke(final Object proxy, final Method method, |
52 |
| - final Object[] args) throws Throwable { |
53 |
| - if ("afterCompletion".equals(method.getName())) { |
54 |
| - final Set<Object> retValSet = new HashSet<Object>(); |
55 |
| - final Set<Throwable> exceptionSet = new HashSet<Throwable>(); |
56 |
| - Thread thread = new Thread() { |
57 |
| - @Override |
58 |
| - public void run() { |
59 |
| - try { |
60 |
| - retValSet.add(method.invoke(target, args)); |
61 |
| - } catch (InvocationTargetException ite) { |
62 |
| - exceptionSet.add(ite.getCause()); |
63 |
| - |
64 |
| - } catch (IllegalArgumentException e) { |
65 |
| - exceptionSet.add(e); |
66 |
| - |
67 |
| - } catch (IllegalAccessException e) { |
68 |
| - exceptionSet.add(e); |
69 |
| - } |
70 |
| - } |
71 |
| - }; |
72 |
| - thread.start(); |
73 |
| - thread.join(); |
74 |
| - if (exceptionSet.isEmpty()) { |
75 |
| - return retValSet.iterator().next(); |
76 |
| - } else { |
77 |
| - throw exceptionSet.iterator().next(); |
78 |
| - } |
79 |
| - } else { |
80 |
| - return method.invoke(target, args); |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - } |
85 |
| - |
86 |
| - /** |
87 |
| - * Creates proxy that performs afterCompletion call on a separate thread |
88 |
| - * |
89 |
| - * @param synchronization |
90 |
| - * @return |
91 |
| - */ |
92 |
| - public TransactionSynchronization createSynchronizationWithAsyncAfterComplete( |
93 |
| - TransactionSynchronization synchronization) { |
94 |
| - if (Proxy.isProxyClass(synchronization.getClass()) |
95 |
| - && Proxy.getInvocationHandler(synchronization) instanceof AsynchAfterCompletionInvocationHandler) { |
96 |
| - // avoiding double wrapping just in case |
97 |
| - return synchronization; |
98 |
| - } |
99 |
| - Class<?>[] interfaces = { TransactionSynchronization.class }; |
100 |
| - return (TransactionSynchronization) Proxy.newProxyInstance(synchronization |
101 |
| - .getClass().getClassLoader(), interfaces, |
102 |
| - new AsynchAfterCompletionInvocationHandler(synchronization)); |
103 |
| - |
104 |
| - } |
105 |
| - |
106 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2010-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.mybatis.spring.asyncsynchronization; |
| 17 | + |
| 18 | +import java.lang.reflect.InvocationHandler; |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.lang.reflect.Proxy; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.springframework.transaction.support.TransactionSynchronization; |
| 26 | + |
| 27 | +/** |
| 28 | + * For use as ByteMan helper |
| 29 | + * |
| 30 | + * @author Alex Rykov |
| 31 | + * |
| 32 | + */ |
| 33 | +public class AsyncAfterCompletionHelper { |
| 34 | + /** |
| 35 | + * |
| 36 | + * Invocation handler that performs afterCompletion on a separate thread |
| 37 | + * See Github issue #18 |
| 38 | + * |
| 39 | + * @author Alex Rykov |
| 40 | + * |
| 41 | + */ |
| 42 | + static class AsynchAfterCompletionInvocationHandler implements |
| 43 | + InvocationHandler { |
| 44 | + |
| 45 | + private Object target; |
| 46 | + |
| 47 | + AsynchAfterCompletionInvocationHandler(Object target) { |
| 48 | + this.target = target; |
| 49 | + } |
| 50 | + |
| 51 | + public Object invoke(final Object proxy, final Method method, |
| 52 | + final Object[] args) throws Throwable { |
| 53 | + if ("afterCompletion".equals(method.getName())) { |
| 54 | + final Set<Object> retValSet = new HashSet<Object>(); |
| 55 | + final Set<Throwable> exceptionSet = new HashSet<Throwable>(); |
| 56 | + Thread thread = new Thread() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + try { |
| 60 | + retValSet.add(method.invoke(target, args)); |
| 61 | + } catch (InvocationTargetException ite) { |
| 62 | + exceptionSet.add(ite.getCause()); |
| 63 | + |
| 64 | + } catch (IllegalArgumentException e) { |
| 65 | + exceptionSet.add(e); |
| 66 | + |
| 67 | + } catch (IllegalAccessException e) { |
| 68 | + exceptionSet.add(e); |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + thread.start(); |
| 73 | + thread.join(); |
| 74 | + if (exceptionSet.isEmpty()) { |
| 75 | + return retValSet.iterator().next(); |
| 76 | + } else { |
| 77 | + throw exceptionSet.iterator().next(); |
| 78 | + } |
| 79 | + } else { |
| 80 | + return method.invoke(target, args); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates proxy that performs afterCompletion call on a separate thread |
| 88 | + * |
| 89 | + * @param synchronization |
| 90 | + * @return |
| 91 | + */ |
| 92 | + public TransactionSynchronization createSynchronizationWithAsyncAfterComplete( |
| 93 | + TransactionSynchronization synchronization) { |
| 94 | + if (Proxy.isProxyClass(synchronization.getClass()) |
| 95 | + && Proxy.getInvocationHandler(synchronization) instanceof AsynchAfterCompletionInvocationHandler) { |
| 96 | + // avoiding double wrapping just in case |
| 97 | + return synchronization; |
| 98 | + } |
| 99 | + Class<?>[] interfaces = { TransactionSynchronization.class }; |
| 100 | + return (TransactionSynchronization) Proxy.newProxyInstance(synchronization |
| 101 | + .getClass().getClassLoader(), interfaces, |
| 102 | + new AsynchAfterCompletionInvocationHandler(synchronization)); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments