The Sales Kafka Mega Blog

TECH WOLF
4 min readMay 26, 2022

--

Part 1: Creating a Springboot Scheduler App which will randomly pick a store and book a order form some item.

DB Schema:

Note: We have not maintained any constraint to table for simplicity purpose. If you want then you can proceed with full fledge relation.

Our next step will be populating our Items table and Store table with data. So that our scheduler might be able to pickup data from store as per requirement.

For me I am using PL-SQL block to populate the same you can prefer the other way.

So, all right done with the db changes we will move on to the our next part and that is making a microservice that will make random order and will populate the Sales with the order details and the sales info. So let’s start with the part II.

Part 2: Creating a Spring boot application to generate random sales record.

You can find the whole source code for the part 2 of this app here. Feel free to fork or clone.

All right let’s now walk through the code and let’s try to understand what we did.

Project Structure

The following is the project Structure of our project. Since model is POJO. So we will simply be ignoring that part. If you require. Just clone the project and have a look.

Next we will look into ItemDao where we are basically selecting the item from particualr store.

Let’s see how:

package com.example.ordermaker.dao;

import com.example.ordermaker.mapper.ItemMapper;
import com.example.ordermaker.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ItemDao {

@Autowired
NamedParameterJdbcTemplate namedParameterJdbcTemplate;

@Value("${item.select.query}")
private String selectItem;

public List<Item> getItemDetails(Integer item)
{
Map<String,Object> params = new HashMap<>();
params.put("item",item);

return namedParameterJdbcTemplate.query(selectItem,params,new ItemMapper());
}
}

Over here, We are using NamedParameterJdbcTemplate for querying into jdbc. If you require a detailed explaination click on the link above for more.

Next we are simply making call to this query:

item.select.query = Select item,dept,class,subclass from Items where item = :item

Similarly we are querying into Store.

package com.example.ordermaker.dao;

import com.example.ordermaker.funcs.Funcs;
import com.example.ordermaker.mapper.StoreMapper;
import com.example.ordermaker.model.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class StoreDao {

@Autowired
NamedParameterJdbcTemplate namedParameterJdbcTemplate;

@Value("${store.select.query}")
private String selectStores;

public List<Store> findStoreToShop()
{
Integer randomStore = Funcs.getRandomNumber(1,90);
Map<String,Object> params = new HashMap<>();
params.put("store",randomStore);

return namedParameterJdbcTemplate.query(selectStores,params,new StoreMapper());
}
}

Once we are done querying Store and Item then comes the Insertion of Sales Date into the Sales Table. Well we will be doing it using Scheduler. At this point just have a look into the code what we have done. If you undertand well and good. If not then also proceed further till the end. By the end you will have a clear understanding what we are trying to perform.

Insert in Sales Table:

package com.example.ordermaker.dao;

import com.example.ordermaker.funcs.Funcs;
import com.example.ordermaker.model.Item;
import com.example.ordermaker.model.Sales;
import com.example.ordermaker.model.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Repository
public class SalesDao {
// (:customeroderno,:item,:store,:dept,:class,:sublass,:qty,:totalcost,:uuid)
@Autowired
NamedParameterJdbcTemplate namedParameterJdbcTemplate;

@Value("${sales.insert.query}")
private String inserSalesData;

public void makeSales(Store store, Item item,String customerOrderNo)
{
Integer qty = Funcs.getRandomNumber(1,10);
Float totalCost = qty*store.getUnitCost();

Map<String,Object> params = new HashMap<>();
params.put("customeroderno",customerOrderNo);
params.put("item",item.getItem());
params.put("store",store.getStore());
params.put("dept",item.getDept());
params.put("class",item.getDeptClass());
params.put("sublass",item.getSubclass());
params.put("qty",qty);
params.put("totalcost",totalCost);
String uuid = String.valueOf(UUID.randomUUID());
params.put("uuid",uuid);

namedParameterJdbcTemplate.update(inserSalesData,params);
}
}

All right done with Dao. We will now try to understand what our scheduler is doing. Let’s have a look at the code:

package com.example.ordermaker.scheduler;

import com.example.ordermaker.dao.ItemDao;
import com.example.ordermaker.dao.SalesDao;
import com.example.ordermaker.dao.StoreDao;
import com.example.ordermaker.funcs.Funcs;
import com.example.ordermaker.model.Item;
import com.example.ordermaker.model.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@EnableScheduling
@Component
public class SalesScheduler {

@Autowired
StoreDao storeDao;

@Autowired
ItemDao itemDao;

@Autowired
SalesDao salesDao;

@Scheduled(fixedDelay = 15000)
public void startSales()
{
List<Store> stores = storeDao.findStoreToShop();
Integer upperLimit = stores.size();
String customerOrderNO = String.valueOf(UUID.randomUUID());
for(int i = 0;i < 6;i++)
{
try {
Store store = stores.get(Funcs.getRandomNumber(0, upperLimit - 1));
Item item = itemDao.getItemDetails(store.getItem()).get(0);
salesDao.makeSales(store,item,customerOrderNO);
System.out.println("Inserting record" + item);
}catch (Exception e){
System.out.println("Failed in sechulder");
e.printStackTrace();
}

}
}
}

Over here,

@EnableScheduling, as clear from it’s name enable the scheduler to be used in this class. When springboot scans for the component. Next @Scheduled(fixedDelay = 15000) ensures our job runs every 15 sec and makes sales order for us. You can have a detailed ides of Scheduler from here

Next, we are querying the store and picking up 6 random items from store each item and placing order against these six items.

That’s it for the first part. In the next part we will continue with making a producer App for Kafka which will use make use of the sales data. Perofrm some business operation and at least send it to Some Consumer group.

___________________________________________________________________

--

--

No responses yet