| 8224 |
manish.sha |
1 |
/*******************************************************************************
|
|
|
2 |
* Copyright 2009 Amazon Services.
|
|
|
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 Java Library
|
|
|
13 |
* API Version: 2009-01-01
|
|
|
14 |
* Generated: Wed Feb 18 13:28:48 PST 2009
|
|
|
15 |
*
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
package com.amazonaws.mws.samples;
|
|
|
21 |
|
|
|
22 |
import java.io.ByteArrayInputStream;
|
|
|
23 |
import java.util.List;
|
|
|
24 |
import java.util.ArrayList;
|
|
|
25 |
import com.amazonaws.mws.*;
|
|
|
26 |
import com.amazonaws.mws.model.*;
|
|
|
27 |
import java.util.concurrent.Future;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
*
|
|
|
31 |
* Submit Feed Samples
|
|
|
32 |
*
|
|
|
33 |
*
|
|
|
34 |
*/
|
|
|
35 |
public class SubmitFeedAsyncSample {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Just add a few required parameters, and try the service
|
|
|
39 |
* Submit Feed functionality
|
|
|
40 |
*
|
|
|
41 |
* @param args unused
|
|
|
42 |
*/
|
|
|
43 |
public static void main(String... args) {
|
|
|
44 |
|
|
|
45 |
/************************************************************************
|
|
|
46 |
* Access Key ID and Secret Access Key ID, obtained from:
|
|
|
47 |
* http://aws.amazon.com
|
|
|
48 |
***********************************************************************/
|
|
|
49 |
final String accessKeyId = "<Your Access Key ID>";
|
|
|
50 |
final String secretAccessKey = "<Your Secret Access Key>";
|
|
|
51 |
|
|
|
52 |
final String appName = "<Your Application or Company Name>";
|
|
|
53 |
final String appVersion = "<Your Application Version or Build Number or Release Date>";
|
|
|
54 |
|
|
|
55 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
56 |
|
|
|
57 |
/************************************************************************
|
|
|
58 |
* Uncomment to set the appropriate MWS endpoint.
|
|
|
59 |
************************************************************************/
|
|
|
60 |
// US
|
|
|
61 |
// config.setServiceURL("https://mws.amazonservices.com");
|
|
|
62 |
// UK
|
|
|
63 |
// config.setServiceURL("https://mws.amazonservices.co.uk");
|
|
|
64 |
// Germany
|
|
|
65 |
// config.setServiceURL("https://mws.amazonservices.de");
|
|
|
66 |
// France
|
|
|
67 |
// config.setServiceURL("https://mws.amazonservices.fr");
|
|
|
68 |
// Italy
|
|
|
69 |
// config.setServiceURL("https://mws.amazonservices.it");
|
|
|
70 |
// Japan
|
|
|
71 |
// config.setServiceURL("https://mws.amazonservices.jp");
|
|
|
72 |
// China
|
|
|
73 |
// config.setServiceURL("https://mws.amazonservices.com.cn");
|
|
|
74 |
// Canada
|
|
|
75 |
// config.setServiceURL("https://mws.amazonservices.ca");
|
|
|
76 |
// India
|
|
|
77 |
// config.setServiceURL("https://mws.amazonservices.in");
|
|
|
78 |
|
|
|
79 |
/************************************************************************
|
|
|
80 |
* The argument (35) set below is the number of threads client should
|
|
|
81 |
* spawn for processing.
|
|
|
82 |
***********************************************************************/
|
|
|
83 |
|
|
|
84 |
config.setMaxAsyncThreads(35);
|
|
|
85 |
|
|
|
86 |
/************************************************************************
|
|
|
87 |
* You can also try advanced configuration options. Available options are:
|
|
|
88 |
*
|
|
|
89 |
* - Signature Version
|
|
|
90 |
* - Proxy Host and Proxy Port
|
|
|
91 |
* - User Agent String to be sent to Marketplace Web Service
|
|
|
92 |
*
|
|
|
93 |
***********************************************************************/
|
|
|
94 |
|
|
|
95 |
/************************************************************************
|
|
|
96 |
* Instantiate Http Client Implementation of Marketplace Web Service
|
|
|
97 |
***********************************************************************/
|
|
|
98 |
|
|
|
99 |
MarketplaceWebService service = new MarketplaceWebServiceClient(
|
|
|
100 |
accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
101 |
|
|
|
102 |
/************************************************************************
|
|
|
103 |
* Setup requests parameters and invoke parallel processing. Of course
|
|
|
104 |
* in real world application, there will be much more than a couple of
|
|
|
105 |
* requests to process.
|
|
|
106 |
***********************************************************************/
|
|
|
107 |
|
|
|
108 |
/************************************************************************
|
|
|
109 |
* Marketplace and Merchant IDs are required parameters for all
|
|
|
110 |
* Marketplace Web Service calls.
|
|
|
111 |
***********************************************************************/
|
|
|
112 |
final String merchantId = "<Your Merchant ID>";
|
|
|
113 |
|
|
|
114 |
SubmitFeedRequest requestOne = new SubmitFeedRequest();
|
|
|
115 |
requestOne.setMerchant( merchantId );
|
|
|
116 |
|
|
|
117 |
requestOne.setFeedType( "<Feed Type>" );
|
|
|
118 |
|
|
|
119 |
// MWS exclusively offers a streaming interface for uploading your feeds. This is because
|
|
|
120 |
// feed sizes can grow past the 1GB range - and as your business grows you could otherwise
|
|
|
121 |
// silently reach the feed size where your in-memory solution will no longer work, leaving you
|
|
|
122 |
// puzzled as to why a solution that worked for a long time suddenly stopped working though
|
|
|
123 |
// you made no changes. For the same reason, we strongly encourage you to generate your feeds to
|
|
|
124 |
// local disk then upload them directly from disk to MWS via Java - without buffering them in Java
|
|
|
125 |
// memory in their entirety.
|
|
|
126 |
//
|
|
|
127 |
// requestOne.setFeedContent( new FileInputStream("my-feed-1.xml" /*or "my-flat-file-1.txt" if you use flat files*/);
|
|
|
128 |
|
|
|
129 |
SubmitFeedRequest requestTwo = new SubmitFeedRequest();
|
|
|
130 |
requestTwo.setMerchant( merchantId );
|
|
|
131 |
|
|
|
132 |
requestTwo.setFeedType( "<Feed Type>" );
|
|
|
133 |
|
|
|
134 |
// requestTwo.setFeedContent( new FileInputStream("my-feed-2.xml" /*or "my-flat-file-2.txt" if you use flat files*/);
|
|
|
135 |
|
|
|
136 |
List<SubmitFeedRequest> requests = new ArrayList<SubmitFeedRequest>();
|
|
|
137 |
requests.add(requestOne);
|
|
|
138 |
requests.add(requestTwo);
|
|
|
139 |
|
|
|
140 |
// invokeSubmitFeed(service, requests);
|
|
|
141 |
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Submit Feed request sample
|
|
|
148 |
* Uploads a file for processing together with the necessary
|
|
|
149 |
* metadata to process the file, such as which type of feed it is.
|
|
|
150 |
* PurgeAndReplace if true means that your existing e.g. inventory is
|
|
|
151 |
* wiped out and replace with the contents of this feed - use with
|
|
|
152 |
* caution (the default is false).
|
|
|
153 |
*
|
|
|
154 |
* @param service instance of MarketplaceWebService service
|
|
|
155 |
* @param requests list of requests to process
|
|
|
156 |
*/
|
|
|
157 |
public static void invokeSubmitFeed(MarketplaceWebService service, List<SubmitFeedRequest> requests) {
|
|
|
158 |
List<Future<SubmitFeedResponse>> responses = new ArrayList<Future<SubmitFeedResponse>>();
|
|
|
159 |
for (SubmitFeedRequest request : requests) {
|
|
|
160 |
responses.add(service.submitFeedAsync(request));
|
|
|
161 |
}
|
|
|
162 |
for (Future<SubmitFeedResponse> future : responses) {
|
|
|
163 |
while (!future.isDone()) {
|
|
|
164 |
Thread.yield();
|
|
|
165 |
}
|
|
|
166 |
try {
|
|
|
167 |
SubmitFeedResponse response = future.get();
|
|
|
168 |
// Original request corresponding to this response, if needed:
|
|
|
169 |
SubmitFeedRequest originalRequest = requests.get(responses.indexOf(future));
|
|
|
170 |
System.out.println("Response request id: " + response.getResponseMetadata().getRequestId());
|
|
|
171 |
System.out.println(response.getResponseHeaderMetadata());
|
|
|
172 |
System.out.println();
|
|
|
173 |
} catch (Exception e) {
|
|
|
174 |
if (e.getCause() instanceof MarketplaceWebServiceException) {
|
|
|
175 |
MarketplaceWebServiceException exception = MarketplaceWebServiceException.class.cast(e.getCause());
|
|
|
176 |
System.out.println("Caught Exception: " + exception.getMessage());
|
|
|
177 |
System.out.println("Response Status Code: " + exception.getStatusCode());
|
|
|
178 |
System.out.println("Error Code: " + exception.getErrorCode());
|
|
|
179 |
System.out.println("Error Type: " + exception.getErrorType());
|
|
|
180 |
System.out.println("Request ID: " + exception.getRequestId());
|
|
|
181 |
System.out.print("XML: " + exception.getXML());
|
|
|
182 |
System.out.println("ResponseHeaderMetadata: " + exception.getResponseHeaderMetadata());
|
|
|
183 |
} else {
|
|
|
184 |
e.printStackTrace();
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
}
|