# Microservice Client that connect to multiple devices

**URL:** <https://community.cumulocity.com/t/microservice-client-that-connect-to-multiple-devices/7683>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [August 14, 2022, 6:25pm UTC](https://community.cumulocity.com/t/microservice-client-that-connect-to-multiple-devices/7683 "2022-08-14T18:25:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Kai\_Sieben](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/kai_sieben/32/5541_2.png) [@Kai\_Sieben](https://community.cumulocity.com/u/Kai_Sieben)\
**Post date:** [August 15, 2022, 6:38am UTC](https://community.cumulocity.com/t/microservice-client-that-connect-to-multiple-devices/7683/2 "2022-08-15T06:38:44Z")

</div>

Hi Misja,

in case of creating new threads the tenant-context gets lost, so that the microservice does not know to which tenant to connect anymore and which credentials to use.  
However there is an easy way to run a new thread within the tenant-context again.  
Therefor you need to use the **MicroserviceSubscriptionsService** and pass the tenant into the new thread.  
Hence I would suggest the following.

1. When creating the runnable add the tenant as parameter

C8YS7Agent:

```auto
C8YS7Client myClient = new C8YS7Client(myDevice, myTenant);

```

1. When invoking the `C8YS7Agent` use the **MicroserviceSubscriptionsService**

C8YS7Client:

```auto
@Autowired
private MicroserviceSubscriptionsService subscriptionsService;

[...]
subscriptionService.runForTenant(myTenant, () -> {
	s7Device = c8yAgent.updateDevice(s7Device);
});

```

Regards Kai

---

_[View the full topic](https://community.cumulocity.com/t/microservice-client-that-connect-to-multiple-devices/7683)._
