1 package org.springframework.samples.jpetstore.service.client; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 6 import org.springframework.beans.factory.ListableBeanFactory; 7 import org.springframework.context.support.FileSystemXmlApplicationContext; 8 import org.springframework.samples.jpetstore.domain.LineItem; 9 import org.springframework.samples.jpetstore.domain.Order; 10 import org.springframework.samples.jpetstore.domain.logic.OrderService; 11 import org.springframework.util.StopWatch; 12 13 29 public class OrderServiceClient { 30 31 public static final String CLIENT_CONTEXT_CONFIG_LOCATION = "clientContext.xml"; 32 33 34 private final ListableBeanFactory beanFactory; 35 36 public OrderServiceClient(ListableBeanFactory beanFactory) { 37 this.beanFactory = beanFactory; 38 } 39 40 public void invokeOrderServices(int orderId, int nrOfCalls) { 41 StopWatch stopWatch = new StopWatch(nrOfCalls + " OrderService call(s)"); 42 Map orderServices = this.beanFactory.getBeansOfType(OrderService.class); 43 for (Iterator it = orderServices.keySet().iterator(); it.hasNext();) { 44 String beanName = (String ) it.next(); 45 OrderService orderService = (OrderService) orderServices.get(beanName); 46 System.out.println("Calling OrderService '" + beanName + "' with order ID " + orderId); 47 stopWatch.start(beanName); 48 Order order = null; 49 for (int i = 0; i < nrOfCalls; i++) { 50 order = orderService.getOrder(orderId); 51 } 52 stopWatch.stop(); 53 if (order != null) { 54 printOrder(order); 55 } 56 else { 57 System.out.println("Order with ID " + orderId + " not found"); 58 } 59 System.out.println(); 60 } 61 System.out.println(stopWatch.prettyPrint()); 62 } 63 64 protected void printOrder(Order order) { 65 System.out.println("Got order with order ID " + order.getOrderId() + 66 " and order date " + order.getOrderDate()); 67 System.out.println("Shipping address is: " + order.getShipAddress1()); 68 for (Iterator lineItems = order.getLineItems().iterator(); lineItems.hasNext();) { 69 LineItem lineItem = (LineItem) lineItems.next(); 70 System.out.println("LineItem " + lineItem.getLineNumber() + ": " + lineItem.getQuantity() + 71 " piece(s) of item " + lineItem.getItemId()); 72 } 73 } 74 75 76 public static void main(String [] args) { 77 if (args.length == 0 || "".equals(args[0])) { 78 System.out.println( 79 "You need to specify an order ID and optionally a number of calls, e.g. for order ID 1000: " + 80 "'client 1000' for a single call per service or 'client 1000 10' for 10 calls each"); 81 } 82 else { 83 int orderId = Integer.parseInt(args[0]); 84 int nrOfCalls = 1; 85 if (args.length > 1 && !"".equals(args[1])) { 86 nrOfCalls = Integer.parseInt(args[1]); 87 } 88 ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext(CLIENT_CONTEXT_CONFIG_LOCATION); 89 OrderServiceClient client = new OrderServiceClient(beanFactory); 90 client.invokeOrderServices(orderId, nrOfCalls); 91 } 92 } 93 94 } 95 | Popular Tags |