Skip to content

Commit 29541a5

Browse files
Replace synchronized blocks with ReentrantLocks for virtual thread support
* The lock call was moved outside the try block * Unnecessary locking has been removed.
1 parent 58ffb22 commit 29541a5

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

consumer/spring-cassandra-consumer/src/main/java/org/springframework/cloud/fn/consumer/cassandra/CassandraConsumerConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525
import java.util.UUID;
26+
import java.util.concurrent.locks.Lock;
27+
import java.util.concurrent.locks.ReentrantLock;
2628
import java.util.function.Consumer;
2729
import java.util.function.Function;
2830

@@ -62,6 +64,7 @@
6264
* @author Thomas Risberg
6365
* @author Ashu Gairola
6466
* @author Akos Ratku
67+
* @author Omer Celik
6568
*/
6669
@AutoConfiguration(after = CassandraReactiveDataAutoConfiguration.class)
6770
@EnableConfigurationProperties(CassandraConsumerProperties.class)
@@ -147,6 +150,8 @@ private static class PayloadToMatrixTransformer extends AbstractPayloadTransform
147150

148151
private final ISO8601StdDateFormat dateFormat = new ISO8601StdDateFormat();
149152

153+
private final Lock dateLock = new ReentrantLock();
154+
150155
PayloadToMatrixTransformer(ObjectMapper objectMapper, String query, ColumnNameExtractor columnNameExtractor) {
151156
this.jsonObjectMapper = new Jackson2JsonObjectMapper(objectMapper);
152157
this.columns.addAll(columnNameExtractor.extract(query));
@@ -170,9 +175,13 @@ protected List<List<Object>> transformPayload(Object payload) {
170175
Object value = entity.get(column);
171176
if (value instanceof String string) {
172177
if (this.dateFormat.looksLikeISO8601(string)) {
173-
synchronized (this.dateFormat) {
178+
this.dateLock.lock();
179+
try {
174180
value = new Date(this.dateFormat.parse(string).getTime()).toLocalDate();
175181
}
182+
finally {
183+
this.dateLock.unlock();
184+
}
176185
}
177186
if (isUuid(string)) {
178187
value = UUID.fromString(string);

consumer/spring-websocket-consumer/src/main/java/org/springframework/cloud/fn/consumer/websocket/trace/InMemoryTraceRepository.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.LinkedList;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.concurrent.locks.Lock;
25+
import java.util.concurrent.locks.ReentrantLock;
2426

2527
/**
2628
* A repository for {@link Trace}s.
@@ -31,6 +33,7 @@
3133
* @author Dave Syer
3234
* @author Olivier Bourgain
3335
* @author Artem Bilan
36+
* @author Omer Celik
3437
* @since 2.0
3538
*/
3639
public class InMemoryTraceRepository {
@@ -41,35 +44,32 @@ public class InMemoryTraceRepository {
4144

4245
private final List<Trace> traces = new LinkedList<>();
4346

47+
private final Lock tracesLock = new ReentrantLock();
48+
4449
/**
4550
* Flag to say that the repository lists traces in reverse order.
4651
* @param reverse flag value (default true)
4752
*/
4853
public void setReverse(boolean reverse) {
49-
synchronized (this.traces) {
50-
this.reverse = reverse;
51-
}
54+
this.reverse = reverse;
5255
}
5356

5457
/**
5558
* Set the capacity of the in-memory repository.
5659
* @param capacity the capacity
5760
*/
5861
public void setCapacity(int capacity) {
59-
synchronized (this.traces) {
60-
this.capacity = capacity;
61-
}
62+
this.capacity = capacity;
6263
}
6364

6465
public List<Trace> findAll() {
65-
synchronized (this.traces) {
66-
return Collections.unmodifiableList(this.traces);
67-
}
66+
return Collections.unmodifiableList(this.traces);
6867
}
6968

7069
public void add(Map<String, Object> map) {
7170
Trace trace = new Trace(new Date(), map);
72-
synchronized (this.traces) {
71+
this.tracesLock.lock();
72+
try {
7373
while (this.traces.size() >= this.capacity) {
7474
this.traces.remove(this.reverse ? this.capacity - 1 : 0);
7575
}
@@ -80,6 +80,9 @@ public void add(Map<String, Object> map) {
8080
this.traces.add(trace);
8181
}
8282
}
83+
finally {
84+
this.tracesLock.unlock();
85+
}
8386
}
8487

8588
}

0 commit comments

Comments
 (0)