{"id":37019025,"url":"https://github.com/tomitribe/microscoped","last_synced_at":"2026-01-14T02:02:34.342Z","repository":{"id":57740983,"uuid":"44929675","full_name":"tomitribe/microscoped","owner":"tomitribe","description":"A handful of custom CDI Scopes for your hashmap-killing pleasure","archived":false,"fork":false,"pushed_at":"2020-10-13T00:29:20.000Z","size":61,"stargazers_count":37,"open_issues_count":12,"forks_count":11,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-11-07T02:00:48.057Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomitribe.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-25T21:02:46.000Z","updated_at":"2025-08-18T06:06:40.000Z","dependencies_parsed_at":"2022-09-06T23:30:54.566Z","dependency_job_id":null,"html_url":"https://github.com/tomitribe/microscoped","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tomitribe/microscoped","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitribe%2Fmicroscoped","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitribe%2Fmicroscoped/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitribe%2Fmicroscoped/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitribe%2Fmicroscoped/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomitribe","download_url":"https://codeload.github.com/tomitribe/microscoped/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomitribe%2Fmicroscoped/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T02:02:33.760Z","updated_at":"2026-01-14T02:02:34.331Z","avatar_url":"https://github.com/tomitribe.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Microscoped\n\nA handful of custom CDI Scopes for your learning and hashmap-killing pleasure.\n\nWhile EJB has `@Stateless`, `@Singleton`, `@Stateful` there are near equivalent of all such concepts in CDI such as `@ApplicationScoped` and `@SessionScoped`.\n Unlike its predecessor EJB, CDI is not fixed to just these bean types.  It is possible for nearly any lifecycle to be supported via adding a custom `@Scope` implementation.\n\nDespite this awesome power and potential, the use of custom CDI Scopes is rarely found in the wild.  This is more due to lack of resources and examples for doing so as well as a truly inspired set of ideas.\n\nThis project attempts to give both.  Rest assured, if you're storing objects in a map in your code and hooking that map up to a `ThreadLocal`, that code is begging to be rewritten.\n\n## Out of the Box Scopes\n\nMicroscoped provides a handful of real-world and usable scopes out of the box:\n\n - `@MethodScoped` -  track objects based on the currently executing method\n - `@DomainScoped` - swap out all your implementations based on what virtual host an HTTP request is targeting\n - `@HeaderScoped` - specify a header, such as `version`, to toggle a completely different set of objects to be used to service a request\n - `@TimerScoped` - keep state between EJB Timer firing by using a scope to remember where you left off last time the timer fired\n\n## Implementing a Custom Scope\n\nFirst order of business is to create the scope annotation itself:\n\n[source,java]\n----\nimport javax.enterprise.context.NormalScope;\nimport java.lang.annotation.Documented;\nimport java.lang.annotation.ElementType;\nimport java.lang.annotation.Inherited;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.RetentionPolicy;\nimport java.lang.annotation.Target;\n\n@Documented\n@NormalScope(passivating = false)\n@Inherited\n@Retention(RetentionPolicy.RUNTIME)\n@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})\npublic @interface MethodScoped {\n}\n----\n\nNext we need to register that scope with the `BeanManger` when the application starts.  We must specify a `Context` implementation.  No worries,\nMicroscoped comes with one very simple implementation called `ScopeContext` that can hold anything.\n\n[source,java]\n----\nimport org.tomitribe.microscoped.core.ScopeContext;\n\nimport javax.enterprise.event.Observes;\nimport javax.enterprise.inject.spi.AfterBeanDiscovery;\nimport javax.enterprise.inject.spi.BeforeBeanDiscovery;\nimport javax.enterprise.inject.spi.Extension;\n\n\npublic class MethodScopedExtension implements Extension {\n\n    public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {\n        bbd.addScope(MethodScoped.class, true, false);\n        bbd.addInterceptorBinding(MethodScopeEnabled.class);\n    }\n\n    public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) {\n\n        abd.addContext(new ScopeContext\u003c\u003e(MethodScoped.class));\n\n    }\n}\n----\n\nFinally, we do the work of putting boundaries on when the scope should be active and not active.\nThis is usually done with an interceptor, filter or some other around advice.\n\n[source,java]\n----\nimport org.tomitribe.microscoped.core.ScopeContext;\n\nimport javax.enterprise.inject.spi.BeanManager;\nimport javax.inject.Inject;\nimport javax.interceptor.AroundInvoke;\nimport javax.interceptor.Interceptor;\nimport javax.interceptor.InvocationContext;\nimport java.lang.reflect.Method;\n\n@Interceptor\n@MethodScopeEnabled\npublic class MethodScopedInterceptor {\n\n    @Inject\n    private BeanManager beanManager;\n\n    @AroundInvoke\n    public Object invoke(InvocationContext invocation) throws Exception {\n        final ScopeContext\u003cMethod\u003e context = (ScopeContext\u003cMethod\u003e) beanManager.getContext(MethodScoped.class);\n\n        final Method previous = context.enter(invocation.getMethod());\n        try {\n            return invocation.proceed();\n        } finally {\n            context.exit(previous);\n        }\n    }\n}\n----\n\n## Using a Custom Scope\n\nTo use your newly created custom scope, you simply need to start annotating beans with it.\n\nThe following will effectively give each method its very own `Count` instance.\n\n[source,java]\n----\nimport org.tomitribe.microscoped.method.MethodScoped;\n\nimport java.util.concurrent.atomic.AtomicInteger;\n\n@MethodScoped\npublic class Count {\n\n    private final AtomicInteger count = new AtomicInteger();\n\n    public int get() {\n        return count.get();\n    }\n\n    public int add() {\n        return count.incrementAndGet();\n    }\n\n    public boolean compareAndSet(int expect, int update) {\n        return count.compareAndSet(expect, update);\n    }\n\n    public int remove() {\n        return count.decrementAndGet();\n    }\n}\n----\n\nThen, in complete magic and with no map in the service class at all, we will get one `Count` per method\n\n[source,java]\n----\nimport org.tomitribe.microscoped.method.MethodScopeEnabled;\n\nimport javax.ejb.Lock;\nimport javax.ejb.Singleton;\nimport javax.inject.Inject;\nimport javax.ws.rs.GET;\nimport javax.ws.rs.Path;\n\nimport static javax.ejb.LockType.READ;\n\n@Lock(READ)\n@Singleton\n@Path(\"/color\")\n@MethodScopeEnabled\npublic class ColorService {\n\n    @Inject\n    private Count count;\n\n    @GET\n    @Path(\"/red\")\n    public String red() {\n        return String.format(\"red, %s invocations\", count.add());\n    }\n\n    @GET\n    @Path(\"/green\")\n    public String green() {\n        return String.format(\"green, %s invocations\", count.add());\n    }\n\n    @GET\n    @Path(\"/blue\")\n    public String blue() {\n        return String.format(\"blue, %s invocations\", count.add());\n    }\n\n}\n----\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomitribe%2Fmicroscoped","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomitribe%2Fmicroscoped","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomitribe%2Fmicroscoped/lists"}