# Issue while fetching inventory data using cumulocity java sdk

**URL:** https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259
**Category:** Forum
**Tags:** cumulocity
**Created:** [April 19, 2023, 12:35pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259 "2023-04-19T12:35:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![anon53114791](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anon53114791](https://community.cumulocity.com/u/anon53114791)
#### Post date: [April 19, 2023, 12:35pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/1 "2023-04-19T12:35:20Z")

</div>

Facing a weird issue while fetching data from inventory using java sdk using the code :  
final var collection = inventoryApi.getManagedObjectsByFilter(filter);  
collection.get(2000).allPages().forEach(managedObject → {  
// some code  
});  
Please let me know if anyone else ever faced such issue and the way to fix this.

Error logs:

`java.lang.IllegalStateException: Client instance has been closed. at org.glassfish.jersey.internal.guava.Preconditions.checkState(Preconditions.java:169) at org.glassfish.jersey.client.JerseyClient.checkNotClosed(JerseyClient.java:257) at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:272) at com.cumulocity.sdk.client.CumulocityHttpClient.target(CumulocityHttpClient.java:33) at com.cumulocity.sdk.client.CumulocityHttpClient.target(CumulocityHttpClient.java:19) at com.cumulocity.sdk.client.RestConnector.getClientResponse(RestConnector.java:130) at com.cumulocity.sdk.client.RestConnector.get(RestConnector.java:109) at com.cumulocity.sdk.client.PagedCollectionResourceImpl.getCollection(PagedCollectionResourceImpl.java:87) at com.cumulocity.sdk.client.PagedCollectionResourceImpl.getNextPage(PagedCollectionResourceImpl.java:100) at com.cumulocity.sdk.client.PagedCollectionIterable.hasNext(PagedCollectionIterable.java:46) at java.base/java.lang.Iterable.forEach(Iterable.java:74)`

---

<div class="post-metadata">

### Author: ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)
#### Post date: [April 19, 2023, 12:42pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/2 "2023-04-19T12:42:43Z")

</div>

Haven’t faced this issue but I would assume two things

1. Are you using any proxy in between your client + C8Y?
2. Why `get(2000)`? This could take very long and might run into client timeouts. Stick with the default page-size `get()` or try a smaller number.

---

<div class="post-metadata">

### Author: ![anon53114791](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anon53114791](https://community.cumulocity.com/u/anon53114791)
#### Post date: [April 19, 2023, 12:55pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/3 "2023-04-19T12:55:20Z")

</div>

1. No proxy in between.
2. this code needs to run for my entire fleet (around 400000 devices). So I am using 2000 as chunk size

---

<div class="post-metadata">

### Author: ![hgansmueller](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hgansmueller](https://community.cumulocity.com/u/hgansmueller)
#### Post date: [April 19, 2023, 1:06pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/4 "2023-04-19T13:06:41Z")

</div>

Hi,

does your code work for first 2000 objects, without allPages?

greets

Helmut

---

<div class="post-metadata">

### Author: ![anon53114791](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anon53114791](https://community.cumulocity.com/u/anon53114791)
#### Post date: [April 19, 2023, 1:09pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/5 "2023-04-19T13:09:36Z")

</div>

> [@anon53114791](#):
>
> collection.get(2000).allPages().forEach(managedObject → {

No, I have allPages in my code :  
collection.get(2000).allPages().forEach(managedObject → {

---

<div class="post-metadata">

### Author: ![hgansmueller](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hgansmueller](https://community.cumulocity.com/u/hgansmueller)
#### Post date: [April 19, 2023, 1:21pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/6 "2023-04-19T13:21:01Z")

</div>

I meant, can you try your code without all pages. Just for the first 2000 managed objects.  
I don´t know java source code, but maybe it works internally with pages. That could be a problem with many objects. For each page request it starts with page 1. then it iterates over the managed objects until it found the requested page. At this moment he starts delivering the next 2000 manged objects. That can run into a timeout from the database. It tooks too long.

---

<div class="post-metadata">

### Author: ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)
#### Post date: [April 19, 2023, 1:27pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/7 "2023-04-19T13:27:30Z")

</div>

The fleet size doesn’t matter. The only thing that is different with 2000 page size is that you have less requests and your responses might be very big and take very long to retrieve. Having smaller page size is in most cases more efficient as it reduces the response size drastically & even scales better.

Just try the default page size and give us feedback if this resolves your issue. If not I guess you need to debug and find potential managed objects that causing timeouts.

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)
#### Post date: [October 16, 2023, 1:28pm UTC](https://community.cumulocity.com/t/issue-while-fetching-inventory-data-using-cumulocity-java-sdk/4259/8 "2023-10-16T13:28:10Z")

</div>

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