Skip to content

Commit

Permalink
adding brewing service - closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Jul 21, 2019
1 parent 94bce9a commit 4f54b97
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
@Configuration
public class JmsConfig {

public static final String BREWING_REQUEST_QUEUE = "brewing-request";

@Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.msscbeerservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* Created by jt on 2019-07-21.
*/
@EnableAsync
@EnableScheduling
@Configuration
public class TaskConfig {

@Bean
TaskExecutor taskExecutor(){
return new SimpleAsyncTaskExecutor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

/**
* Created by jt on 2019-05-17.
*/
public interface BeerRepository extends PagingAndSortingRepository<Beer, UUID> {
public interface BeerRepository extends JpaRepository<Beer, UUID> {
Page<Beer> findAllByBeerName(String beerName, Pageable pageable);

Page<Beer> findAllByBeerStyle(BeerStyleEnum beerStyle, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package guru.springframework.msscbeerservice.services;

import guru.springframework.msscbeerservice.config.JmsConfig;
import guru.springframework.msscbeerservice.domain.Beer;
import guru.springframework.msscbeerservice.events.BrewBeerEvent;
import guru.springframework.msscbeerservice.repositories.BeerRepository;
import guru.springframework.msscbeerservice.services.inventory.BeerInventoryService;
import guru.springframework.msscbeerservice.web.mappers.BeerMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Created by jt on 2019-07-21.
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class BrewingService {
private final BeerRepository beerRepository;
private final BeerInventoryService beerInventoryService;
private final JmsTemplate jmsTemplate;
private final BeerMapper beerMapper;

@Scheduled(fixedRate = 5000) //every 5 seconds
public void checkForLowInventory(){
List<Beer> beers = beerRepository.findAll();

beers.forEach(beer -> {
Integer invQOH = beerInventoryService.getOnhandInventory(beer.getId());

log.debug("Min Onhand is: " + beer.getMinOnHand());
log.debug("Inventory is: " + invQOH);

if(beer.getMinOnHand() >= invQOH){
jmsTemplate.convertAndSend(JmsConfig.BREWING_REQUEST_QUEUE, new BrewBeerEvent(beerMapper.beerToBeerDto(beer)));
}
});

}
}

0 comments on commit 4f54b97

Please sign in to comment.