Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] jetty-users Digest, Vol 100, Issue 9

I have implemented the suggested strategy.   Our session data is read only so sharing is not a problem.   I will test it in the next few days and give me feedback.

 

Thanks for the detailed explanation. It helped me a lot.

 

Regards,

Aravind.

 

From: Jan Bartel [mailto:janb@xxxxxxxxxxx]
Sent: Tuesday, September 12, 2017 4:28 PM
To: Raghavan, Aravind <Aravind.Raghavan@xxxxxxxxxx>
Subject: Re: jetty-users Digest, Vol 100, Issue 9

 

Raghavan,

 

To share the sessions between multiple contexts, you could create your own implementation of the SessionDataStore interface which stores SessionData objects in memory, ignoring any context-related specifics.  You use the same SessionDataStore instance for both of your contexts.  You configure each context with a NullSessionCache, backed up by your SessionDataStore. The NullSessionCache ensures that Session objects are not stored in memory in each context, but rather looked up fresh from your SessionDataStore on each access. Effectively your shared SessionDataStore implementation becomes the shared "cache".

 

The caveat here is that if you have multiple simultaneous requests for the same session and the data in your sessions is not read-only/mostly, then you won't have guaranteed outcomes of writes, because sessions are not ACID according to the servlet spec.

 

Here's a simple example of how your SessionDataStore impl could look:

 

public  class MyStore extends AbstractLifeCycle implements SessionDataStore

{

        HashMap<String, SessionData> store = new HashMap<>();

        

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating()

         */

        @Override

        public boolean isPassivating()

        {

            return false;

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String)

         */

        @Override

        public boolean exists(String id) throws Exception

        {

            return store.get(id) != null;

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String)

         */

        @Override

        public SessionData load(String id) throws Exception

        {

            return store.get(id);

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String)

         */

        @Override

        public boolean delete(String id) throws Exception

        {

            return store.remove(id) != null;

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataMap#initialize(org.eclipse.jetty.server.session.SessionContext)

         */

        @Override

        public void initialize(SessionContext context) throws Exception

        {

            //ignore 

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataMap#store(java.lang.String, org.eclipse.jetty.server.session.SessionData)

         */

        @Override

        public void store(String id, SessionData data) throws Exception

        {

            store.put(id, data); 

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataStore#newSessionData(java.lang.String, long, long, long, long)

         */

        @Override

        public SessionData newSessionData(String id, long created, long accessed, long lastAccessed, long maxInactiveMs)

        {

            return new SessionData(id, "", "", created, accessed, lastAccessed, maxInactiveMs);

        }

 

        /** 

         * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(java.util.Set)

         */

        @Override

        public Set<String> getExpired(Set<String> candidates)

        {

            // TODO

            return Collections.emptySet();

        }

    }

 

 

And here's how you would hook it up:

 

WebAppContext w1 = new WebAppContext();

 

MyStore store = new MyStore();

SessionCache cache = new NullSessionCache(w1.getSessionHandler());

cache.setSessionDataStore(store);

w1.getSessionHandler().setSessionCache(cache);

 

WebAppContext w2 = new WebAppContext();

SessionCache cache2 = new NullSessionCache(w2.getSessionHandler());

cache2.setSessionDataStore(store);

w2.getSessionHandler().setSessionCache(cache2);

 

 

Jan

 

On 12 September 2017 at 14:12, Raghavan, Aravind <Aravind.Raghavan@xxxxxxxxxx> wrote:

Hi Jan,

 

That is the issue I am facing.  Until 9.3  I was able to share the SessionManger between contexts.  In 9.4  I am not able to share the SessionHandler between contexts.  Sharing make the only one of the contexts available and all other contexts become unavailable  (or requests return null context)

 

I solved it by

1. setting cookie path to root ("/")

2. extending the getSession() function of SessionHandler to loop through all the contexts to check if session is created for the cookie id in any other context and return the session from any one of the contexts

 

It doesn’t look efficient so wanted to know if there is any other recommended way of achieving this.

 

Thanks & Regards,

Aravind.

 

From: Jan Bartel [mailto:janb@xxxxxxxxxxx]
Sent: Tuesday, September 12, 2017 11:49 AM
To: Raghavan, Aravind <Aravind.Raghavan@xxxxxxxxxx>
Subject: Re: jetty-users Digest, Vol 100, Issue 9

 

Well, I would recommend that you use cookies to maintain a single-sign-on solution instead.

 

However, have you tried just sharing the same SessionHandler across both ServletContexts? Something like:

 

        Server server = new Server(8080);

        

        ContextHandlerCollection contexts = new ContextHandlerCollection();

        server.setHandler(contexts);

        

        ServletContextHandler context = new ServletContextHandler(

                ServletContextHandler.SESSIONS);

        context.setContextPath("/");

        context.setResourceBase(System.getProperty("java.io.tmpdir"));

        contexts.addHandler(context);

 

        // Access the SessionHandler from the context.

        SessionHandler sessions = context.getSessionHandler();

        

        SessionCache cache = new DefaultSessionCache(sessions);

        cache.setSessionDataStore(new NullSessionDataStore());

        sessions.setSessionCache(cache);

 

        context.addServlet(HelloSessionServlet.class, "/hello");

 

        //Second ServletContext, sharing same SessionHandler

        ServletContextHandler two = new ServletContextHandler();

        two.setContextPath("/two");

        two.addServlet(HelloSessionServlet.class, "/hello");

        two.setSessionHandler(sessions);

        contexts.addHandler(two);

 

        server.start();

        server.join();

 

 

Jan

 

On 11 September 2017 at 16:03, Raghavan, Aravind <Aravind.Raghavan@xxxxxxxxxx> wrote:

Hi Jan,

Thanks for taking time to looking into this issue.  My Initial post did not address the problem correctly.  After further investigation I have found out the exact issue but the solution still eludes me.   Hope you will be able to help me with that.

The reason I was receiving null servletContext was because I was sharing a single SessionHandler object among many WebAppContexts.

 In < Jetty 9.4  I used to share a HashSessionManager object between WebAppContexts  as a way of sharing session objects among them so that users don't have to re-authenticate.   The strategy seemed to work fine without any issues.

In Jetty 9.4 due to changes to Session Management, I am not able to implement similar strategy.  SessionCache are specific to contexts and I am not able to share sessions between contexts.  Is there a way to implement global sessions  (server level rather than context level) in Jetty 9.4  ?


Thanks & Regards,
Aravind.


-----Original Message-----
From: jetty-users-bounces@xxxxxxxxxxx [mailto:jetty-users-bounces@xxxxxxxxxxx] On Behalf Of jetty-users-request@xxxxxxxxxxx
Sent: Friday, September 08, 2017 1:08 PM
To: jetty-users@xxxxxxxxxxx
Subject: jetty-users Digest, Vol 100, Issue 9

Send jetty-users mailing list submissions to
        jetty-users@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
        https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_jetty-2Dusers&d=DwICAg&c=3v6EBbtpnn9A7jIZYjOw6KN7Pe17WoimzcinOq2Xztg&r=YbS3Xf40CmAQ12d73dX0bv-TPzDJnSuxn9AFms1fOWg&m=3EtJu1ppbjOzJ_v-OwhfrxLvSX0DZks9P4rZrXYXMgE&s=RvIeN8HbsO4LJBVcUHpSc5W1dLA4pLTzZV6ZsOpK35o&e=
or, via email, send a message with subject or body 'help' to
        jetty-users-request@xxxxxxxxxxx

You can reach the person managing the list at
        jetty-users-owner@xxxxxxxxxxx

When replying, please edit your Subject line so it is more specific than "Re: Contents of jetty-users digest..."


Today's Topics:

   1. jetty 9.4.6 - getting null servletcontext in servlets
      (Raghavan, Aravind)
   2. Re: jetty 9.4.6 - getting null servletcontext in  servlets
      (Jan Bartel)


----------------------------------------------------------------------

Message: 1
Date: Fri, 8 Sep 2017 03:05:10 +0000
From: "Raghavan, Aravind" <Aravind.Raghavan@xxxxxxxxxx>
To: "'jetty-users@xxxxxxxxxxx'" <jetty-users@xxxxxxxxxxx>
Subject: [jetty-users] jetty 9.4.6 - getting null servletcontext in
        servlets
Message-ID:
        <507B873CF700714381A8EBAC9E77085499E76585@xxxxxxxxxxxxxxx.local>
Content-Type: text/plain; charset="us-ascii"

Hi All,

I recently upgraded from jetty 9.3.11 to 9.4.6 .  And after upgrade  none of my servlets are able to get servlet context.  getServletContext() always returns null.  Can you please help me figure out what I am doing wrong?

Relevant section of code:

handlers_ = new HandlerCollection(true); chc_ = new ContextHandlerCollection();
for(WebAppConfig wap: webAppConfigs)   //webappconfig a POJO from where I am getting webapp configs
{
  String path = wap.getPath();
  String warFile = wap.getWarFile();
  WebAppContext context =
    new WebAppContext(chc_, warFile, path);
  chc_.addHandler(context);
  context.setContextPath(path);
  for (ServletConfig servletConfig: wap.getServletConfigs())  //ServletConfig is another POJO to get servlet configs
  {
    String servletName = servletConfig.getName();
    String servletPath = servletConfig.getPath();
    Servlet servlet = servletConfig.getServlet();
    ServletHolder servletHolder = new ServletHolder(servlet);
    context.addServlet(servletHolder, servletPath);
  }
}
handlers_.setHandlers(new Handler[] { chc_, new DefaultHandler()}); server_.setHandler(handlers_);


Note the same code worked fined with 9.3.11.  Getting null context only after upgrading to 9.4.6.  I am not sure what has changed in 9.4.x to cause this issue.  I can't find any changes (other than Session Management) in documentation.


Thanks,
Aravind.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_private_jetty-2Dusers_attachments_20170908_dc9bb395_attachment.html&d=DwICAg&c=3v6EBbtpnn9A7jIZYjOw6KN7Pe17WoimzcinOq2Xztg&r=YbS3Xf40CmAQ12d73dX0bv-TPzDJnSuxn9AFms1fOWg&m=3EtJu1ppbjOzJ_v-OwhfrxLvSX0DZks9P4rZrXYXMgE&s=uXSeFQ--5DZ_eEs5QQQ2vRjxbnpdypym5CKG62YtgHM&e= >

------------------------------

Message: 2
Date: Fri, 8 Sep 2017 15:08:20 +1000
From: Jan Bartel <janb@xxxxxxxxxxx>
To: JETTY user mailing list <jetty-users@xxxxxxxxxxx>
Subject: Re: [jetty-users] jetty 9.4.6 - getting null servletcontext
        in      servlets
Message-ID:
        <CAEHc-316ZN2MCtR2mQ0jv=RwOhOFkL85dmL51p7KKpmUr2g5ww@xxxxxxxxxxxxxx>
Content-Type: text/plain; charset="utf-8"

Aravind,

There's not enough info in your post to pinpoint the problem. I've whipped up a small example based on your code, and it runs with no problems:

        Server server = new Server(8080);
        HandlerCollection handlers_ = new HandlerCollection(true);
        ContextHandlerCollection chc_ = new ContextHandlerCollection();
        String path = "/blah";

        WebAppContext context =
                new WebAppContext(chc_,
"/path/to/distro/demo-base/webapps/test.war", path);

        //needed for the test webapp
        HashLoginService loginService = new HashLoginService();
        loginService.setName( "Test Realm" );
        loginService.setConfig(
"/path/to/distro/demo-base/etc/realm.properties" );
        server.addBean( loginService );
        chc_.addHandler(context);
        context.setContextPath(path);


        String servletPath = "/blah/*";
        Servlet servlet = new HelloServlet("blah-hello");
        ServletHolder servletHolder = new ServletHolder(servlet);
        context.addServlet(servletHolder, servletPath);

        handlers_.setHandlers(new Handler[] { chc_, new DefaultHandler()});
        server.setHandler(handlers_);
        server.start();
        server.join();


Perhaps you have old jetty-9.3 jars on the classpath?

In the absence of more info, I can only suggest that you turn on full debug to give a clue. Maybe also try a  server.setDumpAfterStart(true) as well to see more.

Jan

On 8 September 2017 at 13:05, Raghavan, Aravind <Aravind.Raghavan@xxxxxxxxxx
> wrote:

> Hi All,
>
>
>
> I recently upgraded from jetty 9.3.11 to 9.4.6 .  And after upgrade
> none of my servlets are able to get servlet context.
> getServletContext() always returns null.  Can you please help me figure out what I am doing wrong?
>
>
>
> Relevant section of code:
>
>
>
> handlers_ = *new* HandlerCollection(*true*);
>
> chc_ = *new* ContextHandlerCollection();
>
> *for*(WebAppConfig wap: webAppConfigs)   //webappconfig a POJO from where
> I am getting webapp configs
>
> {
>
>   String path = wap.getPath();
>
>   String warFile = wap.getWarFile();
>
>   WebAppContext context =
>
>     *new* WebAppContext(chc_, warFile, path);
>
>   chc_.addHandler(context);
>
>   context.setContextPath(path);
>
>   *for* (ServletConfig servletConfig: wap.getServletConfigs())
> //ServletConfig is another POJO to get servlet configs
>
>   {
>
>     String servletName = servletConfig.getName();
>
>     String servletPath = servletConfig.getPath();
>
>     Servlet servlet = servletConfig.getServlet();
>
>     ServletHolder servletHolder = *new* ServletHolder(servlet);
>
>     context.addServlet(servletHolder, servletPath);
>
>   }
>
> }
>
> handlers_.setHandlers(*new* Handler[] { chc_, *new*
> DefaultHandler()});
>
> server_.setHandler(handlers_);
>
>
>
>
>
> Note the same code worked fined with 9.3.11.  Getting null context
> only after upgrading to 9.4.6.  I am not sure what has changed in
> 9.4.x to cause this issue.  I can?t find any changes (other than
> Session Management) in documentation.
>
>
>
>
>
> *Thanks,*
>
> *Aravind.*
>
>
>
>
>
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or
> unsubscribe from this list, visit
> https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_m
> ailman_listinfo_jetty-2Dusers&d=DwICAg&c=3v6EBbtpnn9A7jIZYjOw6KN7Pe17W
> oimzcinOq2Xztg&r=YbS3Xf40CmAQ12d73dX0bv-TPzDJnSuxn9AFms1fOWg&m=3EtJu1p
> pbjOzJ_v-OwhfrxLvSX0DZks9P4rZrXYXMgE&s=RvIeN8HbsO4LJBVcUHpSc5W1dLA4pLT
> zZV6ZsOpK35o&e=
>



--
Jan Bartel <janb@xxxxxxxxxxx>
www.webtide.com
*Expert assistance from the creators of Jetty and CometD*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_private_jetty-2Dusers_attachments_20170908_85398a13_attachment.html&d=DwICAg&c=3v6EBbtpnn9A7jIZYjOw6KN7Pe17WoimzcinOq2Xztg&r=YbS3Xf40CmAQ12d73dX0bv-TPzDJnSuxn9AFms1fOWg&m=3EtJu1ppbjOzJ_v-OwhfrxLvSX0DZks9P4rZrXYXMgE&s=itXXamuZkZTco7DCOqWPAcHLxz54wS64UirsH4ZcZJ0&e= >

------------------------------

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_jetty-2Dusers&d=DwICAg&c=3v6EBbtpnn9A7jIZYjOw6KN7Pe17WoimzcinOq2Xztg&r=YbS3Xf40CmAQ12d73dX0bv-TPzDJnSuxn9AFms1fOWg&m=3EtJu1ppbjOzJ_v-OwhfrxLvSX0DZks9P4rZrXYXMgE&s=RvIeN8HbsO4LJBVcUHpSc5W1dLA4pLTzZV6ZsOpK35o&e=

End of jetty-users Digest, Vol 100, Issue 9
*******************************************



 

--

Jan Bartel <janb@xxxxxxxxxxx>

www.webtide.com
Expert assistance from the creators of Jetty and CometD

 



 

--

Jan Bartel <janb@xxxxxxxxxxx>

www.webtide.com
Expert assistance from the creators of Jetty and CometD

 


Back to the top