forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionAwareBufferedWriter.java
250 lines (216 loc) · 6.94 KB
/
TransactionAwareBufferedWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.support.transaction;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.springframework.batch.item.WriteFailedException;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* Wrapper for a {@link FileChannel} that delays actually writing to or closing the
* buffer if a transaction is active. If a transaction is detected on the call
* to {@link #write(String)} the parameter is buffered and passed on to the
* underlying writer only when the transaction is committed.
*
* @author Dave Syer
* @author Michael Minella
* @author Niels Ferguson
*
*/
public class TransactionAwareBufferedWriter extends Writer {
private final Object bufferKey;
private final Object closeKey;
private FileChannel channel;
private final Runnable closeCallback;
// default encoding for writing to output files - set to UTF-8.
private static final String DEFAULT_CHARSET = "UTF-8";
private String encoding = DEFAULT_CHARSET;
private boolean forceSync = false;
/**
* Create a new instance with the underlying file channel provided, and a callback
* to execute on close. The callback should clean up related resources like
* output streams or channels.
*
* @param channel channel used to do the actual file IO
* @param closeCallback callback to execute on close
*/
public TransactionAwareBufferedWriter(FileChannel channel, Runnable closeCallback) {
super();
this.channel = channel;
this.closeCallback = closeCallback;
this.bufferKey = new Object();
this.closeKey = new Object();
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* Flag to indicate that changes should be force-synced to disk on flush.
* Defaults to false, which means that even with a local disk changes could
* be lost if the OS crashes in between a write and a cache flush. Setting
* to true may result in slower performance for usage patterns involving
* many frequent writes.
*
* @param forceSync the flag value to set
*/
public void setForceSync(boolean forceSync) {
this.forceSync = forceSync;
}
/**
* @return
*/
private StringBuilder getCurrentBuffer() {
if (!TransactionSynchronizationManager.hasResource(bufferKey)) {
TransactionSynchronizationManager.bindResource(bufferKey, new StringBuilder());
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCompletion(int status) {
clear();
}
@Override
public void beforeCommit(boolean readOnly) {
try {
if(!readOnly) {
complete();
}
}
catch (IOException e) {
throw new FlushFailedException("Could not write to output buffer", e);
}
}
private void complete() throws IOException {
StringBuilder buffer = (StringBuilder) TransactionSynchronizationManager.getResource(bufferKey);
if (buffer != null) {
String string = buffer.toString();
byte[] bytes = string.getBytes(encoding);
int bufferLength = bytes.length;
ByteBuffer bb = ByteBuffer.wrap(bytes);
int bytesWritten = channel.write(bb);
if(bytesWritten != bufferLength) {
throw new IOException("All bytes to be written were not successfully written");
}
if (forceSync) {
channel.force(false);
}
if (TransactionSynchronizationManager.hasResource(closeKey)) {
closeCallback.run();
}
}
}
private void clear() {
if (TransactionSynchronizationManager.hasResource(bufferKey)) {
TransactionSynchronizationManager.unbindResource(bufferKey);
}
if (TransactionSynchronizationManager.hasResource(closeKey)) {
TransactionSynchronizationManager.unbindResource(closeKey);
}
}
});
}
return (StringBuilder) TransactionSynchronizationManager.getResource(bufferKey);
}
/**
* Convenience method for clients to determine if there is any unflushed
* data.
*
* @return the current size (in bytes) of unflushed buffered data
*/
public long getBufferSize() {
if (!transactionActive()) {
return 0L;
}
try {
return getCurrentBuffer().toString().getBytes(encoding).length;
} catch (UnsupportedEncodingException e) {
throw new WriteFailedException("Could not determine buffer size because of unsupported encoding: " + encoding, e);
}
}
/**
* @return
*/
private boolean transactionActive() {
return TransactionSynchronizationManager.isActualTransactionActive();
}
/*
* (non-Javadoc)
*
* @see java.io.Writer#close()
*/
@Override
public void close() throws IOException {
if (transactionActive()) {
if (getCurrentBuffer().length() > 0) {
TransactionSynchronizationManager.bindResource(closeKey, Boolean.TRUE);
}
return;
}
closeCallback.run();
}
/*
* (non-Javadoc)
*
* @see java.io.Writer#flush()
*/
@Override
public void flush() throws IOException {
if (!transactionActive() && forceSync) {
channel.force(false);
}
}
/*
* (non-Javadoc)
*
* @see java.io.Writer#write(char[], int, int)
*/
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (!transactionActive()) {
byte[] bytes = new String(cbuf, off, len).getBytes(encoding);
int length = bytes.length;
ByteBuffer bb = ByteBuffer.wrap(bytes);
int bytesWritten = channel.write(bb);
if(bytesWritten != length) {
throw new IOException("Unable to write all data. Bytes to write: " + len + ". Bytes written: " + bytesWritten);
}
return;
}
StringBuilder buffer = getCurrentBuffer();
buffer.append(cbuf, off, len);
}
/*
* (non-Javadoc)
*
* @see java.io.Writer#write(String, int, int)
*/
@Override
public void write(String str, int off, int len) throws IOException {
if (!transactionActive()) {
byte[] bytes = str.substring(off, off + len).getBytes(encoding);
int length = bytes.length;
ByteBuffer bb = ByteBuffer.wrap(bytes);
int bytesWritten = channel.write(bb);
if(bytesWritten != length) {
throw new IOException("Unable to write all data. Bytes to write: " + len + ". Bytes written: " + bytesWritten);
}
return;
}
StringBuilder buffer = getCurrentBuffer();
buffer.append(str, off, off + len);
}
}