मैंने सिंक RestTemplate और AsyncRestTemplate दोनों के प्रदर्शन का परीक्षण करने के लिए निम्न कोड लिखा था। मैंने इसे POSTMAN पर मैन्युअल रूप से कुछ बार चलाया।स्प्रिंग रेस्ट टेम्पलेट - एसिंक बनाम सिंक बाकी टेम्पलेट
हम सिर्फ इसलिए है कि हम 10 लिंक लौट सकते हैं किसी GET कॉल में 10 संदर्भों से गुजर रहे हैं:
RestTemplate - तुल्यकालिक और रिटर्न 2806ms में:
ArrayList<String> references = new ArrayList<>();
ArrayList<String> links = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
for (int i = 0; i < 10; i++) {
ResponseEntity<String> resource = restTemplate.getForEntity(references.get(i), String.class);
links.add(resource.getBody().toString());
}
RestTemplate - अतुल्यकालिक और रिटर्न में 27 9 4 एमएमएस:
//Creating a synchronizedList so that when the async resttemplate returns, there will be no concurrency issues
List<String> links = Collections.synchronizedList(new ArrayList<String>());
//CustomClientHttpRequestFactory just extends SimpleClientHttpRequestFactory but disables automatic redirects in SimpleClientHttpRequestFactory
CustomClientHttpRequestFactory customClientHttpRequestFactory = new CustomClientHttpRequestFactory();
//Setting the ThreadPoolTaskExecutor for the Async calls
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor pool = new org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(10);
pool.setWaitForTasksToCompleteOnShutdown(true);
pool.initialize();
//Setting the TaskExecutor to the ThreadPoolTaskExecutor
customClientHttpRequestFactory.setTaskExecutor(pool);
ArrayList<String> references = new ArrayList<>();
ArrayList<String> links = new ArrayList<>();
AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(customClientHttpRequestFactory);
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
for (int i = 0; i < 10; i++) {
Future<ResponseEntity<String>> resource = asyncRestTemplate.getForEntity(references.get(i), String.class);
ResponseEntity<String> entity = resource.get(); //this should start up 10 threads to get the links asynchronously
links.add(entity.getBody().toString());
}
ज्यादातर मामलों में, दोनों विधियां वास्तव में परिणामों को एक बहुत ही समान समय के साथ वापस लौटती हैं, औसत एसिंक और सिंक कॉल दोनों में 2800ms औसत।
क्या मैं कुछ गलत कर रहा हूं क्योंकि मुझे एसिंक कॉल बहुत तेज होने की उम्मीद होगी?
हाय, धन्यवाद। मुझे आश्चर्य है कि जब हम आपके कोड में ResponseEntity इकाई = future.get() कहते हैं, तो क्या यह कोड को अवरुद्ध नहीं करता है ताकि प्रतिक्रिया प्राप्त होने तक लूप आगे नहीं बढ़े? मैं उस समय में मामूली सुधार देख सकता हूं जहां कॉल 2500ms या उससे भी अधिक में आता है लेकिन यह पर्याप्त नहीं है। –
Simon
हाँ, future.get() ब्लॉक लेकिन उस बिंदु पर सभी अनुरोध पहले ही भेजे जा चुके हैं। यदि आप जेडीके 8 कॉम्प्लेटेबल फ्यूचर्स या अन्य रचना पुस्तकालय का उपयोग कर सकते हैं, तो आप कुछ और अधिक कुशल हो सकते हैं। इसे मापते समय, ध्यान रखें कि RestTemplate/AsyncRestTemplate बनाने में समय और संसाधन लगते हैं और एक बार किया जाना चाहिए (और आपके टाइमर में गिनना नहीं चाहिए) –