2. Debug a Java application

When you develop an application for a cloud environment, sometimes there is no substitute for quickly updating a part of the application or interactively debugging it while running in the cloud. IBM provides Development Mode for Java Liberty and Node.js runtimes to help you debug cloud applications.

You can switch an application instance in Bluemix to Development Mode to be able to:

  • Initiate remote debug sessions with that application instance
  • Push incremental file updates to that application instance and see the changes without redeploying the application
  • Run and access additional tools inside the application container such as a shell, support for attaching a JMX client, or the node inspector as supported by each buildpack’s development mode

You can use these steps with any Java application, but to get started with a common reference point, this lab will step through adding a new application to Bluemix by using a boilerplate and then adding the application to your copy of Eclipse. If you already have an application in Eclipse and deployed to Bluemix, you can skip or just review steps 1 – 9.

To create a new application in Bluemix:

  1. From the Bluemix Catalog, click Boilerplates > Java Cloudant Web Starter boilerplate.
  2. Enter a unique name for the application. In these examples, you will see an application named cadc-javacloudant-1216. You should use a different name and also do not use an underscore in the name. Keep the remaining defaults in the boilerplate and click Create.
  3. From the Bluemix dashboard, navigate to the new application. To download the sample code to import into Eclipse, click Getting Started. Select Download Starter Code to download the application starter code to your workstation.
  4. Start Eclipse on your workstation and open the Java EE perspective for the rest of these steps.
  5. Go to the Servers tab by clicking Window > Show View and click to define a new server. Expand IBM and select IBM Bluemix. If this option is not available, revisit the setup for IBM Eclipse Tools for Bluemix. Then, click Next.
  6. Provide your Bluemix account information by either username and password or a one-time token and the region where your application is deployed and click Next.
  7. Select the organization and space where the application is deployed. The default is your email address with a space of dev. Then, click Finish. Eclipse will contact Bluemix by API, and you can see your running applications under the server name.
  8. Import the ZIP file you downloaded in step 3 into a new project in Eclipse.
  9. In the Servers tab, right-click your application that is running in Bluemix and select Link with Project. In the dialog, select the Eclipse project that contains the downloaded source code. Click OK if you are prompted with a warning about the project contents being synchronized with the published application.

    When you right-click your application in the Servers tab, you’ll see that the options Enable Application Debug and Enable Development Mode are available.

  10. Put the application into debugging mode by selecting Enable Application Debug. The application will be stopped and restarted in debug mode.

    In debug mode, you can experiment with setting breakpoints. For example, open the source code for the AttachServlet.java class in the example.nosql package and toggle a breakpoint on line 47 to stop execution after an upload completes.

  11. Open the application in a browser by clicking the link from the Bluemix application overview or entering (replacing host name with your application name): http://cadc-javacloudant-12ws16.mybluemix.net. Then, upload a file and observe the results either in the Java or Debug perspectives.
  12. Also while in development mode, note that you can update content without redeploying the application. You can test this by modifying the index.html file inside the WebContent folder. Add your name inside the <title> HTML tag in the header or change some of the static text. After you save the file, the status of the application in the Servers tab changes to Republish.

  13. Select the Bluemix server item and then click Republish on the right to send the changes to Bluemix. You can reload the application when the status is shown as Synchronized.

    The application will now show a different title in the browser tab:

  14. Return normal mode by right-clicking the application in the Servers view and select Disable Application Debug. You will be prompted to disable development mode at the same time. If you leave Development mode active, you can still republish changes to the application without redeploying. For production use, disable both Debug and Development mode.