# Java REST Client and Multithreading

**URL:** <https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298>\
**Category:** Elasticsearch\
**Created:** [March 9, 2018, 3:59pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298 "2018-03-09T15:59:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [March 9, 2018, 3:59pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/1 "2018-03-09T15:59:14Z")

</div>

Is the Java REST Client Thread safe?

Can I instantiate a single client object and share among the threads?  
Or should I open separate connections in each thread?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 9, 2018, 4:15pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/2 "2018-03-09T16:15:46Z")

</div>

One client only is fine IMO.

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [March 9, 2018, 4:22pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/3 "2018-03-09T16:22:50Z")

</div>

7000 records are taking forever to reach Elastic 😥

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 9, 2018, 4:46pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/4 "2018-03-09T16:46:12Z")

</div>

But what are you doing?

Calling index() 7000 times? If so, that's not the best solution here.  
But please be more specific about what your problem is.

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [March 9, 2018, 4:54pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/5 "2018-03-09T16:54:32Z")

</div>

I'm looping, the reason is, the AWS instance I was using kept running out of memory while uploading to Hubspot. Keeping the objects in memory was the problem, and very little RAM.

So I decided to serialize the objects then send to Elastic. This is why I didn't use the BULK API in the first place.

I've since increased the RAM from 2 GB to 8GB, so maybe I can revisit the BULK API.

```
public static void insertCDSData(HubspotContact registrant) {
	
	CDSRegImport_ES_Credentials credentials = new CDSRegImport_ES_Credentials();
	RestHighLevelClient client = credentials.getClient();
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	
		
		
		IndexRequest indexRequest = new IndexRequest("cdsregimport", "CDSREGIMPORT", registrant.getBadge()).
				source("firstname",registrant.getFirstName(), 
						"lastname", registrant.getLastName(),
						"phone",registrant.getPhone(),
						"ATS ID",registrant.getATSID(),
						"email",registrant.getEmail(),
						"company",registrant.getCompany(),
						"degree",registrant.getDegreeDesignation(),
						"Address 1",registrant.getMailStreet1(),
						"Address 2", registrant.getMailStreet2(),
						"City",registrant.getMailCity(),
						"State",registrant.getMailState(),
						"Country", registrant.getMailCountry(),
						"Registration Type", registrant.getRegistrationType(),
						"Title", registrant.getTitle(),
						"Topics of Interest",registrant.getTopicOfInterest(),
						"CDS Address Type",registrant.getAddressType(),
						"Work Settings", registrant.getWorkSettings(),
						"Nature of Professional Activities", registrant.getNatureOfProfessionalWork(),
						"zipcode",registrant.getMailZip(),
						"postDate", new Date());
		UpdateRequest upR = new UpdateRequest("cdsregimport", "CDSREGIMPORT", String.valueOf(registrant.getBadge())).doc(indexRequest)
				.upsert(indexRequest);
		UpdateResponse updateResponse = null;
		
		try{
			updateResponse = client.update(upR);
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
		upR = null;
		registrant = null;
		indexRequest = null;
	
	
	credentials.closeConnection();

}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 9, 2018, 5:34pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/6 "2018-03-09T17:34:46Z")

</div>

Definitely the bulk processor will help you here.

BTW did you look at [cloud.elastic.co](http://cloud.elastic.co)? It runs also on AWS.

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [March 9, 2018, 5:36pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/7 "2018-03-09T17:36:41Z")

</div>

AWS is way too expensive for ES.  
Other services, yes, ES, no.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 6, 2018, 6:10pm UTC](https://discuss.elastic.co/t/java-rest-client-and-multithreading/123298/9 "2018-04-06T18:10:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
