java - Spring boot application thread safe -
i have spring-boot java application streams data continuously kafka , saves database cassandra after applying business logic.
below pseudo classes , functions resemble application fully.
kafkastreamer
@configuration @enablekafka public class kafkastreamer { private static final logger logger = loggerfactory.getlogger(mydomain.class); @autowired private mycontroller mycontroller; @kafkalistener(topics = "${my-topic}", group = "${my-group}") public void streamfromkafka(string payload) { mycontroller.processpayload(payload); logger.info("i continously streaming data kafka " + "and forwarding controller further processing...!"); } }
mycontroller
@controller public class mycontroller { private static final logger logger = loggerfactory.getlogger(mydomain.class); @autowired private myservice myservice; public void processpayload(string payload) { myservice.applybusinesslogic(payload); logger.info("send service business-logic processing"); } }
myservice
@service public class myservice { private static final logger logger = loggerfactory.getlogger(mydomain.class); @autowired private mydomain mydomain; public void applybusinesslogic(string payload) { mydomain.savetodatabase(payload); logger.info("applied business-logic"); } }
mydomain
@repository public class mydomain { private static final logger logger = loggerfactory.getlogger(mydomain.class); @autowired private cassandraoperations cassandratemplate; /** session. */ private session session = null; public void savetodatabase(string payload) { savetotablea(payload); savetotableb(payload); // hello, have saved data database logger.info("saved data database"); } private void savetotableb(string payload) { if (session == null) session = cassandratemplate.getsession(); session.execute(payload); } private void savetotablea(string payload) { if (session == null) session = cassandratemplate.getsession() session.execute(payload); } }
the above pseudo code resembles original application fully.
as can see not have class level variables other logger, auto-wired variable , cassandra session in mydomain class
according knowledge, auto-wire default in spring-boot singleton.
i passing payload (which message kafka) 1 class class in function argument rather setting class level property of other class.
my question is,
is above application architecture or code thread safe ?.
can autowire create problem default gives singleton reference of class ( point note here not have class level variables other logger , auto-wire variable)
if feel if there exists better way or anything, please feel free write.
many me.
your solution stops thread-safe if start mutate state of shared object.
for example if you'd have payload
property in mydomain
singleton spring bean , set value in savetodatabase
, bit later (even in same method) consult it, mutation , code won't thread-safe.
for example:
@repository public class mydomain { private string payload; public void savetodatabase(string payload) { this.payload = payload; savetotablea(); savetotableb(); } private void savetotablea() { tablea.save(this.payload); }
in case when several threads call savetodatabase
concurrently different values, there no guarantee value save db in savetotablea()
.
Comments
Post a Comment