# Temporal Workers polling from a queue

**URL:** https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515
**Category:** Community Support
**Created:** [December 4, 2024, 9:21am UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515 "2024-12-04T09:21:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Adrian\_Puscuta](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/adrian_puscuta/32/6341_2.png) [@Adrian\_Puscuta](https://community.temporal.io/u/Adrian_Puscuta)
#### Post date: [December 4, 2024, 9:21am UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515/1 "2024-12-04T09:21:50Z")

</div>

Im designing a system using the java sdk where I have different temporal workers in different locations listening to a certain task pool. When a workflow is started, as the first workflow activity each worker polls a message queue which responds with data based on the worker’s location and then the worker processes it. The second activity is receiving the processed data and sends it to DB. What is the best way to make the workers continually poll the message queue? I considered having the workflow activities in a while loop, but in that case when a certain worker exits the loop, temporal considers the workflow complete and stops all workers.

---

<div class="post-metadata">

### Author: ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)
#### Post date: [December 4, 2024, 12:01pm UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515/2 "2024-12-04T12:01:11Z")

</div>

Looks like you need to route all activities to same worker, for that can take a look at [fileprocessing](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/fileprocessing) sample.

> but in that case when a certain worker exits the loop, temporal considers the workflow complete and stops all workers.

note sure i fully understand the “and stops all workers” part, can you give more info please?

---

<div class="post-metadata">

### Author: ![Adrian\_Puscuta](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/adrian_puscuta/32/6341_2.png) [@Adrian\_Puscuta](https://community.temporal.io/u/Adrian_Puscuta)
#### Post date: [December 4, 2024, 12:06pm UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515/3 "2024-12-04T12:06:46Z")

</div>

To test I have 2 instances of worker started. When I call WorkflowClient.start(), both workers start consuming from the queue, as expected. I encounter situations when the queue has no more messages for one of the workers, but still has messages for the other. The worker for which there are no more messages exits the while loop and the workflow is marked complete, therefore the second worker also stops.

---

<div class="post-metadata">

### Author: ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)
#### Post date: [December 4, 2024, 12:48pm UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515/4 "2024-12-04T12:48:43Z")

</div>

Could you share your code where you create workers and start executions (and this loop you mention).

> When I call WorkflowClient.start(), both workers start consuming from the queue, as expected

workers start polling as soon as you start the WorkerFactory. you can stop your workers by calling `shutdown` method on the worker factory.

> The worker for which there are no more messages exits the while loop

would like to understand this better please, not sure why while loop is needed and what it does. thanks.

---

<div class="post-metadata">

### Author: ![Adrian\_Puscuta](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/adrian_puscuta/32/6341_2.png) [@Adrian\_Puscuta](https://community.temporal.io/u/Adrian_Puscuta)
#### Post date: [December 4, 2024, 1:23pm UTC](https://community.temporal.io/t/temporal-workers-polling-from-a-queue/15515/5 "2024-12-04T13:23:45Z")

</div>

Cant share code. Can share pseudocode 🙂

This is the workflow implementation that is ran by both workers.

```auto
Activity One {
 HTTP Post Request to Server to get worker specific data
 Process Data
}

Activity Two {
 Send Processed Data to DB
}

Workflow {
 while(true) {
 ActivityOne
 if(processedData.notEmpty) {
 ActivityTwo}
else{
break
}

```
