Apache OpenWebBeans, an implementation of JSR 365 dependency injection (CDI 2.0), recently released version 1.0 of their Meecrowave microservices server that provides built-in support for the Java Persistence API (JPA), the Java Transaction API (JTA), and OAuth2 without requiring a full container such as Java EE.
Meecrowave is built on top of some existing Apache projects, including Tomcat (servlets), OpenWebBeans (CDI), Johnzon (JSON-P and JSON-B), and CFX (JAX-RS).
Meecrowave, initially released in January 2017, may be used for microservices and standalone applications. As defined in their blog:
Meecrowave can be either started via a maven plugin (for ease of development), or programmatically as an embedded server, bundled as an application with your business code or as a runner to start up a portable WAR or JAR applications.
Meecrowave is comprised of the following components:
The core component defines a range of configuration and command line interface (CLI) options. Custom CLI options may also be defined by implementing the Cli.Options
interface and using the @CliOption
annotation as shown below.
The Maven and Gradle components provide the properties that correspond with the core configuration and CLI options, and allow for changes to the default values.
Getting Started
The following JAX-RS example demonstrates how to get started with Meecrowave.
The Defaults
class defines a default first and last name for the example application. Note the use of extending the CLI with an option named app-default-name
along with its corresponding description.
public class Defaults implements Cli.Options {
@CliOption(name="app-default-name", description="The default first and last names for the app.")
private String firstName="Michael";
private String lastName="Redlich";
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
The DefaultsProducer
class injects an instance of Meecrowave’s Builder
class for initiating Meecrowave. The getExtension()
method captures the name and description defined in the @CliOption
annotation from the Defaults
class.
@Dependent
public class DefaultsProducer {
@Inject
private Meecrowave.Builder builder;
@Produces
@ApplicationScoped
public Defaults defaults() {
return builder.getExtension(Defaults.class);
}
}
The SimpleApplication
class defines the main application path, redlich
:
@Dependent
@ApplicationPath("redlich")
public class SimpleApplication extends Application {
}
The SimpleEndpoint
class defines the endpoint, meecrowave
, URL query parameters, firstName
and lastName
, and an inner class, Hello
, a Java bean used to model a first and last name.
@Path("meecrowave")
@ApplicationScoped
public class SimpleEndpoint {
@Inject
private Defaults defaults;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Hello sayFullName(@QueryParam("firstName") final String firstName,@QueryParam("lastName") final String lastName) {
return new Hello(ofNullable(firstName).orElse(defaults.getFirstName()),ofNullable(lastName).orElse(defaults.getLastName()));
}
public static class Hello {
private String firstName;
private String lastName;
public Hello() {
}
private Hello(final String firstName,final String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
}
}
Compile and run with Maven:
mvn clean package meecrowave:run
Once the server is running, the complete URL is comprised of the main application path and endpoint that may be entered in the browser:
http://localhost:8080/redlich/meecrowave
Note the default values of “Michael” and “Redlich” are displayed. URL query parameters may be used to change the default values defined in firstName
and lastName
. The following example changes only the first name via the URL:
http://localhost:8080/redlich/meecrowave?firstName=Rowena
This next example demonstrates how to change both first and last names via the URL:
http://localhost:8080/redlich/meecrowave?firstName=Barry&lastName=Burd
The entire sample application can be found on GitHub.
Meecrowave also has companion projects:
- Apache DeltaSpike (configuration, exception handling, advanced CDI)
- Apache Sirona (monitoring)
Support for the new CDI 2.0 specification is gaining traction, and Apache recently released OpenWebBeans 2.0.0 that was designed to work with the new CDI 2.0 specification.