英文:
Java common variable across the objects for a request
问题
I using Kafka to get a message and update its corresponding data if already present in the DB. There lot of complex objects involved for the synchronization.
I wanted to print at the end of synchronization, all the objects that has got updated. Flow starts like below,
public class Consumer {
@KafkaListener(topics = "${kafka.topics.test}")
public void synchroData(FinTechData finTechData) {
finService.update(finTechData);
}
}
public class FinServiceImpl {
public void updateMetrics(FinTechData finTechData) {
if (finTechData != null) {
//update finTechData data ....
revolverService.updateRevolver(finTechData.getRevolver());
}
}
}
public class RevolverServiceImpl {
public void updateRevolver(Revolver revolver) {
if (revolver != null) {
//update revolver data ....
utilizationService.updateUtilization(revolver.getUtilization());
exposureService.updateexposure(revolver.getExposure());
}
}
}
....
It goes on for 8 to 10 objects. At the end I wanted to know what are the objects that got updated.
Currently I have a class like below with List of String as property and every time when any object gets updated,
```java
public class Context {
List<String> objectsUpdated = new ArrayList<>();
}
I add a message to it. But I need to pass the object to all the methods down the line. Is there any efficient way of doing it ?
I looking for below,
FinTech Data updated
Revolver Data updated
Exposure Data updated
...
英文:
I using Kafka to get a message and update its corresponding data if already present in the DB. There lot of complex objects involved for the synchronization.
I wanted to print at the end of synchronization, all the objects that has got updated. Flow starts like below,
public class Consumer {
@KafkaListener(topics = "${kafka.topics.test}")
public void synchroData(FinTechData finTechData) {
finService.update(finTechData);
}
}
public class FinServiceImpl {
public void updateMetrics(FinTechData finTechData) {
if (finTechData != null) {
//update finTechData data ....
revolverService.updateRevolver(finTechData.getRevolver());
}
}
}
public class RevolverServiceImpl {
public void updateRevolver(Revolver revolver) {
if (revolver != null) {
//update revolver data ....
utilizationService.updateUtilization(revolver.getUtilization());
exposureService.updateexposure(revolver.getExposure());
}
}
}
....
It goes on for 8 to 10 objects. At the end I wanted to know what are the objects that got updated.
Currently I have a class like below with List of String as property and everytime when any object gets updated,
public class Context {
List<String> objectsUpdated = new ArrayList<>();
}
I add a message to it. But I need to pass the object to all the methods down the line. Is there any efficient way of doing it ?
I looking for below,
FinTech Data updated
Revolver Data updated
Exposure Data updated
...
答案1
得分: 1
你可以使用ThreadLocal将你的Context类包装起来,例如在你的Consumer类中添加以下代码:
public static final ThreadLocal<Context> perThreadContext = new ThreadLocal();
然后在 synchroData()
方法中,在调用 finService.update(finTechData);
之前添加 perThreadContext.set(new Context());
,在每个更新程序中,当你想要向上下文添加新条目时使用 Consumer.perThreadContext.get();
。
ThreadLocal包装器将为每个消费者线程创建一个单独的Context实例。这样,你将获得由该方法特定调用更新的对象列表。
参考:https://www.baeldung.com/java-threadlocal 以获取有关ThreadLocal的更多信息。
英文:
You can wrap your Context class with ThreadLocal, e.g. in your Consumer class add:
public static final ThreadLocal<Context> perThreadContext = new ThreadLocal();
Then in synchroData()
add perThreadContext.set(new Context());
before calling finService.update(finTechData);
and use Consumer.perThreadContext.get();
in each one of the updaters when you want to add a new entry to the context.
The ThreadLocal wrapper will create a separate instance of Context for each consumer thread. This way you'll have the list of objects updated by this specific invocation of the method.
See: https://www.baeldung.com/java-threadlocal for additional read bout ThreadLocal.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论