GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - weld/command-context-example

BranchesTags
Open more actions menu

Latest commit

 

History

13 Commits
13 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
dco.txt
dco.txt
 
 
pom.xml
pom.xml
 
 

Repository files navigation

Command context example

This example project shows how to implement a custom CDI context with all the extras (including activation and deactivation). This branch uses CDI 4.1 and Weld 6.0.

Note

If you are looking for the same example with CDI 2.0 and Weld 3 (using javax namespace), here is the branch.

Mission

Suppose we have a simple functional interface:

public interface Command {
    void execute();
}

Our mission is to provide a custom context so that it's possible to create bean instances whose lifecycle is bound to the command execution. In other words, a @CommandScoped bean instance should be destroyed after the execution completes. As a bonus we would like to be able to inject CommandExecution metadata (and see for example the time an execution started at).

Implementation

First of all, we need to provide a scope annotation:

@NormalScope
public @interface CommandScoped {
}

Note that the scope is normal. This implies few facts and requirements:

  • whenever you inject a @CommandScoped bean you get a client proxy, this allows e.g. to inject a @CommandScoped bean into @ApplicationScoped bean
  • there may be no more than one mapped bean instance per @CommandScoped bean per thread

See also the spec - 6.3. Normal scopes and pseudo-scopes.

The other important class is CommandContextImpl. Few things to notice:

  • ThreadLocal is used to store the map of bean instances
  • there must be a way to activate/deactivate the context - see also activate() and deactivate() methods
  • InjectableCommandContext is an injectable version of context which allow to detect the original "activator", so that it's possible to skip deactivation during deactivate()
  • ContextualInstance wrapper allows to create and destroy a bean instance properly

Activation

The custom context can be activated/deactivated either by CommandDecorator, CommandExecutor or manually - a @Dependent bean with bean type CommandContext and qualifier @Default is automatically registered. See also the tests and compare the different ways of activating the context.

Tests

All the tests use Weld SE programmatic bootstrap API. This allows us to:

  • run the tests as regular unit tests - no Java EE container is needed
  • disable automatic discovery and gain the full control of scanning

About

No description, website, or topics provided.

Resources

Readme
Apache-2.0 license

Code of conduct

Code of conduct

Contributing

Contributing
Activity
Custom properties

Stars

9 stars

Watchers

1 watching

Forks

5 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.