Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7473 vikram.rag 1
/******************************************************************************* 
2
 *  Copyright 2008-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
4
 *  
5
 *  You may not use this file except in compliance with the License. 
6
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
7
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
8
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the 
9
 *  specific language governing permissions and limitations under the License.
10
 * ***************************************************************************** 
11
 * 
12
 *  Marketplace Web Service Orders Java Library
13
 *  API Version: 2011-01-01
14
 * 
15
 */
16
 
17
 
18
 
19
package com.amazonservices.mws.orders.samples;
20
 
21
import java.util.List;
22
import java.util.ArrayList;
23
import com.amazonservices.mws.orders.*;
24
import com.amazonservices.mws.orders.model.*;
25
import java.util.concurrent.Future;
26
import java.util.concurrent.Executors;
27
import java.util.concurrent.ExecutorService;
28
 
29
/**
30
 *
31
 * List Orders  Samples
32
 *
33
 *
34
 */
35
public class ListOrdersAsyncSample {
36
 
37
    /**
38
     * Just add few required parameters, and try the service
39
     * List Orders functionality
40
     *
41
     * @param args unused
42
     */
43
    public static void main(String... args) {
44
 
45
         ExecutorService executor = Executors.newFixedThreadPool(100);
46
         MarketplaceWebServiceOrdersAsync service = new MarketplaceWebServiceOrdersAsyncClient(
47
					OrdersConfig.accessKeyId, 
48
					OrdersConfig.secretAccessKey, 
49
					OrdersConfig.applicationName, 
50
					OrdersConfig.applicationVersion, 
51
					OrdersConfig.config,
52
					executor);
53
 
54
        /************************************************************************
55
         * Setup requests parameters and invoke parallel processing. Of course
56
         * in real world application, there will be much more than a couple of
57
         * requests to process.
58
         ***********************************************************************/
59
         ListOrdersRequest requestOne = new ListOrdersRequest();
60
         // @TODO: set request parameters here
61
 
62
         ListOrdersRequest requestTwo = new ListOrdersRequest();
63
         // @TODO: set second request parameters here
64
 
65
         List<ListOrdersRequest> requests = new ArrayList<ListOrdersRequest>();
66
         requests.add(requestOne);
67
         requests.add(requestTwo);
68
 
69
         invokeListOrders(service, requests);
70
 
71
         executor.shutdown();
72
 
73
    }
74
 
75
 
76
 
77
    /**
78
     * List Orders request sample
79
     * ListOrders can be used to find orders that meet the specified criteria.
80
     *   
81
     * @param service instance of MarketplaceWebServiceOrders service
82
     * @param requests list of requests to process
83
     */
84
    public static void invokeListOrders(MarketplaceWebServiceOrdersAsync service, List<ListOrdersRequest> requests) {
85
        List<Future<ListOrdersResponse>> responses = new ArrayList<Future<ListOrdersResponse>>();
86
        for (ListOrdersRequest request : requests) {
87
            responses.add(service.listOrdersAsync(request));
88
        }
89
        for (Future<ListOrdersResponse> future : responses) {
90
            while (!future.isDone()) {
91
                Thread.yield();
92
            }
93
            try {
94
                ListOrdersResponse response = future.get();
95
                // Original request corresponding to this response, if needed:
96
                ListOrdersRequest originalRequest = requests.get(responses.indexOf(future));
97
                System.out.println("Response request id: " + response.getResponseMetadata().getRequestId());
98
            } catch (Exception e) {
99
                if (e.getCause() instanceof MarketplaceWebServiceOrdersException) {
100
                    MarketplaceWebServiceOrdersException exception = MarketplaceWebServiceOrdersException.class.cast(e.getCause());
101
                    System.out.println("Caught Exception: " + exception.getMessage());
102
                    System.out.println("Response Status Code: " + exception.getStatusCode());
103
                    System.out.println("Error Code: " + exception.getErrorCode());
104
                    System.out.println("Error Type: " + exception.getErrorType());
105
                    System.out.println("Request ID: " + exception.getRequestId());
106
                    System.out.println("XML: " + exception.getXML());
107
                    System.out.print("ResponseHeaderMetadata: " + exception.getResponseHeaderMetadata());
108
                } else {
109
                    e.printStackTrace();
110
                }
111
            }
112
        }
113
    }
114
 
115
}