# Accessing session values inside regular method using session API

**URL:** <https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466>\
**Category:** Gatling (Open-Source)\
**Created:** [September 25, 2014, 4:55am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466 "2014-09-25T04:55:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akumar](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@Akumar](https://community.gatling.io/u/Akumar)\
**Post date:** [September 25, 2014, 4:55am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/1 "2014-09-25T04:55:43Z")

</div>

Hi,

I’m struggling to make use of the session API for retrieving the session values inside normal function within the feed() scope. I tried the below ways but no luck.

Feed(reqFeed)  
.exec(session =\> session.set(“start\_time”,System.nonoTime)  
.exec(new myAction(“test”, “${query}”))  
.exec(session =\> session.set(“req\_duration”,"System.nonoTime)

when I run the above snippet, the parameter ${query} is not substituted but I can see that value stored in a session. I tried the the following ways:

val s: Session = ???  
.exec(new myAction(“test”, s.attributes.seq.apply(“query”).toString)

If I use this session API, I get the error " Exception in thread “main” Scala.NotImplementedError : An implementation is missing."

if I use this way,

exec((s: Session) =\> {  
new myAction(“test”,s.attributes.seq.apply(“query”.toString))  
session  
})

I get no errors but it reports that " there were no requests sent during the simulation . No reports get generated" message.

So I need a way to access the session attributes inside the regular function. Appreciate your quick help on this. Thank you

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [September 25, 2014, 6:41am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/2 "2014-09-25T06:41:23Z")

</div>

What is myAction doing?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 25, 2014, 6:58am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/3 "2014-09-25T06:58:51Z")

</div>

Substitution only happens when required by the compiler.  
Please read the documentation: [http://gatling.io/docs/2.0.0-RC5/session/expression\_el.html#expression-language](http://gatling.io/docs/2.0.0-RC5/session/expression_el.html#expression-language)

Session API is also documented: [http://gatling.io/docs/2.0.0-RC5/session/session\_api.html](http://gatling.io/docs/2.0.0-RC5/session/session_api.html)

---

<div class="post-metadata">

**Author:** ![Akumar](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@Akumar](https://community.gatling.io/u/Akumar)\
**Post date:** [September 25, 2014, 7:17am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/4 "2014-09-25T07:17:24Z")

</div>

Hi Stephane,

Thanks for reply. I have gone through the documentation but seems I’m not clear on this. Can you please tell me some example on this. Thanks.

---

<div class="post-metadata">

**Author:** ![Akumar](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@Akumar](https://community.gatling.io/u/Akumar)\
**Post date:** [September 25, 2014, 7:23am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/5 "2014-09-25T07:23:30Z")

</div>

Hi John,

myAction is a plain java class which actually run some db script. This block of code is part of scenarioBuilder

def function1 : ChainBuilder  
{

Feed(reqFeed)  
.exec(session =\> session.set(“start\_time”,System.nonoTime)  
.exec(new myAction(“test”, “${query}”))  
.exec(session =\> session.set(“req\_duration”,"System.nonoTime)  
}

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 25, 2014, 7:47am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/6 "2014-09-25T07:47:25Z")

</div>

Have your myAction take an Expression[String] instead of a String.

---

<div class="post-metadata">

**Author:** ![Akumar](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@Akumar](https://community.gatling.io/u/Akumar)\
**Post date:** [September 25, 2014, 7:52am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/7 "2014-09-25T07:52:38Z")

</div>

No it is defined as String param. What is Expression[String]?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [September 25, 2014, 8:03am UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/8 "2014-09-25T08:03:07Z")

</div>

You REALLY have to read the documentation: [http://gatling.io/docs/2.0.0-RC5/session/expression\_el.html](http://gatling.io/docs/2.0.0-RC5/session/expression_el.html)

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [September 26, 2014, 2:07pm UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/9 "2014-09-26T14:07:07Z")

</div>

Does myAction do the database action immediately when new is called, as part of the constructor? Or does it return an object which gets called at run time when it is time to do the action?

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [September 26, 2014, 2:20pm UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/10 "2014-09-26T14:20:51Z")

</div>

@John : As it is an Action, it will only take place when it is time to do the action.  
Meaning this could block the WHOLE scenario, for all users, which is a really, really bad idea…  
Blocking operations need to be handled very carefully when working with Akka 🙂

---

<div class="post-metadata">

**Author:** ![Akumar](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@Akumar](https://community.gatling.io/u/Akumar)\
**Post date:** [September 26, 2014, 4:11pm UTC](https://community.gatling.io/t/accessing-session-values-inside-regular-method-using-session-api/1466/11 "2014-09-26T16:11:19Z")

</div>

Hi John, Pierre,  
Thanks for your update. MyAction is actually an extension of Action Builder. Its a custom implementation of actor like similar to http protocol. I can make it work without using feeder & sessions, but using for loop to construct the scenarioBuiler.

I think either I’m missing something or doing wrong when used feeders/sessions to buildup chain builder.

I really don’t understand how to use the session api in this case. Appreciate if you can show some light on this.
