This lab takes you through how to update the MicroProfile Meeting application to add persistence.
At the time this lab was written MicroProfile had not defined a persistence mechanism. This is not because MicroProfile does not view persistence as important, but indicates the many equally valid choices that could be made for persistence in microservices. In this lab, Cloudant is used but there are other equally valid options like JPA, MongoDB, or Cassandra.
Adapted from the blog post: Writing a simple MicroProfile application: Adding persistence
-
Completed Part 1: MicroProfile Meeting Application
-
Eclipse IDE for Web Developers: Run the installer and select Eclipse IDE for Java EE developers. Note: these steps were tested on the 2018-09 version of Eclipse running on Linux and Liberty Developer Tools 18.0.0.3. Note: If you encounter an error message like
Could not initialize class org.codehaus.plexus.archiver.jar.JarArchiverplease see the Troubleshooting section. -
IBM Liberty Developer Tools (WDT)
- Start Eclipse
- Launch the Eclipse Marketplace: Help -> Eclipse Marketplace
- Search for IBM Liberty Developer Tools, and click Install with the defaults configuration selected
-
Install the IBM Cloud CLI
Run the following commands:
$ git clone https://github.com/IBM/microprofile-meeting-persistence.git
- In Eclipse, switch to the Git perspective.
- Click Clone a Git repository from the Git Repositories view.
- Enter URI
https://github.com/IBM/microprofile-meeting-persistence.git - Click Next, then click Next again accepting the defaults.
- From the Initial branch drop-down list, click master.
- Select Import all existing Eclipse projects after clone finishes, then click Finish.
- Switch to the Java EE perspective.
- The meetings project is automatically created in the Project Explorer view.
If you completed the previous labs and installed MongoDB, make sure MongoDB is running. If you are starting fresh, make sure you install MongoDB. Depending on what platform you are on the installation instructions may be different. For this exercise you should get the community version of MongoDB from the mongoDB download-center.
- Once installed you can run the MongoDB database daemon using:
mongod -dbpath <path to database>The database needs to be running for the application to work. If it is not running there will be a lot of noise in the server logs.
To start writing code, the Maven pom.xml needs to be updated to indicate the dependency on MongoDB:
- Open the
pom.xml. - On the editor select the Dependencies tab.
- On the Dependencies tab there are two sections, one for Dependencies, and the other for Dependency Management. Just to the right of the Dependencies box there is a Add button. Click the Add button.
- Enter a groupdId of
org.mongodb. - Enter a artifactId of
mongo-java-driver. - Enter a version of
2.14.3. - In the scope drop-down, select provided. This will allow the application to compile, but will prevent the Maven WAR packager putting the API in the WAR file. Later the build will be configured to make it available to the server.
- Click OK.
- Save the
pom.xml.
When updating the application, we need to convert between the MongoDB representation of data and the JSON-Processing one. Rather than scatter this around this can be placed in the MeetingsUtil class:
-
Open the
MeetingsUtilclass from meetings > Java Resources > src/main/java > net.wasdev.samples.microProfile.meetings > MeetingsUtil.java -
The first method that is needed takes a MongoDB
DBObjectand returns aJsonObject. At the beginning of the file, after the class definition, add the method declaration:
public static JsonObject meetingAsJsonObject(DBObject obj) {
// method body will go here
}- This introduces one new class, the
DBObjectclass is in thecom.mongodbpackage:
import com.mongodb.DBObject;- The first step in creating a new
JsonObjectis to create aJsonObjectBuilderusing theJSONclass. All of these are already used in the class so no new imports will be required. Add the following line to the start of the method:
public static JsonObject meetingAsJsonObject(DBObject obj) {
JsonObjectBuilder builder = Json.createObjectBuilder();- The JSON object for a meeting uses a field called
id. MongoDB uses_idfor the same function, soidin JSON will map to_idin MongoDB. To extract theidfield from theDBObjectand add it to the JSON, add this line to the method:
builder.add("id", (String) obj.get("_id"));- The
titlefield can be directly mapped from JSON to the MongoDB object. Thetitleis a String but, to ensure the right add method is called, cast it to a String. Add this line:
builder.add("title", (String) obj.get("title"));- The
durationfield is a Long. As before it needs to be cast to a Long to ensure the rightaddmethod is called:
builder.add("duration", (Long) obj.get("duration"));- If a meeting is running it’ll have a
meetingURLfield. If it isn’t running it’ll return null. If there is nomeetingURL field, we don’t want to add it to the meeting returned so we want to only add ifmeetingURLis non-null. Add this:
String meetingURL = (String) obj.get("meetingURL");
if (meetingURL != null)
builder.add("meetingURL", meetingURL);- Finally we need to return a
JsonObject. This can be obtained by calling thebuildmethod on theJsonObjectBuilder. Add this:
return builder.build();- Next, we need a method that does the opposite, mapping from a
JsonObjectto aDBObject. This method serves two purposes: It creates a newDBObjectand it merges aJsonObjectto an existingDBObject, so that it has two parameters rather than one. After the end of the previous method add this:
public static DBObject meetingAsMongo(JsonObject json, DBObject mongo) {
// method body will go here
}- The first thing to do in this new method is check if the
DBObjectpassed in is null. If it is null, a newDBObjectis created.DBObjectis abstract so aBasicDBObjectis what will be instantiated. At the beginning of the method add this (there will be a compile error after this but don’t worry, it’ll be fixed soon):
if (mongo == null) {
mongo = new BasicDBObject();- Next, the
idneeds to be moved from theJsonObjectto the newDBObject. This should only be done when a newDBObjectis created because, otherwise, a disconnect between theURLand theJsonObjectcould result in anidbeing incorrectly overwritten. Add this:
mongo.put("_id", json.getString("id"));
}- This introduced a new class, the
BasicDBObjectwhich is in thecom.mongodbpackage but needs to be imported:
import com.mongodb.BasicDBObject;- The
titlefield is a direct mapping from theJsonObjectto theDBObjectbut theJsonObjectcontains aJsonStringwhich needs to be converted to a String. ThetoStringmethod can’t be used for this because it wraps the String literal with quotes, which isn’t required here. FortunatelyJsonObjectprovides a convenientgetStringmethod for this:
mongo.put("title", json.getString("title"));- The
durationfield is also a direct mapping but it’s aJsonNumberin theJsonObject, which needs to be converted to a Long to go into theDBObject:
mongo.put("duration", ((JsonNumber) json.get("duration")).longValue());- This introduced the
JsonNumber, which needs to be imported:
import javax.json.JsonNumber;- We want to get the
meetingURLbut, since it might not be there, you can’t use thegetStringmethod because it’ll throw aNullPointerExceptionif there is no field with that name. To get around this, use thegetmethod, which returns aJsonString. A null check can then be performed and only if it is non-null will it be added to the JSON. ThegetStringmethod must be used since toString wraps the string in quotes:
JsonString jsonString = json.getJsonString("meetingURL");
if (jsonString != null) {
mongo.put("meetingURL", jsonString.getString());
}- This introduced the
jsonString, which needs to be imported:
import javax.json.JsonString;- Finally return the mongo object and save the file.
return mongo;The MeetingManager currently makes use of a ConcurrentMap to store the meetings. All the code that integrates with this needs to be updated. In this section this is done one step at a time; as a result there will be compilation errors until you get to the end:
-
Open the
MeetingManagerclass from meetings > Java Resources > src/main/java > net.wasdev.samples.microProfile.meetings > MeetingManager.java. -
After the class definition delete the following line from the file:
private ConcurrentMap<String, JsonObject> meetings = new ConcurrentHashMap<>();- To interact with MongoDB, an instance of
DBneeds to be injected. This can be done using the@Resourceannotation which can identify which resource fromJNDIto inject. Add the code where theConcurrentMapwas removed (in the previous step):
@Resource(name="mongo/sampledb")
private DB meetings;- This pulls in two new classes. The MongoDB
DBclass and the Java EE@Resourceannotation. These need to be imported:
import javax.annotation.Resource;
import com.mongodb.DB;- MongoDB stores entries in a collection in the database. The first thing to do when writing or reading is to select the collection. To simplify code later on, let’s create a convenience method to get the collection:
public DBCollection getColl() {
return meetings.getCollection("meetings");
}- This introduces a new class, the
DBCollectionclass, which is in the com.mongodb package. This needs to be imported:
import com.mongodb.DBCollection;- Find and edit the
addmethod:
- Remove the method body from the
addmethod. This will be replaced to update the database.
public void add(JsonObject meeting) {
// code will be added here
}- First get the collection.
DBCollection coll = getColl();- The method is given a
JsonObjectand we need aDBObjectfor MongoDB, so we need to convert. The API can take an existing entry from the database, so first lets see if we can find something from the database using the findOne method:
DBObject existing = coll.findOne(meeting.getString("id"));- This introduces a new class, the
DBObjectclass which is in thecom.mongodbpackage. This needs to be imported:
import com.mongodb.DBObject;- Next call the
MeetingsUtilconvenience method to convert fromJsonObjecttoDBObject:
DBObject obj = MeetingsUtil.meetingAsMongo(meeting, existing);- Finally save the new or changed
DBObjectback into the database:
coll.save(obj);- Find and update the
getmethod:
- Remove the method body from the
getmethod (this will be replaced to fetch from the database):
public JsonObject get(String id) {
// code will be added here
}- To get a single entry the collection needs to be obtained, an entry found by id, and then converted to a
JsonObjectusing the utility method created earlier. Add the following line to thegetmethod:
return MeetingsUtil.meetingAsJsonObject(getColl().findOne(id));- Find and update the
listmethod. Thelistmethod is slightly more complicated to update. The general structure stays the same but for loop will change. To iterate over entries in a collection aDBCursoris used and that returns aDBObject. TheDBObjectthen needs to be converted to aJsonObject. Replace the existing loop that looks like this:
for (JsonObject meeting : meetings.values()) {
results.add(meeting);
}with this one
for (DBObject meeting : getColl().find()) {
results.add(MeetingsUtil.meetingAsJsonObject(meeting));
}- Find and update the
startMeetingmethod:
- This change will radically simplify the code because there is no need to create and merge multiple
JsonObjects. Instead, you can simply add themeetingURLto the existingDBObject. Theidandurlwill still need to be fetched from theJsonObject. Remove the following lines from thestartMeetingmethod:
JsonObject existingMeeting = meetings.get(id);
JsonObject updatedMeeting = MeetingsUtil.createJsonFrom(existingMeeting).add("meetingURL", url).build();
meetings.replace(id, existingMeeting, updatedMeeting);- After the
idandurlare fetched, replace the removed code with the following four lines to get a collection, find the meeting entry, set themeetingURL, and then save it back to the database:
DBCollection coll = getColl();
DBObject obj = coll.findOne(id);
obj.put("meetingURL", url);
coll.save(obj);- Save the file.
The MeetingManager is now able to persist to a MongoDB database and back. However, the server still needs to be configured to enable it. This consists of two parts: First, server configuration and, second, getting the server runtime set up.
The server configuration is part of the project so first let’s configure that:
-
Open the
server.xmlfrom src > main > liberty > config > server.xml. -
Add
mongodb-2.0as a new feature. It should look like this:
<featureManager>
<feature>microProfile-2.0</feature>
<feature>mongodb-2.0</feature>
</featureManager>- A shared library needs to be defined to be used by the application and the runtime for defining the MongoDB resources:
<library id="mongodriver">
<file name="${shared.resource.dir}/mongo-java-driver.jar"/>
</library>- Next the
mongoneeds to be defined. This tells the server where themongoserver instance is running:
<mongo id="mongo" libraryRef="mongodriver">
<ports>27017</ports>
<hostNames>localhost</hostNames>
</mongo>- Next, define the database:
<mongoDB databaseName="meetings" jndiName="mongo/sampledb" mongoRef="mongo"/>- Finally, configure the application so it can see the MongoDB classes:
<webApplication location="meetings-${project.version}.war">
<classloader commonLibraryRef="mongodriver"/>
</webApplication>- Save the file.
The next step is to configure the Maven build to make sure all the resources end up in the right place.
The Maven POM needs to be updated to do a few things: It needs to copy the MongoDB Java driver into the Liberty server, define an additional bootstrap property, copy the application to a different location, and ensure that the mongodb-2.0 feature is installed:
-
Open the
pom.xmlin the root of the project. -
Select the
pom.xmltab in the editor.
- Search the file for the string
maven-dependency-pluginyou should see this in the file:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-server-files</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>server-snippet</includeArtifactIds>
<prependGroupId>true</prependGroupId>
<outputDirectory>${project.build.directory}/wlp/usr/servers/${wlpServerName}/configDropins/defaults</outputDirectory>
</configuration>
</execution>
</executions>This is copying server snippets from dependencies into the server configuration directory. We are going to add to it instructions to download the mongo-java-driver, copy it to the usr/shared/resources folder, and strip the version off the JAR file name. This last part means we don’t have to remember to update the server.xml every time the dependency version is upgraded.
- To add these additional instructions, add the following lines after the
</execution>closing tag:
<execution>
<id>copy-mongodb-dependency</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>mongo-java-driver</includeArtifactIds>
<outputDirectory>${project.build.directory}/wlp/usr/shared/resources/</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>The server.xml references the WAR by artifact name and version. The version is referenced using a variable which needs to be provided to the server. This can easily be done using the bootstrap.properties:
- Search the
pom.xmlfile for the string<bootstrapProperties>. You should see this in the file:
<bootstrapProperties>
<default.http.port>${testServerHttpPort}</default.http.port>
<default.https.port>${testServerHttpsPort}</default.https.port>
</bootstrapProperties>- Before the closing add the following line:
<project.version>${project.version}</project.version>The Maven POM deploys the application into the dropins folder of the Liberty server but this doesn’t allow a shared library to be used. So, instead, the application needs to be copied to the apps folder:
- Search in the
pom.xmlfor the stringdropins, you should see this:
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/wlp/usr/servers/${wlpServerName}/dropins</outputDirectory>- Update the word dropins to be apps. It should look like this:
<configuration>
<outputDirectory>${project.build.directory}/wlp/usr/servers/${wlpServerName}/apps</outputDirectory>The mongodb-2.0 feature is not in the Liberty server installations that are stored in the Maven repository so it needs to be downloaded from the Liberty repository at build time:
- Search the
pom.xmlfor the stringpackage-server, you should see this:
<execution>
<id>package-app</id>
<phase>package</phase>
<goals>
<goal>package-server</goal>
</goals>
</execution>
</executions>- Just before the
</executions>tag, add the following lines to cause themongodb-2.0feature to be installed:
<execution>
<id>install-feature</id>
<phase>package</phase>
<goals>
<goal>install-feature</goal>
</goals>
<configuration>
<features>
<acceptLicense>true</acceptLicense>
<feature>mongodb-2.0</feature>
</features>
</configuration>
</execution>- Save the
pom.xml.
There are two ways to get the application running from within WDT:
- The first is to use Maven to build and run the project:
- Run the Maven
installgoal to build and test the project: Right-click pom.xml in themeetingsproject, click Run As… > Maven Build…, then in the Goals field typeinstalland click Run. The first time you run this goal, it might take a few minutes to download the Liberty dependencies. - Run a Maven build for the
liberty:start-server goal: Right-click pom.xml, click Run As… > Maven Build, then in the Goals field, typeliberty:start-serverand click Run. This starts the server in the background. - Open the application, which is available at
http://localhost:9080/meetings/. - To stop the server again, run the
liberty:stop-serverbuild goal.
- The second way is to right-click the
meetingsproject and select Run As… > Run on Server but there are a few things to note if you do this. WDT doesn’t automatically add the MicroProfile features as you would expect so you need to manually add those. Also, any changes to the configuration insrc/main/liberty/configwon’t be picked up unless you add an include.
Find out more about MicroProfile and WebSphere Liberty.
You can run your application on IBM Cloud using Cloud Foundry.
- Login to your IBM cloud account
ibmcloud cf login
- Push your application to IBM Cloud (specifying no start)
$ ibmcloud cf push --no-start <yourappname> -p wlp/usr/servers/meetingsServer
-
While your app is deploying, create a Compose MongoDB instance on IBM Cloud. Name the service instance
mongo/sampledb, leave all other default configurations. Click Create. -
From your IBM Cloud Dashboard, find your deployed app and click on it.
-
On the left-hand side, click Connections. Then click Connect existing.
-
Find your
mongo/sampledband click on it. It will prompt you to restage the app, click Restage. -
Once the app finishes restaging, you can revisit the route/url. Be sure to add
/meetingsto the end of the route to hit the home page of your app.
Part 3: MicroProfile Application - Using Java EE Concurrency