3 Replies Latest reply on Oct 15, 2013 12:40 PM by dudehook

    Help implementing a context

    dudehook

      Hi all-

       

      I'm trying to implement a custom context - right now it is basically equivalent to an HTTP session context, except I'm not using HTTP.  So I've created my own context "MyContext", extending AbstractBoundContext<BoundBeanStore>, implementing associate() and getScope() [using my own scope type "MyScope"].

       

      Now, I have code which processes "requests" by putting them on a LinkedeBlockingQueue, which is serviced by a thread, which creates a Runnable to handle the request within a thread pool.  My Runnable is a class I called "Worker", and it's being automatically decorated because I've enabled ThreadScoped contexts.  The worker then fires an event containing the request.  I want the bean instances servicing the request events to be session scoped within my context.

       

      My question is... how to I associate a BoundBeanStore to my session context?

       

      First, I know I have to get a reference to the session context, but I don't know how to do that.  I've tried:

       

           MyContext sessionContext = bm.instance().select(MyContext.class).get();

       

      within the worker, but this doesn't seem to give me a session context which is the one used by the bean that I'm invoking in my next line of code:

       

          requestEvent.select(new VersionQualifier(version)).fire(request);

       

      I'm assuming this is because the "requestEvent" member variable is injected into the worker, and the context it is using is not the same as the bm.instance()  ???

       

      Anyway, all I need to know is how to set up the context for the session such that the event handler invoked from the worker will work.

       

      Any ideas?  Thanks in advance!

        • 1. Re: Help implementing a context
          mkouba

          Hi,

          the result of bm.instance().select(MyContext.class).get() will not be the same unless you register a special bean for your context. This is what org.jboss.weld.bootstrap.WeldStartup does in createContexts() method. So you could either follow the way Weld registers context beans or for example hold a MyContext reference on the extension class which registers the context (for each extension, the container must provide a bean with scope @ApplicationScoped and qualifier @Default).

          • 2. Re: Help implementing a context
            dudehook

            Ok - I do have an extension which registers MyContext, in the BeforeBeanDiscovery event observer.  Is that not going to work?

             

            Are you saying that I need to use something like:

                 contexts.add(new ContextHolder<ApplicationContext>(new ApplicationContextImpl(), ApplicationContext.class, UnboundLiteral.INSTANCE));

             

            from WeldBootstrap?  If so, how do I access the "contexts" and ContextHolder from my extension?

             

            But really, isn't that what I'm already doing by adding the context in the observer method?

             

            Also, my context is being registered and is available.  I just don't know how to get the right instance at the right time to associate the bean store.

            • 3. Re: Help implementing a context
              dudehook

              Solved the issue, with direction from Martin on IRC (thanks!)...

               

              In my extension which registers my context, I save the instance and make it globally accessible (singleton pattern).  Then, whenever I need to associate/disassociate a bean store with the context I can do it with the global instance.

               

              Tried it and it works.  Thanks.