Dropwizard & IBM Bluemix

IBM Bluemix allows developers to create and deploy applications on the cloud while offering services to integrate into your application. While there is extensive supporting documentation to help you use IBM Bluemix as a cloud platform, our team overcame some hiccups and roadblocks while integrating Bluemix’s offered services and deploying our Dropwizard application to this platform. This blog post will outline a few of those challenges and the steps we took to successfully move past them in hopes to help fellow developers who are interested in using this cloud solution.

Deploying a Dropwizard Application to IBM Bluemix

Step 1

To deploy your application, certain requirements are to be followed to have your app successfully push and run in Bluemix. The first is to ensure that your application and all necessary files are packaged into a zip file. In our case, we used Gradle to build and run our app, so we used Gradle’s Distribution Plugin to package all of our necessary files into one zip file to push to Bluemix. Within this distribution file, we included the latest build of our project using a Gradle Shadow plugin and our necessary supporting files and libraries such as firebase credentials and Bluemix service credentials that we integrated into our project (in our case, MySQL and Object Storage). After running the Bluemix distributions task, your application is now packaged into a zip file; first step done!

Step 2

Next, another crucial feature to include before pushing to bluemix is a manifest.yml file. When using the command line to push your project, application manifests instruct the command what to do with your application. You can pass instructions for your application within this file such as how much memory to allocate and your app’s hostname. It is very important that you include two pieces of instructions; the path to the zip file that you created in Step 1 and the Cloud Foundry Java Buildpack. The custom buildpack is important to include so that you can properly run your JVM application on Bluemix. The manifest below shows proper conventions for your manifest.yml file.

applications:
 -  name: app_name
	host: app_name
	buildpack: https://github.com/cloudfoundry/java-buildpack.git
	path: path/to/your/zip/file

Finally, your application is ready to be pushed to bluemix. To do this, download the Cloud Foundry CLI in order to successfully deploy your app.

After downloading the CLI, change to the directory where your application is located.

cd your_application_directory

Connect and login to Bluemix.

cf api https://api.ng.bluemix.net
cf login -u <username> -o <org_name> -s <space_name>

Push your application to Bluemix.

cf push <app_name>

Once you push your project, your application will either successfully run or fail. If it is unable to run, use the following command to view logs and debug the issues:

cf logs bni --recent

If it is able to run, you have successfully deployed your application to IBM Bluemix!

Using Bluemix Services: Object Storage

One of the challenges in using IBM Bluemix to develop an application is using and linking Object Storage to your project. Object Storage can be used to store files and access them from your application. For example, files can be uploaded from your application into Object Storage. You can also fetch them in your application from Object Storage.

First, let’s start with some background to Object Storage. Within this service, files are considered “objects” and are stored in specified “containers” that you create. After initializing this service to your application via the Bluemix console, you will be provided with service credentials which will be used to gain access to your Object Storage instance. These credentials will be used in every action within your API that requires access to Object Storage. For example, if you want a user to be able to upload a file to or view a file from Object Storage, credentials will need to be set.

Uploading a File to Object Storage

We’ll start with how to tackle a task to allow a user to upload a file and have it stored in Object Storage. The most important step is to initialize your Object Storage instance in your Java/Dropwizard application. To do this, credentials must be set which can be done with the helpful hand of OpenStack4j. Here’s a helpful tutorial that we followed to connect to Bluemix Object Storage using OpenStack4j.

Within your application, set these credentials and create the OSClient object within your main Dropwizard Application class to ensure that the object is created when the server is running.

// DropwizardApplication.class

// within run() method
OSClient.OSClientV3 os = OSFactory.builderV3()
	.endpoint(<auth_url>)
	.credentials(<userId>, <password>)
	.scopeToProject(Identifier.byId(<project_id>))
	.authenticate();

Next, in order to create a method to upload the inputted file to Object Storage, we passed the token belonging to our OSClient object from our Application class to the appropriate DAO class where this method was to be created.

// after creating os object
Token token = os.getToken();

final SampleDAO sampleDAO = new SampleDAO(token);

In our case, there was trial and error in this process; at first, we simply tried to pass the OSClient object directly to the DAO class but doing so didn’t maintain its authorization. Because of this, it is best to pass the token belonging to the authorized object and creating that object again within the DAO class.

public class SampleDAO {

	private Token token;

	public SampleDAO(Token token) { this.token = token; }

	public saveFile(File file) {
		// Token from authorized current session is used to create authorized client
		OSClient.OSClientV3 os = OSFactory.clientFromToken(token);

		// Continued saving of file to Object Storage
	}
}

From here, you now have an authorized client that is connected to your Object Storage instance and can use any of the OpenStack4j commands to work with it.

For example, to save the file to Object Storage, use the command:

os.objectStorage().objects().put(<container_name>, <file_name>, Payloads.create(<file>))

You have now successfully connected to Object Storage and saved a file within your created container!

Downloading a File from Object Storage

Another challenge is loading a file from Object Storage and displaying it within your application. Bluemix has documentation on how to create temporary URLs for objects within Object Storage but it is more efficient to have a permanent URL so that an admin user doesn’t have to keep creating URLs. The only way to set a permanent URL is to take away the authorization component and make your container within Object Storage public.
To begin, ensure that you are in your application directory and follow Bluemix’s documentation on configuring its CLI to be able to use commands that connect to your Object Storage instance. The following example is taken from Bluemix documentation:

export OS_USER_ID=24a20b8e4e724f5fa9e7bfdc79ca7e85
export OS_PASSWORD=*******
export OS_PROJECT_ID=383ec90b22ff4ba4a78636f4e989d5b1
export OS_AUTH_URL=https://identity.open.softlayer.com/v3
export OS_REGION_NAME=dallas
export OS_IDENTITY_API_VERSION=3
export OS_AUTH_VERSION=3

swift auth

After setting all of your Object Storage credentials and using the swift auth command, the CLI will give you an authorization token that you’ll need next.

To make your container public, enter the following command:

swift post <container_name>> --read-acl “.r:*,.rlistings

To create a permanent URL for your container:

curl -X GET "https://dal.objectstorage.open.softlayer.com/v1/AUTH_<project_id>/<container-name>" -H "X-Auth-Token:<auth_token>" -H "X-Container-Read: .r:*,.rlistings

To get access to a specific object, add the file name to the end of your set URL.

https://dal.objectstorage.open.softlayer.com/v1/AUTH_<project_id>/<container_name>/<file_name>

With this permanent URL you are able to load a file from Bluemix Object Storage in your own application.

Wrap-up

To conclude, there were some challenges integrating IBM Bluemix as a cloud solution but we were able to overcome them and are quite happy with the result (and what developer doesn’t hit roadblocks?). Hopefully this post can help a fellow developer work through some hiccups and create their own application using a new cloud platform.

Midori Takeuchi

Midori Takeuchi

Midori is a Software Developer with A.Y. Technologies. She is a current fourth year UBC student with a major in Computer Science. With previous experience in IT and Project Management, she hopes to continue to broaden her passion in mobile development with software driven roles. Along with Midori’s love for code, she is also a devoted spinner and coffee enthusiast.
Midori Takeuchi

Latest posts by Midori Takeuchi (see all)

2 Comments
  1. David Soroko 2 years ago

    Does the deployed Dropwizard app actually works? As ports are managed by Bluemix, how do you indicate to Dropwizard on which port it should be running?

    • Author
      Midori Takeuchi 2 years ago

      Hi David!

      When Dropwizard runs a service such as the server command, it looks for a necessary YML file which contains instructions to run your application. In our case, we specified setup instructions for our database and server (where we specified the port), as well as credentials for our IBM account.

      Here is an example YML file provided by Dropwizard:
      https://github.com/dropwizard/dropwizard/blob/master/dropwizard-example/example.yml

      When packaging your application as specified in Step 1 above, ensure that your created YML file is included. This way, once you’ve packaged your project and deployed to Bluemix, your application will successfully run.

Leave a reply

Your email address will not be published. Required fields are marked *

©2019 A.Y.Technologies

Log in with your credentials

Forgot your details?