title: Spring boot application thread safe
body: <p>I have a <strong>Spring-boot Java</strong> application which streams data continuously from <strong>Kafka</strong> and saves it to the database <strong>Cassandra</strong> after applying business logic.</p>
  
  <p>Below are the pseudo classes and functions which resemble my application fully. </p>
  
  <p><strong>KafkaStreamer</strong></p>
  
  <pre><code> @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 am continously streaming data from Kafka "
                  + "and forwarding it to controller for further processing...!");
      }
  }
  </code></pre>
  
  <p><strong>MyController</strong></p>
  
  <pre><code>@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 to service for business-logic processing");
      }
  }
  </code></pre>
  
  <p><strong>MyService</strong></p>
  
  <pre><code>@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");
      }
  }
  </code></pre>
  
  <p><strong>MyDomain</strong></p>
  
  <pre><code>    @Repository
      public class MyDomain {
  
          private static final Logger LOGGER = LoggerFactory.getLogger(MyDomain.class);
  
     @Autowired
      private CassandraOperations cassandraTemplate;
  
      /** The session. */
      private Session session = null;
  
          public void saveToDatabase(String payload) {
              saveToTableA(payload);
              saveToTableB(payload);
              // Hello, I have saved data to database
              LOGGER.info("Saved data to 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);
  
          }
  
      }
  </code></pre>
  
  <p>The above pseudo code resembles my original application fully. </p>
  
  <p>As you can see I do <strong>not have any class level variables</strong> <strong>other than logger, some auto-wired variable and cassandra session in MyDomain class</strong></p>
  
  <p>According to my knowledge, <strong>auto-wire</strong> by default in spring-boot is <strong>singleton</strong>.</p>
  
  <p>I am passing payload (which is my message from Kafka) from one class to another class in function argument rather setting as the class level property of other class.</p>
  
  <p>My question is, </p>
  
  <p><strong>Is my above application architecture or code thread safe ?</strong>.</p>
  
  <p>Can Autowire create problem as by default it gives singleton reference of a class ( a point to note here is I do not have any class level variables other than logger and auto-wire variable)</p>
  
  <p>If you feel if there exists a better way or anything, please feel free to write. </p>
  
  <p>Many thanks from me.</p>