KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > jpetstore > service > client > OrderServiceClient


1 package org.springframework.samples.jpetstore.service.client;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
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 /**
14  * Demo client class for remote OrderServices, to be invoked as standalone
15  * program from the command line, e.g. via "client.bat" or "run.xml".
16  *
17  * <p>You need to specify an order ID and optionally a number of calls,
18  * e.g. for order ID 1000: 'client 1000' for a single call per service or
19  * 'client 1000 10' for 10 calls each".
20  *
21  * <p>Reads in the application context from a "clientContext.xml" file in
22  * the VM execution directory, calling all OrderService proxies defined in it.
23  * See that file for details.
24  *
25  * @author Juergen Hoeller
26  * @since 26.12.2003
27  * @see org.springframework.samples.jpetstore.domain.logic.OrderService
28  */

29 public class OrderServiceClient {
30
31     public static final String JavaDoc 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 JavaDoc orderServices = this.beanFactory.getBeansOfType(OrderService.class);
43         for (Iterator JavaDoc it = orderServices.keySet().iterator(); it.hasNext();) {
44             String JavaDoc beanName = (String JavaDoc) 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 JavaDoc 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 JavaDoc[] 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