How to get DB Response

Viewed 61
package com.example.demo.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener; 
import org.springframework.stereotype.Service;

import com.example.demo.User;
import com.example.demo.UserRepository;

@Service 
public class MessageConsumer {
  
    @Autowired
    UserRepository userrepo;
    @Autowired
    EmailSenderService senderservice;

    @KafkaListener(topics="k2-topic",groupId="mygroup2")

    public void consumeFromTopic(String message) {
        System.out.println("consumed message "+message);
        String msg = message;
        String vin = msg.substring(0, 17);
        String verified = msg.substring(17, 18);
        int sp = Integer.parseInt(msg.substring(18,21));
        String alert = msg.substring(21,22);
        char ch = alert.charAt(0);
        String timeStamp = msg.substring(22);
        String bd = "Hai There !\n Your CAR VIN NO IS : "+vin+"\nYou're crossing your SPEED 
        LIMIT : "+sp+"\nPlease Drive slowly\nSafe driving saves life.";

        if(ch=='y')  senderservice.sendEmail(bd);

        User obj = new User(vin,verified,sp,alert,timeStamp);
        userrepo.save(obj);
    } 
}

Hai in this code i am receiving message from kafka. based on that message i am sending mail before storing the message into database. But i want to send the mail after storing the data. if the response from db is ok then i will send mail otherwise not. So How to get DB response and how to know message is storing in DB or NOT. Iam using cassandra DB. Please Help.

1 Answers

If the db insertion will not succeed, then you will get an exception.
If you do not catch this exception in your service or in your repository, then this exception will be caught by Spring Web.

Thus, you can just send the email after running the save method.

package com.example.demo.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener; 
import org.springframework.stereotype.Service;

import com.example.demo.User;
import com.example.demo.UserRepository;

@Service 
public class MessageConsumer {
  
    @Autowired
    UserRepository userrepo;
    @Autowired
    EmailSenderService senderservice;

    @KafkaListener(topics="k2-topic",groupId="mygroup2")

    public void consumeFromTopic(String message) {
        System.out.println("consumed message "+message);
        String msg = message;
        String vin = msg.substring(0, 17);
        String verified = msg.substring(17, 18);
        int sp = Integer.parseInt(msg.substring(18,21));
        String alert = msg.substring(21,22);
        char ch = alert.charAt(0);
        String timeStamp = msg.substring(22);
        String bd = "Hai There !\n Your CAR VIN NO IS : "+vin+"\nYou're crossing your SPEED 
        LIMIT : "+sp+"\nPlease Drive slowly\nSafe driving saves life.";

        User obj = new User(vin,verified,sp,alert,timeStamp);
        userrepo.save(obj);

        if(ch=='y')  senderservice.sendEmail(bd);
    } 
}
  • If the insertion will not succeed then the email will not be send because an exception will be thrown by the Server.
  • If the insertion succeed, then the email will be send.
Related