Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to persist session when passing to class/object

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

"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)

View solution in original post

4 Replies

Avatar

Level 10

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. 

Avatar

Level 5

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?

Avatar

Correct answer by
Level 10

"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)

Avatar

Level 5

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