# Token Refresh - Java - Example

**URL:** <https://community.gatling.io/t/token-refresh-java-example/7935>\
**Category:** Gatling (Open-Source)\
**Created:** [May 10, 2023, 10:03pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935 "2023-05-10T22:03:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 10, 2023, 10:03pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935/1 "2023-05-10T22:03:08Z")

</div>

I see that lot of people asking about solution to refresh Token when expired.  
I have prepared Case0022SetOrRefreshTokenSimulation where Token is refreshed every some period of time.

What you think about this solution @slandelle @sbrevet ?

> <https://github.com/gemiusz/gatling-examples-maven-java/blob/master/src/test/java/pl/gemiusz/Case0022SetOrRefreshTokenSimulation.java>

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [May 11, 2023, 8:00am UTC](https://community.gatling.io/t/token-refresh-java-example/7935/2 "2023-05-11T08:00:03Z")

</div>

Hi @GeMi,

Thanks for this code suggestion! ❤

Here my comments:

- Create a pull request: it is easier to comment and discuss about code there.
- I think it needs a disclaimer that is for a single load generator (in Gatling Enterprise, you can launch a single run with multiple load generators, but global variables are trickier to manage)
- I’m on two minds about the `scnForSetOrRefreshToken`:  
Perhaps a scenario with a single user that loops with a pause?
  - In one hand, your code doesn’t need to have a max duration
  - In the other, it’s complicated to pre-compute the amount of requests this way

- A small comment next to `nothingFor(2)` : “waiting for the first credentials”

Note:

- Avoid string injection in the URL by using `queryParam` (personal taste)

Cheers!

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 11, 2023, 8:15pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935/3 "2023-05-11T20:15:12Z")

</div>

Hi @sbrevet,  
Thank you for your valuable suggestions ♥  
I have make huge changes in code including your suggerstions 🙂  
If it is possible please check my PR → [Case0022SetOrRefreshTokenSimulation in new version by gemiusz · Pull Request #17 · gemiusz/gatling-examples-maven-java · GitHub](https://github.com/gemiusz/gatling-examples-maven-java/pull/17)

In few words what it’s going in Simulation now:

- I changed injection profile to have dependencies on necessary steps: first get token → token refresh loop AND Core Scenario → after finish my simulation “shut down” token refresh loop.
- Simulation time = time of getting first token + Core Scenario time + 1 second (shut down token refresh loop)
- Don’t need set any tricky counters etc.
- Set `queryParam` ☺
- Silent request for token - that was from beginning but I want to mention about that here

Cheers!

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [May 12, 2023, 7:12am UTC](https://community.gatling.io/t/token-refresh-java-example/7935/4 "2023-05-12T07:12:54Z")

</div>

Hi @GeMi,

Nice improvements!

Cheers!

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 12, 2023, 10:05am UTC](https://community.gatling.io/t/token-refresh-java-example/7935/5 "2023-05-12T10:05:21Z")

</div>

@sbrevet thank you for review and comments in PR.  
I will look closely at them at Sunday afternoon because I’m at trip 🙂

Cheers!

---

<div class="post-metadata">

**Author:** ![Banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Banu](https://community.gatling.io/u/Banu)\
**Post date:** [June 7, 2023, 4:23pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935/6 "2023-06-07T16:23:24Z")

</div>

> [@GeMi](#):
>
> Case0022SetOrRefreshTokenSimulation in new version by gemiusz · Pull Request #17 · gemiusz/gatling-examples-maven-java · GitHub

Hi @GeMi Is it possible to provide the solution in Scala as well? Thank you in advance!

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [June 9, 2023, 1:30pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935/7 "2023-06-09T13:30:19Z")

</div>

Sorry but not.  
I’m not familiar with Scala.

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [July 9, 2023, 1:30pm UTC](https://community.gatling.io/t/token-refresh-java-example/7935/8 "2023-07-09T13:30:25Z")

</div>

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