News

Weld 3.0.0.Alpha4

2015-2-5   release , cdi2   Jozef Hartinger

Here we are again with the next Alpha release of Weld 3. This time there are no additional CDI 2.0 previews. Instead, this release contains multiple performance improvements, enables @Asynchronous calls to be implemented simply with CDI interceptors and most importantly, it comes with weld-probe.

Weld Probe

Why isn’t my field injected? Why isn’t my interceptor triggered? What extensions do third-party libraries register?

Have you ever found yourself asking a similar question? Making Weld developer-friendly and helpful in troubleshooting is our priority. Yet, similar problems still occur sometimes.

But we can do event better! In Alpha4 of Weld 3 we are introducing a new tool called Weld Probe. Probe is a built-in tool that provides a detailed view into internals of a CDI application. Weld Probe has a web UI and is provided out-of-the-box.

Probe makes it easy to inspect:

  • beans, their properties such as qualifiers, stereotypes and name

  • a graph of bean dependencies

  • observers and producers declared by beans

  • interceptors and decorators bound to a bean

  • extensions

  • information about current contextual instances

  • tracking of invocations and invocation trees

Plus more addons are planned in future.

Weld Probe Screenshot

To try probe all that needs to be done is to upgrade to Alpha4, enable the development mode and point your browser to the URL of your CDI application plus /weld-probe suffix.

For more thorough overview of what Probe provides see Martin’s blog post or the reference documentation.

Bear in mind that Probe is still work in progress and that this early preview may have defects here and there.

Implementing @Asynchronous using CDI interceptor

It is possible to implement a substitute for EJB @Asynchronous method calls using CDI interceptors. Arjan Tijms recently blogged about his solution A similar one can be found in Weld’s testsuite. Long story short: This would not work smoothly until now because of a limitation in Weld. This limitation is now gone in Alpha4.

Performance

This release integrates various runtime performance improvements mainly around bean instance construction, producer and observer method invocation.

Next

Weld 3.0.0.Alpha5 is planned in 3 weeks time from now, with some more CDI 2.0 previews. We are going to continue releasing Alpha versions of Weld 3.0 roughly every 3 weeks until the early draft of CDI 2.0, which should happen sometime in March. At that point, we’ll move to Beta releases.

In the meantime, we’d like to hear your feedback on Probe or on Weld 3.0 Alpha releases in general. Feel free to use our forums or #weld-dev on freenode


An update on Weld 3

2014-12-10   cdi2   Jozef Hartinger

Today we are releasing the third Alpha release of Weld 3. These Alpha releases serve as prototypes of changes currently being discussed by the CDI expert group for the upcoming CDI 2.0.

The Alpha releases are not suitable for production use as the new API and functionality are still subject to change. We are releasing them to allow the community to test-drive the changes early in the development cycle. We want to shorten the feedback loop and identify possible glitches as soon as possible.

Let’s just quickly review what has been available since Alpha1:

  • declarative ordering of observer methods using @Priority

  • ability for an extension to veto and modify an observer method

  • support for Java 8 repeatable annotations as qualifiers and interceptor bindings

  • enhanced AnnotatedType API

For more details and examples of these features see my previous blog post.

On top of this, we’re now adding the following new features and enhancements:

  • asynchronous events

  • simplified configuration of Weld-specific properties

  • Guava is no longer used internally

Asynchronous events

Since its first version CDI has provided events as a facility for component interaction. Events enable loose coupling while preserving type safety. So far, CDI has supported synchronous delivery of events - the calling thread blocks until invocations of all associated observer methods complete. An alternative to this are transactional observer methods which are called asynchronously at the end of a transaction.

For the upcoming CDI 2.0 specification one of the hot topics is enhancement of the events facility. The expert group is considering adding fully asynchronous event dispatching mechanism.

A working prototype of this is available in Weld 3.0.0.Alpha3. The current proposal adds a method called fireAsync to the existing Event interface.

@Inject
    private ExperimentalEvent<Configuration> event;
    …
    event.fireAsync(new Configuration());

The call to event.fireAsync() returns immediately. The event is delivered to corresponding observers in a dedicated thread pool that can be configured.

What about thread-safety?

There are two common usage pattens for events. In the first one an immutable event object is used. An alternative is to use mutable events. A mutable event allows observers to participate on the result which is later used by the component that fired the event. An example of this would be the ProcessAnnotatedType<T> event used by CDI extensions. When events are fired synchronously, both approaches work fine but how does this work when we switch to async?

Nothing changes actually. No matter if the event object is immutable or not, you do not have to worry about thread-safety of the event object. The current implementation comes with the guarantee that event object is safely published which means that an observer method observes the event in the state in which:

  • it was left by an observer executing before the given observer, or

  • the initial state of the event if the given observer is the first one

Furthermore, the state is consistent throughout the execution of an observer method which means that we guarantee safe publication and prevent races for you. The only thing that should be avoided is modifying the state of the event object outside of an observer method. This behavior matches the option 4.1.1.1 in the current spec proposal.

In addition, if observer methods are ordered (another new feature proposed for CDI 2.0) we preserve the ordering (as in such situation the observers are ordered for a reason!) and invoke observers in the given order.

Last but not least, if an observer is transactional, we again preserve this and invoke the observer method in the corresponding transaction phase.

How do I know when event delivery finishes and what about exceptions?

In the current prototype we’re reusing the CompletionStage API, introduced in Java 8, which allows actions (callbacks) to be bound to the completion of the asynchronous delivery process. This is what it looks like:

event.fireAsync(new Configuration()).thenAccept(config -> master.compute(config));

This piece of code starts with asynchronously firing a mutable configuration object allowing loosely-coupled observers to alter the configuration of a computation. Once all observers finish, computation is initiated based on the resulting configuration.

If an exception occurs this can be dealt with also, either by falling back to a default value

event.fireAsync(new Configuration())
        .exceptionally(throwable -> DEFAULT_CONFIGURATION)
        .thenAccept((config) -> master.compute(config));

or by executing arbitrary code:

event.fireAsync(new Configuration()).whenComplete((config, throwable) -> {
        if (throwable != null) {
            System.err.println("Oops. Failed because of " + throwable.getMessage());
        } else {
            master.compute(config);
        }
    });

CompletionStage allows much more. If you are unfamiliar with the API see the Javadoc page for more information.

How do I try this myself?

It’s easy and multiple options are available. First of them is to use Weld in a standalone application.

  1. Create a new Java SE application.

  2. Add dependency on Weld

    <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>3.0.0.Alpha3</version>
        </dependency>
  3. Create an empty beans.xml file, e.g.

    mkdir src/main/resources/META-INF
        touch src/main/resources/META-INF/beans.xml
  4. Launch Weld and fire an event asynchronously

    public static void main(String[] args) {
            WeldContainer weld = new Weld().initialize();
            Event<String> evnt = weld.event().select(String.class);
            ExperimentalEvent<String> event = (ExperimentalEvent<String>) evnt;
        
            event.fireAsync("message");
        }

WildFly

Alternatively, a patch is available for WildFly that upgrades Weld within an existing WildFly instance. See the download page for more details.

Note that these new prototyped APIs are not part of the CDI API yet. Instead, they are currently located in Weld API in a package named org.jboss.weld.experimental

All these altered APIs have the Experimental prefix (that’s why we are using ExperimentalEvent in the examples)

We would appreciate your feedback! Feel free to use Weld forums or the cdi-dev mailing list for this purpose.

What’s next?

We are going to continue releasing early prototypes of features currently proposed for CDI 2.0. The plan is to release a new Alpha version every 3 weeks. There are several areas we want to focus on:

  • simplifying how extensions register beans and observers

  • monitoring and debugging of CDI applications

  • experimenting with full interception (intercepting even calls within a given component)

  • splitting the codebase into a “light” and “full” version (to support proposed CDI light version)

  • bootstrap API for SE environment


Weld 3.0.0.Alpha1 released!

2014-10-2   Jozef Hartinger

Today we are releasing Weld 3.0.0.Alpha1. This release serves as an early proof of concept of some of the ideas that are being discussed by the CDI Expert Group for the upcoming CDI 2.0 specification. Furthermore, this is a great opportunity for the community to test-drive some of the proposed improvements and provide us with feedback. Be warned though that this released does not come with any guarantee of stability and that binary and functional compatibility is likely to be broken in the following releases.

Ordering of observer methods

The first of the new features are ordered observer methods. This requirement came up a long ago and several approaches were proposed in the meantime, as documented by CDI-4.The Alpha1 release employs the general-purpose @Priority annotation to determine the order in which observer methods are to be notified.

There are many open questions about this feature. Here are some of the rules we set for the purpose of this experimental implementation. These are by no means set in stone but instead we encourage you to give us feedback on these:

  • Each observer method has a certain priority value. An observer method that does not define a priority explicitly is given the default priority which is 2500 (in the middle of the Interceptor.Priority.APPLICATION range)

  • The priority of an observer method determines the order. An observer method with lower value is called before an observer method with higher value

  • If multiple observer methods define the same priority value, the order is undefined

  • Observer methods priorities should match existing priority range convention

  • The ordering applies to both transactional and non-transactional observer methods

  • The priority annotation is applied on the event parameter (not the observer method)

Here’s an example:

public void sendWelcome(@Observes @Priority(APPLICATION + 800) @Registered User user) {
        // ...
    }

Note that javax.annotation.Priority can currently only be applied to types. Therefore, we temporarily created a substitute called org.jboss.weld.experimental.Priority which is identical to the original except that it can also be applied on parameters (of observer methods). The plan is to propagate this change back to javax.annotation.Priority. In the meantime, Weld’s twin gets the job done.

In addition to the @Priority annotation, the SPI was also enhanced to expose the priority of an observer method. We are not touching javax.enterprise interfaces just yet. Instead, the org.jboss.weld.experimental package contains proposals for how the new SPI should look like. As the package name suggests, this SPI is good for a test-drive but do not expect it to be stable. To use the experimental SPIs, you’ll need to add a dependency on weld-api.

Here’s an example of reading the observer method priority using the SPI in an Extension. ExperimentalProcessObserverMethod and ExperimentalObserverMethod interfaces come from the org.jboss.weld.experimental package.

public class SimpleExtension implements Extension {
    
        void observe(@Observes ExperimentalProcessObserverMethod<User, ?> event) {
            ExperimentalObserverMethod<User> observerMethod = event.getObserverMethod();
            this.priority = observerMethod.getPriority();
        }
    }

Use the corresponding CDI ticket for any feedback on these features.

Vetoing and modifying observer methods

We’ll stay with observer methods for a few more paragraphs. A request was raised in the CDI issue tracker for making it possible to disable an observer method using the ProcessObserverMethod SPI.

This Alpha1 release introduces the veto() methods (aligned with ProcessAnnotatedType.veto()) for this:

void disableDebuggingObservers(@Observes ExperimentalProcessObserverMethod<?, Debug> event) {
        if (projectStage != ProjectStage.Development) {
            event.veto();
        }
    }

In addition, the observer method metadata may be altered by an extension. This is done similarly to how InjectionTarget, InjectionPoint or BeanAttribute metadata are modified which is most often by wrapping the original object (decorator design pattern). Any piece of metadata (including aforementioned priority) can be altered. For example, the transaction phase:

void alterObserver(@Observes ExperimentalProcessObserverMethod<User, ?> event) {
        event.setObserverMethod(new ForwardingExperimentalObserverMethod<User>(event.getObserverMethod()) {
            @Override
            public TransactionPhase getTransactionPhase() {
                return TransactionPhase.AFTER_SUCCESS;
            }
        });
    }

Repeatable qualifiers and interceptor bindings

This release serves as a proof of concept for supporting repeating qualifiers and interceptor bindings. You can now fully utilize Java 8 features and use multiple qualifiers or interceptor bindings of the same type in the same location, for example:

public class School {
    
        @Produces
        @Speaks("English")
        @Speaks("French")
        public Student graduate() {
            // ...
        }
    }

Repeating qualifiers can be used for both bean and event resolution. See the Java documentation for how to define a repeating annotation.

Interceptor bindings in invocation context

This is an often recurring scenario. An interceptor binding defines several @NonBinding members which serve as configuration for the interceptor. How does an interceptor get hold of these values? This is not easy as the interceptor binding may often appear on the intercepted method, the class that defined it or may be inherited from another interceptor binding or stereotype (even transitively!). This problem gave birth to utilities such as this one. Still, even after all this effort the result is not entirely correct as it was obtained using Java reflection ignoring the fact that the interceptor binding may have been modified (e.g. using ProcessAnnotatedType).

This problem is being addressed as CDI-468. The Alpha1 release of Weld implements this feature and exposes new methods for obtaining interceptor bindings that are in effect for the interception. This is done using ExperimentalInvocationContext.getInterceptorBindings() or ExperimentalInvocationContext.getInterceptorBindingsByType().

Again, ExperimentalInvocationContext can be found in the org.jboss.weld.experimental package and depicts how the future version of javax.interceptor.InvocationContext could look like.

@Interceptor
    @Secure
    public class SecurityInterceptor {
    
        @Inject
        private User user;
    
        @AroundInvoke
        public Object intercept(ExperimentalInvocationContext ctx) throws Exception {
            Secure binding = ctx.getInterceptorBindingsByType(Secure.class).iterator().next();
            if (!user.getRoles().contains(binding.requireRole())) {
                throw new SecurityException();
            }
            return ctx.proceed();
        }
    }

Again, your feedback is welcome at CDI-468.

Changes in the Annotated layer

CDI provides an abstraction over the Reflection API - AnnotatedType and friends - which mostly allows extensions to alter the set of annotations present on a type, field, method, etc.

The Alpha1 release contains two minor additions to this API. Firstly, the API now supports Java 8 repeating annotations with a new method ExperimentalAnnotated.getAnnotationsByType(Class<T> annotationClass)

Secondly, it is now possible to access the java.lang.reflect.Parameter instance that is wrapped by AnnotatedParameter using ExperimentalAnnotatedParameter.getJavaParameter();

See CDI-471 and CDI-481 for details.


Weld.Next

2014-9-18   Jozef Hartinger

Today, we released Weld 2.2.5.Final. The fifth mostly bug-fixing release in the series addresses 23 issues. In addition, the Servlet module got an update and among other things now supports:

  • all three bean discovery modes (e.g. only explicitly annotated classes will be loaded as CDI beans)

  • bean archive isolation

  • using Jandex for faster deployment discovery

See the reference documentation for details.

Talking about documentation we are grateful to Antoine Sabot-Durand for migrating our reference documentation to AsciiDoc. We also thank our community contributors Antonin Stefanutti and Stefan Grossmann who contributed with pull requests.

Weld 3.0

In the coming months, our focus will be shifting towards Weld 3.0 - the future reference implementation of CDI 2.0. Initially, we plan on releasing Alpha releases every two weeks starting in early October.

The Alpha releases are likely to break compatibility from time to time and will often contain bleeding edge or prototype code. The goals is however to give the CDI community a chance to test drive the proposed changes in the CDI specification as soon as possible and give quick feedback to the CDI expert group.

Weld 3.0 will also be a good opportunity to get involved in the development of this open-source project. Interested? See our community web page for more details.


Weld 2.2 (CDI 1.2 reference implementation) released!

2014-4-15   Jozef Hartinger

Today we released Weld 2.2.0.Final - the reference implementation of Contexts and Dependency Injection for Java EE 1.2 (CDI 1.2). Besides implementing the changes in the specification, this release comes with several new features, performance improvements and more than 50 bug fixes.

CDI 1.2

CDI 1.2 is a maintenance release of the CDI specification. It contains a number of small fixes and clarifications as well as several enhancements. Most notable changes for application developers are related to the definition of bean defining annotations.

In CDI 1.2, the set of bean defining annotations now contains:

  • all the normal scope annotations (e.g. @RequestScoped, @ApplicationScoped, …​)

  • the built-in @Dependent scope

  • @Interceptor and @Decorator annotations

  • stereotype annotations

As a result, any class annotated with a bean defining annotation is by default recognized by the application server and registered as a CDI bean. The beans.xml file is optional.

In addition, there are other fixes, clarifications and minor enhancements in this maintenance release of the CDI specification. See the release notes for the complete list of changes.

Performance

Weld is now capable of using bytecode-scanning utilities, such as the Jandex tool, to speed up deployment. This is especially notable in extra large deployments (e.g. 5000+ classes) where we observed up to 20% faster deployment.

In addition, there are noticeable improvements in the following areas:

  • runtime performance of observers, interceptors and decorators

  • session replication overhead (failover)

  • memory consumption

Weld 2.2 on WildFly

WildFly does not come with Weld 2.2 support yet. It is however easy to patch an existing WildFly installation to use Weld 2.2.

To do so, follow these steps:

  1. Download a patch appropriate for the WildFly version (e.g. 8.0.0.Final) from the download section

  2. In the WildFly installation, run the CLI console

    sh jboss-cli.sh
  3. From the console run the following command:

    patch apply /path/to/wildfly-8.0.0.Final-weld-2.2.0.Final-patch.zip

Your installation is now patched!

Weld SE and Servlet

Weld comes with the SE module which allows CDI to be used in plain Java SE environment. In Weld 2.2 we added partial support for implicit bean archives. Partial support here means that the beans.xml file is still required, but bean-discovery-mode=”annotated” can be specified for Weld to only discover classes explicitly annotated with bean defining annotations (see above).

In addition to the Weld SE module, Weld also provides the Weld Servlet module which makes it possible to use Weld on top of a plain Servlet container, such as Apache Tomcat or Jetty.

In this release we simplified configuration by bootstrapping Weld using the ServletContainerInitializer.

Furthermore, Weld Servlet now supports Jetty 9.1. At the same time, the support for Tomcat 6 was dropped. To summarize, Weld Servlet is currently supported on the following Servlet containers:

  • Tomcat 7, 8

  • Jetty 7, 8, 9.0, 9.1

Acknowledgement

We greatly appreciate your contributions to this release. Big thanks go to: Martin Kouba, Matúš Abaffy, Matej Briškár, Ron Šmeral, Marek Schmidt, Marko Lukša, Tomáš Remeš, Stuart Douglas, Radoslav Husár, Max Pimm, Alexandre Gattiker, Antonin Stefanutti and Steve Moyer.