Enterprise Java

Microservices Series: MicroProfile and Apache TomEE

Introduction

MicroProfile is an initiative started in September 2016 by group of prominent vendors to build a Microservices architecture based on JEE platform. The mission is to optimize Enterprise Java for a Microservices architecture. The developers can make use of this architecture to build and develop Microservices applications in a standardized way using Enterprise Java platform.

The API building will be part of JCP program. MicroProfile is currently built on minimum footprint technology stack that includes JAX-RS, CDI and JSONP. More capabilities will be added going forward to build a more robust Microservices platform. The MicroProfile project is still a work in progress and its current release 1.2 is based on Eclipse Foundation under Apache 2.0 license. More information can be found at https://microprofile.io/

In this article, we will explore how to build and deploy a JEE based Microservices RESTful endpoint using one such MicroProfile runtime named Apache TomEE. Apache TomEE is the open source project that spawned the company Tomitribe which is one of the primary vendor contributing towards MicroProfile project.

Apache TomEE Microprofile

The example here will focus on building RESTful endpoint that will retrieve all customer expenses for a particular customer. Let’s quickly jump to the code:

@ApplicationScoped
@Produces({MediaType.APPLICATION_JSON})
@Path("/expenses")
public class ExpenseEndpoint {
	
	private Map<String, List<Expense>> expenses;
	
    @GET
	@Path("{customerId}")
    public List<Expense> getExpenses(@PathParam("customerId") String customerId) throws Exception {
		expenses = loadExpenses();
        return expenses.get(customerId);
    }
...

The above code is a simple JAX-RS ExpenseEndpoint endpoint that allows you to retrieve customer expenses based on id. We will build and publish this endpoint using Tomee runtime. The build narrative is part of POM file that uses TomEE maven plugin which enables us to build and deploy our ExpenseEndpoint microservice.

....
	<dependencies>
		<dependency>
            <groupId>org.apache.tomee</groupId>
            <artifactId>javaee-api</artifactId>
			<version>7.0</version>
        </dependency>
		<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
			<version>2.8.7</version>
        </dependency>
	</dependencies>
	<build>
		<finalName>tomeeweb</finalName>
		<plugins>
		...
			<plugin>
                <groupId>org.apache.tomee.maven</groupId>
                <artifactId>tomee-maven-plugin</artifactId>
                <version>7.0.1</version>
				<configuration>
                    <tomeeClassifier>webprofile</tomeeClassifier>
                </configuration>
				<executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
....

The above POM file makes use of exec goal of the package phase to build the jar file. The very fact that our endpoint will be an independent microservice that should be capable of being deployed and run in isolation, it needs to be embedded with a server runtime package, more as a fat jar. The other alternative can be to build as a docker image. To build our code, run the following command:

mvn package

The above maven command will run the exec goal of the TomEE maven plugin and build the fat jar. The fat jar will internally contain our mini web app runtime and this can be perceived as micro profile. To run our application, use the following command:

java -jar target\<buildname>-exec.jar

This will start the TomEE runtime, deploy our ExpenseEndpoint RESTful service. You can go to the browser and test the endpoint with the following URL: http://localhost:8080/expenses/C001 and it will throw you back the JSON string with all the expense details of the customer id C001.

As stated earlier MicroProfile is still a work in progress. The above was just one micro profile from Apache TomEE based on JAX-RS, JSON and CDI as a minimum API stack. MicroProfle project will continue its evolution through open collaboration and discussions within the community of developers. We have the larger message: JEE is here to stay!

Code for this article can be found at https://github.com/rhathi/microprofile

Reference: Microservices Series: MicroProfile and Apache TomEE from our JCG partner Rajeev Hathi at the TECH ORGAN blog.

Rajeev Hathi

Rajeev is a senior Java architect and developer. He has been designing and developing business applications for various companies (both product and services). He is co-author of the book titled 'Apache CXF Web Service Development' and shares his technical knowledge through his blog platform techorgan.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button