Skip to content

Commit

Permalink
Merge pull request #130 from fvaleri/master
Browse files Browse the repository at this point in the history
chapter 7 - fix rules service call
  • Loading branch information
davsclaus authored Dec 25, 2018
2 parents ff3b0db + 5e2523d commit 7106e98
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
target
activemq-data
chapter1/file-copy/data/outbox/
data/*
.idea
*.i??
.classpath
.project
.settings
.vertx
.metadata
.factorypath
.vscode
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class RecommendController {

@RequestMapping(value = "recommend", method = RequestMethod.GET, produces = "application/json")
public List<ItemDto> recommend(HttpSession session) {
String id = session.getId();
// hardcoded session id to simplify testing (you should take it from session)
String id = "123";
LOG.info("HTTP session id {}", id);

// get the current item in the shopping cart associated with the HTTP session id
Expand All @@ -43,7 +44,7 @@ public List<ItemDto> recommend(HttpSession session) {

// call rules service with the items from the shopping cart to get list of items to be recommended
LOG.info("Calling rules service {}", rulesUrl);
ItemDto[] items = restTemplate.getForObject(rulesUrl, ItemDto[].class, id, cartIds);
ItemDto[] items = restTemplate.getForObject(rulesUrl, ItemDto[].class, cartIds);
String itemIds = itemsToCommaString(items);
LOG.info("Inventory items {}", itemIds);

Expand Down Expand Up @@ -91,6 +92,9 @@ private void appendRatingToItem(RatingDto rating, ItemDto[] items) {
}

private String cartsToCommaString(CartDto[] carts) {
if (carts.length == 0) {
return "-1";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < carts.length; i++) {
CartDto cart = carts[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.converter.jaxb.JaxbDataFormat;

/**
* Camel route that calls the legacy system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public void removeItem(@Header("sessionId") String sessionId, @Header("itemId")

Set<CartDto> dtos = content.get(sessionId);
if (dtos != null) {
dtos.remove(itemId);
dtos.removeIf(i -> i.getItemId() == itemId);
}
}

public Set<CartDto> getItems(@Header("sessionId") String sessionId) {
LOG.info("getItems {}", sessionId);
Set<CartDto> answer = content.get(sessionId);
if (answer == null) {
answer = Collections.EMPTY_SET;
answer = Collections.emptySet();
}
return answer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public boolean equals(Object o) {
public int hashCode() {
return itemId.hashCode();
}

@Override
public String toString() {
return "" + itemId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public int getRating() {
public void setRating(int rating) {
this.rating = rating;
}

@Override
public String toString() {
return "" + itemNo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public List<ItemDto> recommend(HttpSession session) {
String cartIds = shoppingCart.shoppingCart(cartUrl, id);

// call rules service with the items from the shopping cart to get list of items to be recommended
ItemDto[] items = rules.rules(rulesUrl, id, cartIds);
ItemDto[] items = rules.rules(rulesUrl, cartIds);
String itemIds = itemsToCommaString(items);

// call rating service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package camelinaction;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -15,19 +18,19 @@ public class RulesService {
private final RestTemplate restTemplate = new RestTemplate();

@HystrixCommand(fallbackMethod = "standardItems")
public ItemDto[] rules(String rulesUrl, String id, String cartIds) {
public ItemDto[] rules(String rulesUrl, String cartIds) {
LOG.info("Calling rules service {}", rulesUrl);
ItemDto[] items;
// if not existing items in the shopping cart then use item 999 as special
if (cartIds == null || cartIds.isEmpty()) {
cartIds = "999";
}
items = restTemplate.getForObject(rulesUrl, ItemDto[].class, id, cartIds);
LOG.info("Inventory items {}", (Object[]) items);
items = restTemplate.getForObject(rulesUrl, ItemDto[].class, cartIds);
LOG.info("Inventory items {}", Arrays.toString(items));
return items;
}

public ItemDto[] standardItems(String rulesUrl, String id, String cartIds) {
public ItemDto[] standardItems(String rulesUrl, String cartIds) {
// a special item to use as fallback
ItemDto special = new ItemDto();
special.setItemNo(999);
Expand Down

0 comments on commit 7106e98

Please sign in to comment.