How to persist session when passing to class/object | Community
Skip to main content
TheBigRed
New Participant
December 2, 2015
Solved

How to persist session when passing to class/object

  • December 2, 2015
  • 4 replies
  • 986 views

Once session is live I'm trying to pass it to class

which needs to use session object for computing some data. I can

see that the session is not persisting when being passed to an object. How can I make sure that

the session persists. Or is the solution that I have to reconnect the session

within the class?

 

 

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

"I want to get the paths from a different class which processes the query"

Ok - i see what you are doing. I have never tired this - I have always developed QueryBuilder App Logic using  different methods in the same class - not different classes. 

You can try passing the QueryBuilder Object itself -- for example - 

Servlet Class{

@Reference
private QueryBuilder builder;

@Reference
private ServiceA serA;

@Reference
private ServiceB servB;

@Reference
private ServiceC servC;

doGet

{

serA.getPathA(builder); 

serB.getPathB(builder); 

serC.getPathC(builder); 

}

}

Assuming:

ServiceA - expose method getPathA(QueryBuilder qb)

ServiceB - expose method getPathB(QueryBuilder qb)

ServiceC - expose method getPathB(QueryBuilder qb)

I have never tried this. Since QueryBuilder is a managed object and was created with Dependency Injection, not sure if this will work in other classes or if you will have to re-create it in each service.

Try that and see what happens. I am also assuming that the other classes are also using @Service annotations.  

Like I mentioned - i also keep the QueryBuilder logic in the same class as it's defined as a data member of the class and can be used in many class methods without issue. 

Also since you need a session - you may have to re-create the session in each class as well:

builder.createQuery(PredicateGroup.create(map), session)

4 replies

TheBigRed
TheBigRedAuthor
New Participant
December 3, 2015

Thank you for this Scott, will let you know the outcome!

smacdonald2008
smacdonald2008Accepted solution
New Participant
December 2, 2015

"I want to get the paths from a different class which processes the query"

Ok - i see what you are doing. I have never tired this - I have always developed QueryBuilder App Logic using  different methods in the same class - not different classes. 

You can try passing the QueryBuilder Object itself -- for example - 

Servlet Class{

@Reference
private QueryBuilder builder;

@Reference
private ServiceA serA;

@Reference
private ServiceB servB;

@Reference
private ServiceC servC;

doGet

{

serA.getPathA(builder); 

serB.getPathB(builder); 

serC.getPathC(builder); 

}

}

Assuming:

ServiceA - expose method getPathA(QueryBuilder qb)

ServiceB - expose method getPathB(QueryBuilder qb)

ServiceC - expose method getPathB(QueryBuilder qb)

I have never tried this. Since QueryBuilder is a managed object and was created with Dependency Injection, not sure if this will work in other classes or if you will have to re-create it in each service.

Try that and see what happens. I am also assuming that the other classes are also using @Service annotations.  

Like I mentioned - i also keep the QueryBuilder logic in the same class as it's defined as a data member of the class and can be used in many class methods without issue. 

Also since you need a session - you may have to re-create the session in each class as well:

builder.createQuery(PredicateGroup.create(map), session)

TheBigRed
TheBigRedAuthor
New Participant
December 2, 2015

smacdonald2008 wrote...

I would create the session in the class that uses it. That reflects best practice. For example - if you need a session in an custom AEM service - then keep the session in  the class that uses the @Service annotation.   Try and keep open sessions to a minimum as a lot of open session can result in poor performance. 

 

I've tried that particularly with the QueryBuilder API.

What I'm trying to do is display a bunch of paths through a servlet.

I want to get the paths from a different class which processes the query and returns a string array of

paths. However this object is displaying nothing?

smacdonald2008
New Participant
December 2, 2015

I would create the session in the class that uses it. That reflects best practice. For example - if you need a session in an custom AEM service - then keep the session in  the class that uses the @Service annotation.   Try and keep open sessions to a minimum as a lot of open session can result in poor performance.