Bluemix relies on Cloud Foundry, an open source platform for creating, deploying, and managing apps within the cloud.
Usually, users work together with an app in Bluemix by way of the Cloud Foundry command line interface (CLI) with the cf command. Through the CLI you'll be able to run a number of operations in Bluemix such as cf push, cf delete, and cf start/stop.
Along with the CLI, Cloud Foundry provides different tools and libraries that you need to use to create and handle apps within the cloud. This tutorial shows the way to use the Cloud Foundry Javaâ„¢ client library with Bluemix.
Cloud Foundry Java client library with Bluemix,Java client library with Bluemix,client library with Bluemix,Cloud Foundry Java,IBM Bluemix,how to Create and manage cloud applications using Java,Create and manage cloud applications using Java,manage cloud applications using Java,cloud applications using Java,,

What you need to build your application

Step 1. Get the library

To use the Cloud Foundry Java Client Library in your project, you must add the following dependency to the pom.xml file:

<dependencies>
   <dependency>
    <groupId>org.cloudfoundry</groupId>
    <artifactId>cloudfoundry-client-lib</artifactId>
    <version>1.0.2</version>
   </dependency>
  </dependencies>

  You can also access and download the library's source code on Github.

Step 2. Create and manage apps in Bluemix

Now let's explore how to create and manage apps in Bluemix by using the Cloud Foundry Java Client Library.

Log in to Bluemix

You need to log in to Bluemix to be able to create or manage your apps. Create an instance of the CloudFoundryClient class with your credentials and the URL to access the Bluemix API (for example, https://api.ng.bluemix.net).
You must also provide the organization and the space to be used. By default, the space is dev and the organization is the same as the user ID.

String user = "user@email.com";
String password = "password";
String orgName = user;
String spaceName = "dev";

// login
CloudCredentials credentials = new CloudCredentials(user, password);
URL target = URI.create("https://api.ng.bluemix.net").toURL();
CloudFoundryClient client = new CloudFoundryClient(credentials, target, orgName, spaceName);
client.login();
At the end, after creating or managing Bluemix apps, you need to log out:

client.logout();

Create a new app

The CloudFoundryClient class provides several methods to create and manage apps in the cloud. To create a new app, call the createApplication method and provide:

  • A name for the new application
  • A list with the URL to be used to access the application, such as http://<appname>.mybluemix.net
  • The amount of memory in MB (128, 256, 512, and so on) available to the app
  • A list with the services that the application will use
  • A Staging object where you specify the buildpack and, if necessary, the command to start the app

To find the buildpacks that are available in Bluemix, use the cf buildpacks command.
For example, some of the buildpacks are: liberty-for-java, sdk-for-nodejs, java_buildpack, and ruby_buildpack.
The code below shows how to create a NodeJS application:

String appName = "my-node-app";
String baseUrl = "http://" + appName + ".mybluemix.net/";
List<String> uris = Arrays.asList(baseUrl);

Integer memory  = 128;
List<String> serviceNames = null;

String command = "node app.js";
String buildpack = "sdk-for-nodejs";

// creating a new application
client.createApplication(appName, new Staging(command, buildpack), memory, uris, serviceNames);
After creating the app, upload the source code or binary that is to be used to Bluemix:

// uploading the zip file
File appFile = new File("nodeApp.zip");
client.uploadApplication(appName, appFile);
Finally, you can start the app:

// starting the application
client.startApplication(appName);

Manage an app

You can use the CloudFoundryClient class to perform several other operations on Bluemix, including: Delete an app, stop an app, create a service, and bind a service to an app. For example, the following code shows how to create and manage a Java web app in Bluemix that uses an SQL database service:

// creating the db service
CloudService service = new CloudService();
service.setLabel("sqldb");
service.setName("mydb");
service.setPlan("sqldb_small");
client.createService(service);

// creating a new application
String appName = "my-java-web-app";
String baseUrl = "http://" + appName + ".mybluemix.net/";
List<String> uris = Arrays.asList(baseUrl);
List<String> serviceNames =  Arrays.asList("mydb");
client.createApplication(appName, new Staging(null, "liberty-for-java"),
    512, uris, serviceNames);

// uploading the war file
File warFile = new File("webApp.war");
client.uploadApplication(appName, warFile);

// starting the application
client.startApplication(appName);

// checking the application state
Assert.assertEquals("STARTED", client.getApplication(appName).getState().toString());

// stopping the application
client.stopApplication(appName);
Assert.assertEquals("STOPPED", client.getApplication(appName).getState().toString());
   
// updating the application
File warFile2 = new File("webApp2.war");
client.uploadApplication(appName, warFile2);

// binding other service to the application
client.bindService(appName, "otherService");
client.startApplication(appName);

// deleting the application
client.deleteApplication(appName);

// deleting the service
client.deleteService("mydb");
You can get the services and plans available in Bluemix by using the cf marketplace command.

Step 3. Try it yourself

Based on the examples in this tutorial, I created a wrapper of the Cloud Foundry Java library for Bluemix. This wrapper makes it easier to deploy new apps in Bluemix. You can try it by downloading the source code and running the JUnit test class BluemixClientTest. There are two tests in this class that show how to deploy a NodeJS and a Java (WAR) application.
0

Add a comment

Contributors
Contributors
Popular Posts
Popular Posts
Archive
Labels
Labels
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.