News - tagged as "release"

Weld 3.0.0.Alpha8

2015-4-21   release , cdi2   Jozef Hartinger

Weld 3.0.0.Alpha8 has been released. The main change is the enhanced API for using Weld in Java SE environment. In addition, this release comes with several weld-probe improvements.

Enhanced API for Weld SE

Weld has provided support for the Java SE environment for a long time with the weld-se module. The API provides an easy way for an application to initialize Weld and use it in a standalone mode. On initialization Weld SE scans the classpath for bean archives with the beans.xml file, similarly to how it’s done in the Java EE environment.

In this release we are extending the API further. This is partially inspired by the current discussion in the CDI expert group where a standardized CDI API for Java SE is being proposed as part of CDI-26.

The following code snippet shows the new API in action:

Weld builder = new Weld()
        .disableDiscovery()
        .packages(Main.class, Utils.class)
        .interceptors(TransactionalInterceptor.class)
        .property("org.jboss.weld.construction.relaxed", true);
    
    try (WeldContainer weld = builder.initialize()) {
        MyBean bean = weld.select(MyBean.class).get();
        System.out.println(bean.computeResult());
    }

There are several new things to notice:

  • the Weld class is used as a builder to configure Weld before it is initialized

  • automatic scanning can be disabled

  • instead of scanning, classes or packages can be selected explicitly. All classes in those packages will be managed by Weld

  • interceptors, decorators, extensions and Weld-specific configuration options can be specified using the builder

  • WeldContainer now implements AutoCloseable and can therefore be used in a try-with-resources block. At any time that execution gets outside of the code block, the Weld instance is shut down and all managed instances are safely destroyed.

It is also possible to start multiple independent Weld instances:

new Weld().disableDiscovery().containerId("one").beanClasses(MyBean.class).initialize();
    new Weld().disableDiscovery().containerId("two").beanClasses(OtherBean.class).initialize();
    
    MyBean bean = WeldContainer.instance("one").select(MyBean.class).get();
    System.out.println(bean.computeResult());
    
    WeldContainer.instance("one").shutdown();
    WeldContainer.instance("two").shutdown();

Here, two independent WeldContainer instances are initialized. Each of them is given a unique ID. The ID can subsequently be used to obtain a WeldContainer reference in a different place of the code. One possible use-case this enables is for a library or framework (e.g. a testing framework) to use an embedded instance of Weld internally for its own needs (dependency injection, events, extensibility). This instance would not interfere with the Weld instance used by the application.

Obviously, automatic classpath scanning can still be used as before:

try (WeldContainer weld = new Weld().enableDiscovery().initialize()) {
        MyBean bean = weld.select(MyBean.class).get();
        System.out.println(bean.computeResult());
    }

Update:

To play with the new API use the following dependency in you Maven project:

<dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se-core</artifactId>
        <version>3.0.0.Alpha8</version>
    </dependency>

Aforementioned classes are from the org.jboss.weld.environment.se package.

Weld Probe Enhancements

Since the last Alpha releases there were several enhancements to Weld Probe. If you are not familiar with Weld Probe, check this introductory blog post first.

A new feature of Probe is that, when the development mode is enabled, it now embeds a tiny information bar directly into the application’s HTML output. That makes it easy to navigate to Probe directly from the application anytime. Furthermore, if invocation tracking is enabled, the information bar helps navigate directly to the invocation tree related to the request that rendered the output.

Weld Probe information bar embedded in application's HTML output


Additionally, the following Probe improvements were implemented:

  • tracked invocations are now grouped into a invocation tree instead of being tracked in isolation

  • a special type of edges is now used in the overview graph to represent a "declared by" relation (when a bean declares a producer method or field)

  • Instance<?> injection points are now treated specially - a resolved bean is show as injection point’s dependency

See this live demo and try Weld Probe yourself.

[ Experimental API documentation ] [ Release notes ] [ Distribution ] [ Patch for Wildfly (8.2, 9 Beta) ]


Weld 3.0.0.Alpha5

2015-2-25   release , cdi2   Jozef Hartinger

Weld 3.0.0.Alpha5, the latest release in the series of CDI 2.0 prototypes, has been released. The highlights of the release include new Bean builder API and many improvements to Weld Probe.

Bean Builder API

The common way for frameworks and libraries to expose their API to applications via CDI is by using producer methods. An alternative approach is a custom implementation of the javax.enterprise.inject.spi.Bean interface that is registered by a CDI portable extension. This latter approach gives the extension more control but is also less straightforward.

One of the goals of CDI 2.0 is to fill this gap and simplify extension development. This Weld release comes with a preview of the Bean builder API. The API is inspired by Bean builder API provided by DeltaSpike. In addition, Java 8 lambdas are used in the API to simplify the entire process even further.

Here’s an example of a simple extension that builds and registers a bean:

void register(@Observes ExperimentalAfterBeanDiscovery event) {
        event.addBean()
            .addType(Integer.class)
            .addQualifier(Random.LITERAL)
            .produceWith(() -> new java.util.Random().nextInt(1000));
    }

That’s it! Likewise, it is easy to can define a bean with custom disposal logic using a lambda expression:

void register(@Observes ExperimentalAfterBeanDiscovery event) {
        event.addBean()
            .addType(Connection.class)
            .produceWith(this::newConnection)
            .disposeWith(connection -> connection.close());
    }

See the BeanBuilder Javadoc for more information.

Event monitoring with Probe

We introduced Probe in the previous blog post. This release adds event monitoring view to Probe. In this view both system and application events are displayed. The events can be filtered by type and qualifiers. It is also possible to track which observer methods a particular event was delivered to.

Weld Probe event monitoring

Further improvements

In addition, there were a couple of minor improvements to Probe, namely:

  • better filtering of invocation trees - Probe now searches the whole tree, not just the entry points

  • the context view now displays the conversation context

  • Probe is now more careful and does not try to bind interceptors to classes that are not proxyable

  • all the configuration options for Probe are now documented

If you haven’t tried Probe yet check this demo running on OpenShift. The easiest way to get Probe to run locally is to patch WildFly with a Weld patch. If you’re not familiar with patching WildFly, check Markus’s tutorial.

Relaxed construction

CDI requires that beans that are normal-scoped, intercepted or decorated always define a no-argument constructor. This requirement applies even if the bean already defines an @Inject annotated constructor with parameters. This is purely a technical requirement implied by how Java allocates class instances.

Weld is however able to operate fine even if this requirement is not met. Weld uses special non-portable JVM APIs that allow it to allocate proxy instances without calling proxy’s constructor. This mode is non-portable and needs to be explicitly enabled using a configuration option.

This mode is not new but has been neglected in the past. For this release we added a bunch of tests for this mode, fixed a lot of bugs and enabled it by default in Weld-SE. Any feedback on this mode is appreciated.


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. You can play with Probe using this simple CDI application hosted on OpenShift. Just append /weld-probe to the URL.

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