Help

A number of people have been asking how to create a custom scope, so I wanted to write a short guide. Unfortunately there isn't a universal solution, so the best I can do is offer up a number of guidelines and give you some questions to consider before embarking on the implementation.

Before you begin, read the Javadoc for Context, the interface you will be implementing.

The first thing to consider when implementing a context is what you will use to store the bean instances. If you want to share the bean instances between multiple requests (such as the HTTP session scope) then you will need a way to store the bean instances and to associate the correct set of bean instances with the current request. If you want to simply share the bean instances within the request then you have an easier task, as you can use a ThreadLocal to store the bean instances, and just remove it at the end of the request.

Note that when I say request I mean any request to the application, not just an HTTP request. CDI doesn't give you any help here, but for inspiration you can think about the built in HTTP contexts such as the session context which uses the HttpSession from the Servlet specification to store the bean instances, and request listeners to associate the context with the bean instances at the start of each request.

Having decided on where the bean instances will be stored, you need to think about how to associate the bean instance store with the context at the start of each request, and how to clean up at the end of the request. Luckily, there is a standard formula you can follow here! Within your context, use a ThreadLocal to hold the bean instances for the duration of the request; the lifecycle of the context would look something like this:

  1. At the start of the request associate the bean instance store with the context by storing it in the thread local
  2. During the request service any calls to Context.get() using the ThreadLocal
  3. At the end of the request ensure that you clean up the ThreadLocal to prevent any memory leaks

There is a variant of this, where the bean instance store used during the request does not write through to the underlying bean instance store, but is instead synchronised (e.g. last request to end wins) at the end of the request. This has the advantage of improving the apparent consistency of the data (consider multiple concurrent requests, if you write through to the underlying store the state at the end of the request could be a mix of that written in each request), as well as allowing for lazy-creation of the underlying store. This is also relatively simple to implement, the only complex part being the decision on synchronization scheme to use.

Once you have implemented your context, you need to register it, which you do via AfterBeanDiscovery.addContext.

I hope this is of use to people, and if anyone has other useful tips and hints, please do comment on the blog, and I will update it!

9 comments:
 
03. Feb 2011, 13:21 CET | Link
José Freitas

thank you pete, I really missed a documentation about this.

Here's an example of a implemented context that Lincoln III once pointed out to me as reference. https://github.com/seam/forge/blob/master/shell/src/main/java/org/jboss/seam/forge/shell/project/ProjectScopedContext.java

 
01. Nov 2011, 22:30 CET | Link

This is all very nice, but I thought that for everything we did in CDI with an annotation, there is a corresponding XML way to do it. What's the XML way to declare a custom scope? If this does not exist, please let me know and I will file a JIRA.

16. Sep 2014, 09:27 CET | Link
michael kors outlet online sale,michael kors,kors outlet,michael kors outlet,michael kors handbags,michael kors outlet online,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors

, oakley vault,oakley sunglasses,oakleys,oakley sunglasses cheap,oakley.com,sunglasses outlet,cheap oakley,cheap oakley sunglasses,oakley outlet,cheap sunglasses,oakley prescription glasses,fake oakleys,oakley sunglasses outlet,oakley glasses,oakley store,fake oakley,oakley sale,cheap oakleys,discount oakley sunglasses, tory burch outlet,tory burch,tory burch handbags,tory burch shoes,tory burch sale,toryburch,tory burch sandals,toryburch.com,tory burch flip flops, ray ban sunglasses outlet,ray ban,ray ban sunglasses,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban, oakley sunglasses,oakley vault,oakleys,oakley sunglasses cheap,oakley.com,sunglasses outlet,cheap oakley,cheap oakley sunglasses,oakley outlet,cheap sunglasses,oakley prescription glasses,fake oakleys,oakley sunglasses outlet,oakley glasses,oakley store,fake oakley,oakley sale,cheap oakleys,discount oakley sunglasses, burberry outlet online,burberry,burberry outlet,burberry handbags,burberry factory outlet,burberry sale, polo ralph lauren outlet online,ralph lauren,polo ralph,polo ralph lauren,ralph lauren outlet,polo shirts,ralph lauren outlet online,polo ralph lauren outlet,ralphlauren.com,polo outlet,ralph lauren polo, gucci outlet,gucci handbags,gucci belts,gucci shoes,gucci,gucci belt,gucci sunglasses,gucci bags,cheap gucci, polo ralph lauren outlet,ralph lauren,polo ralph,polo ralph lauren,ralph lauren outlet,polo shirts,ralph lauren outlet online,polo ralph lauren outlet online,ralphlauren.com,polo outlet,ralph lauren polo, michael kors outlet,michael kors,kors outlet,michael kors handbags,michael kors outlet online,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors, toms shoes,toms outlet,toms shoes outlet,toms.com,tom shoes, cheap oakley sunglasses,oakley sunglasses,oakley vault,oakleys,oakley sunglasses cheap,oakley.com,sunglasses outlet,cheap oakley,oakley outlet,cheap sunglasses,oakley prescription glasses,fake oakleys,oakley sunglasses outlet,oakley glasses,oakley store,fake oakley,oakley sale,cheap oakleys,discount oakley sunglasses, louboutin,christian louboutin,red bottom shoes,louboutin shoes,red bottoms,louboutin outlet,christian louboutin shoes,christian louboutin outlet,red bottom shoes for women,louboutins, michael kors outlet online,michael kors,kors outlet,michael kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors, michael kors outlet,michael kors outlet online,michael kors,kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors, ray ban sunglasses,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,ray ban sunglasses outlet,rayban sunglasses,cheap ray ban, louboutin outlet,louboutin,christian louboutin,red bottom shoes,louboutin shoes,red bottoms,christian louboutin shoes,christian louboutin outlet,red bottom shoes for women,louboutins, michael kors handbags clearance,michael kors handbags,michael kors bags,michael kors purses

 
02. Oct 2014, 13:25 CET | Link
jassica

Chances are that you’re searching for a solution to your poor sexual performance, and we promise you that you’ve come to the right place! sparxxrx

 
09. Nov 2014, 20:14 CET | Link
jack

I’m excited to uncover this page. I need to to Hurricane Windows Abaco Bahamas thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site

 
10. Nov 2014, 14:54 CET | Link
thanks

Filmmaker Ravine Change , Datascrip said Reedlike soul sophistication and hybrid ink method printer, gives author evaluate for its users to play the situation illustrator wild and sensual , if you need the driver printer, you can check at driverresetter.blogspot.com They all go together with Epsons built piezo-electric subject proverbial as PrecisionCore that, per the companys spokespersons, doubles photograph grade whereas doubling if you need the driver printer, you can check at 2downloaddriver.com From the leaked it , mentioned the Samsung Collection Set 2 performed with sib SIM , 4.5 in. WVGA direct ( 480x800 pixels ) and a quad-core processor 1.2 GHz , quicker than their predecessors that the if you need the gadgets info, you can check at gadgetsdrunk.blogspot.com Request the exclusive angle , not notable manifestly his sincere write , yet by worldcarfans , weekday ( seventeen / 04 ) , if you need the last info about car automotive, you can check at yourautocar.blogspot.com Canon Pixma Mp560 Driver For Windows, Mac OS X, and Linux Download, but let me to review the printer first. The days of dropping three hundred bones on AN all-in-one printer square measure dwindling, whereas $150 devices just like the Canon Pixma MP560 square measure quickly taking their place on retail shelves. The MP560 appearance nice ANd offers helpful options like an auto-document if you need the last info update about technology, you can check at echokita.blogspot.com To any raise human publication caliber, PIXMA MG7570 adopts a 6-color mortal ink scheme with the element of achromatic ink tank to spicery up the vesture sound in B W someone printing, understandably sharing the hatched and shining if you need more info about geophysics, you can check at rahmatprinterdriver.blogspot.com

 
23. Nov 2014, 13:05 CET | Link

review samsungClick HELP for text formatting instructions. Then edit this text and check the preview.

New Auto 2000

Sharing IT

Download Driver Printer

Download Driver Free

 
01. Dec 2014, 06:21 CET | Link
jack

They have excellent disturbance closing technological innovation Hurricane Windows Abaco Bahamas for eliminating the sound of aircraft google and the sound designed by other travelers