# HTTP requests not sent from Java using exec(session -\>{})

**URL:** <https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658>\
**Category:** Gatling (Open-Source)\
**Created:** [February 22, 2023, 8:09pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658 "2023-02-22T20:09:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://community.gatling.io/u/JohnGreen)\
**Post date:** [February 22, 2023, 8:09pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/1 "2023-02-22T20:09:44Z")

</div>

This seems really simple and I have validated that the requests are never sent via Wireshark.  
This DOES NOT send an HTTP request

```java
.exec(
  session -> {
    try {
      http("Send [STORE]_CURRENT_STATE")
        .post("http://localhost:11640/messages")
        .headers(dashboardEventHeaders)
        .header("MESSAGE_TYPE", "#{storeId}_STORE_UPDATED")
        .body(StringBody(buildStoreUpdatedEvent()))
        .check(regex("Sent"))
        ;
      } catch (Exception e) {
        e.printStackTrace();
      }
      return session;
    }
  )
  .pause(TEN_SECONDS)
  .injectOpen(atOnceUsers(1))

```

No Exception is printed

This DOES (Identical except I have commented out the session)

```java
.exec(
// .session -> {
// try {
        http("Send [STORE]_CURRENT_STATE")
          .post("http://localhost:11640/messages")
          .headers(dashboardEventHeaders)
          .header("MESSAGE_TYPE", "#{storeId}_STORE_UPDATED")
          .body(StringBody(buildStoreUpdatedEvent()))
          .check(regex("Sent"))
// ;
// } catch (Exception e) {
// e.printStackTrace();
// }
// return session;
// }
  )
  .pause(TEN_SECONDS)
  .injectOpen(atOnceUsers(1))

```

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [February 22, 2023, 8:30pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/2 "2023-02-22T20:30:17Z")

</div>

Indeed, and it’s clearly stated that this doesn’t work in the [documentation](https://gatling.io/docs/gatling/reference/current/core/scenario/#exec) for `exec`:

 ![Screenshot 2023-02-22 at 21.27.07](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/5/5b536673bb7e8fd6c2e38e372b9216f6df4bb705.png)

Now if what you’re trying to achieve is to make it so that `buildStoreUpdatedEvent()` is called every time a request is generated:

```java
.exec(
  http("Send [STORE]_CURRENT_STATE")
    .post("http://localhost:11640/messages")
    .headers(dashboardEventHeaders)
    .header("MESSAGE_TYPE", "#{storeId}_STORE_UPDATED")
    .body(StringBody(session -> buildStoreUpdatedEvent())) // <== pass a function here
    .check(regex("Sent"))
  )
  .pause(TEN_SECONDS)
  .injectOpen(atOnceUsers(1))

```

---

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://community.gatling.io/u/JohnGreen)\
**Post date:** [February 22, 2023, 8:59pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/3 "2023-02-22T20:59:05Z")

</div>

I guess I didn’t quite comprehend that comment correctly, this was part of a chain but probably not in the right way (need to get the execution model installed in my brain)  
I worked around it in another way, but I like your solution better and will give that a try.  
Thanks for the quick response !

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [February 23, 2023, 9:18am UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/4 "2023-02-23T09:18:33Z")

</div>

> I guess I didn’t quite comprehend that comment correctly, this was part of a chain but probably not in the right way (need to get the execution model installed in my brain)

We’d be very keen on having your feedback on how we could explain things better and improve our documentation. Also, there’s an “Edit this page on GitHub” at the bottom of each page to contribute documentation improvements.

Cheers

---

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://community.gatling.io/u/JohnGreen)\
**Post date:** [February 23, 2023, 6:20pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/5 "2023-02-23T18:20:05Z")

</div>

So I think I am getting a better model in my head now and I want to confirm my understanding with you.  
Because of the reactive architecture, execution and specification must remain separate.  
The session holds a map of vars that must be accessed atomically, so mixing state change and execution doesn’t work.  
So if you want to mutate anything during a run it must be done first via exec(s-\> f(s)), then those values can be used in an action definition via “#{var}” replacements.  
This makes it difficult to use object methods within a ActionBuilder without a lot of plumbing or am I missing something.

---

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://community.gatling.io/u/JohnGreen)\
**Post date:** [February 23, 2023, 7:12pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/6 "2023-02-23T19:12:53Z")

</div>

Still not getting something  
Here sendDriverOnRun (Http Action to send to rest client) faults because startDriverDto is not found, that’s why I commented it out and replaced it with a debug print.

```java
scenario("Drivers going on runs")
  .feed(driverFeeder)
  .exec(session -> buildDriverDto(session))
// .exec(sendDriverOnRun()) 
  .exec(session -> {
    System.out.println("--- From Session ---");
    System.out.println(session.getString("startDriverDto"));
    System.out.println("------");
    return session;
  })
  .injectOpen(atOnceUsers(1));

private Session buildDriverDto(Session session) {
  DriverDto driver = session.get("driver");
  String s = buildDriverPayload(driver);
  session.set("startDriverDto",s);
  System.out.println("--- Driver DTO ---");
  System.out.println(s);
  System.out.println("-----");
 return session;
 }

```

Output from a run is

```auto
--- Driver DTO ---
{
  "driverId":123,
..... JSON payload ...
  "sequence" : 1
}
-----
--- From Session ---
null
------

```

Just can’t see what I am doing wrong here

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [February 23, 2023, 8:10pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/7 "2023-02-23T20:10:45Z")

</div>

> Because of the reactive architecture, execution and specification must remain separate.

Exactly

> The session holds a map of vars that must be accessed atomically, so mixing state change and execution doesn’t work.

It’s more that functions are executed at runtime, way after the specification is built.

> So if you want to mutate anything during a run it must be done first via exec(s-\> f(s)), then those values can be used in an action definition via “#{var}” replacements.

That (first store, then fetch), or directly pass functions to individual methods.

> This makes it difficult to use object methods within a ActionBuilder without a lot of plumbing or am I missing something.

Sorry, don’t understand what you mean here.

> faults because startDriverDto is not found

Because Session is **immutable** (it’s the easiest way to deal with state in an asynchronous architecture, but something we’re considering changing for Gatling 4), see [doc](https://gatling.io/docs/gatling/reference/current/core/session/session_api/#setting-attributes):

 ![Screenshot 2023-02-23 at 21.04.45](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/9/90897f15e282de127446f4370ffa1d4e0cbfbbdc.png)

So;

```java
private Session buildDriverDto(Session session) {
  DriverDto driver = session.get("driver");
  String s = buildDriverPayload(driver);
  Session newSession.set("startDriverDto",s);
  System.out.println("--- Driver DTO ---");
  System.out.println(s);
  System.out.println("-----");
 return newSession;
 }

```

or shorter:

```java
private Session buildDriverDto(Session session) {
  return session.set("startDriverDto", buildDriverPayload(driver));
 }

```

Still, I don’t understand why you don’t directly build everything you need in `sendDriverOnRun()`.

---

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://community.gatling.io/u/JohnGreen)\
**Post date:** [February 24, 2023, 1:32pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/8 "2023-02-24T13:32:47Z")

</div>

Im not building things directly because this test is getting complex (Maybe not the best one to start out with, but it means I have to learn fast) this has an init process, then concurrent scenarios of orders arriving, someone working a UI and drivers delivering orders so keeping the main script legible is important.  
Also, at this point I am just trying to get it working before refactoring (probably today as I think I am pretty close) for a demo next Thursday  
I didn’t see that section on the session, that explains a lot for me.  
Expected an immutable to throw exceptions on a set(), instead of returning a new instance, thats more the builder pattern.

Probably a good idea for me to scan the docs and read those warning blocks carefully !

You have been very helpful, and so far I am pretty pleased with the product, thank you

---

<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:** [March 26, 2023, 1:33pm UTC](https://community.gatling.io/t/http-requests-not-sent-from-java-using-exec-session/7658/9 "2023-03-26T13:33:38Z")

</div>

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