jeudi 23 avril 2020

Thread safety in Mutable Spring Bean

I have Spring Boot service which is listener to Kafka cluster with @KafkaListner. Kafka provides json data to instruct my service on what need to do. I convert this configuration json to pojo upon receiving it. I want to share this pojo across my classes & methods. I don't want to pass through intermediary classes which doesn't need to know it

So, i did it like this.

  1. Create empty pojo and save into spring container first.
  2. Then, after receiving from Kafka, fetch empty pojo from spring container and set its values
  3. This way i can get it from anywhere in my service.

But, I find this mutable pojo is not thread-safe. I explored bean-scope and nothing seems to suit me.

Can you guys suggest how to make it thread safe? can provide example on using custom SimpleThreadScope?

Can you suggest any design pattern which doesn't need to use spring bean?

Aucun commentaire:

Enregistrer un commentaire