Using Red Hat JBoss Developer Studio to Debug Java Applications in the Red Hat Container Development Kit
In an earlier article, Debugging Java Applications using the Red Hat Container Development Kit, it was discussed how developer productivity could be improved through the use of remotely debugging containerized Java applications running in OpenShift and the Red Hat Container Development Kit. Not only does remote debugging provide real time insight into the operation and performance of an application, but reduces the cycle time a developer may face as they are working through a solution. Included in the discussion were the steps necessary to configure both OpenShift and an integrated development environment (IDE), such as the Eclipse based Red Hat JBoss Developer Studio (DevStudio). While the majority of these actions were automated, there were several manual modifications, like configuring environment variables and exposing ports, that needed to be completed to enable debug functionality. Through advances in the Eclipse tooling for OpenShift, most if not all of these manual steps have been eliminated to enable a streamlined process that offers even more functionality out of the box.
Red Hat JBoss Developer Studio Integration
Enhancements made in Red Hat JBoss Developer Studio now provide full lifecycle support of the Red Hat Container Development Kit, including starting and stopping the underlying Vagrant machine. This eliminates the need for the user to execute commands inside a terminal. To start the CDK from within DevStudio, either use an existing workspace or open a new workspace and open the Servers view by navigating to Window -> Show View and select Servers on the menu bar. With the view now open, right click inside the view and select New -> Server and under the Red Hat JBoss Middleware folder, select Red Hat Container Development Kit. Keep the default location for the server’s host name as localhost and select a name of your choosing if desired to represent the CDK connection and select Next. On the next dialog, two items are required to be configured prior to configuring the CDK:
- The credentials to the Red Hat customer portal to register the machine
- The location of the Vagrantfile used to start up the CDK
First, add your credentials by selecting the Add button next to the Username field and enter your username and password and click Ok. Next, click the Browse button to navigate to the location containing the Vagrantfile. Click Finish to add the CDK as a new server.
To start up the CDK, right click on the newly created Container Development Kit server and select Start. The CDK will start using the details provided in the configuration. Once the CDK is active, DevStudio will attempt to connect to the OpenShift instance in order to make it available in the OpenShift Explorer view. When prompted, click Yes to accept the self-signed certificate to complete the configuration. Once the CDK has started up successfully, thanks to the integrated tooling, a connection to OpenShift running within the CDK is automatically configured in the OpenShift Explorer view using the openshift-dev user by default. The process for starting up the CDK and configuration of the connection to OpenShift in DevStudio needed to be previously configured manually.
Everything you need to grow your career.
With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.SIGN UP
Deploy an Application
With the CDK now active and DevStudio configured to communicate with the OpenShift environment, an application can be deployed to the environment to demonstrate the enhanced tooling available for debugging applications. Once again, the Ticket Monster application that was shown in the previous post will be used. In the OpenShift Explorer view, create a new project called eap-debug-jbds by right clicking on the OpenShift connection and selecting New -> Project. Create a new application by right clicking on the eap-debug-jbds project and instantiating the eap64-basic-s2i template and specifying the following parameters:
- APPLICATION_NAME: ticket-monster
- CONTEXT_DIR: demo
- SOURCE_REPOSITORY_URL: https://github.com/jboss-developer/ticket-monster.git
- SOURCE_REPOSITORY_REF: 2.7.0.Final
Once the template has been instantiated, a results dialog will confirm the actions taken and the resulting OpenShift API objects created. The final dialog prompts the user to specify a location the source code should be stored on the local machine and then imported into the workspace. This is an essential step as the source code must be available on the local machine and imported into the workspace to facilitate debugging the running application in OpenShift. Click Finish to clone the repository into the workspace and complete the instantiation process.
In the OpenShift explorer view, a ticket-monster service was created under the eap-debug-jbds project as part of the instantiation process. If the service is expanded, the pod currently executing the source to image build can be observed. To view the build progress, right click on the pod that has the “Build running” status next to it, and select Build logs.
Once the build completes, the resulting image will be deployed as a new application. DevStudio makes it easy to determine the URL of the application and provide easy access directly from the IDE as the ticket-monster service is exposed as a route. The URL is provided next to the name of the ticket-monster service, and the application can be accessed by right clicking on the service, selecting Show In, and then selecting Web Browser.
A new view will open up directly within DevStudio. Selecting Web Console from the Show In context menu would launch the OpenShift web console.
With confirmation the application is running, let’s describe the process of using DevStudio to debug the application using its’ integrated tooling. A server adapter is now available for integrating the IDE with the lifecycle of applications running in OpenShift. This includes the management of the OpenShift and Kubernetes API objects, as well as the running application. To utilize the server adapter in the ticket-monster application, right click on the ticket-monster service in the OpenShift Explorer view and select Server Adapter. Confirm the name of the project in the workspace matches the project next to the Eclipse Project in the dialog box and click Finish. One of the functions of the server adapter is to synchronize content from the running application and the workspace. This allows the developer to make incremental changes to their application without requiring a full image rebuild as part of the development process. While this functionality will not be covered during this discussion, it is a very useful feature for reducing development cycle time.
Once the server adapter setup process is successful, a new Server called ticket-monster will be available on the Servers page.
Debugging The Running Application
Now that DevStudio is configured to communicate with the application, the process for configuring the application to accept remote debug sessions and to initiate a session is simplified. To begin debugging the remote application, navigate to the Servers view and right click on the ticket-monster server adapter and select Restart in Debug. By activating this option, the ticket-monster Deployment Configuration API object will be automatically configured with the necessary settings and the application will restart to accept these connections.
Note: It may take some time to complete the operation
These changes can be seen by navigating the to OpenShift Explorer view, right clicking the ticket-monster service and selecting Manage Environment Variables.
Notice how DEBUG and DEBUG_PORT have been automatically configured with the proper values. These configurations were modified manually previously.
A full explanation on the role these changes play is described in the previous post.
To confirm DevStudio is actively debugging the application, open the Debug perspective by selecting Perspective from the Window menu bar option and select Open Perspective, and then Debug. If the remote debugger is running as shown below, then DevStudio is connected to the application.
Add a breakpoint at the beginning of the createBooking method in the BookingService class within the ticket-monster application in DevStudio. This will halt the operation as soon as a user attempts to purchase a new ticket.
Navigate to the application in a web browser and complete the process of purchasing a ticket for an event. When finalizing the transaction, the application will break at the breakpoint configured previously. Use DevStudio and perform typical debugging functions, such as viewing variable values and stepping through code.
To stop the remote debugging session, right click on the ticket-monster server adapter in the Servers view and select Stop.
The enhancements and improvements in the integrated tooling provided by Red Hat JBoss Developer Studio and the localized Red Hat Container Development Kit ecosystem make it even easier to develop robust containerized applications.
Andrew Block is a Senior Consultant with Red Hat Consulting. Andrew has worked with a wide range of Red Hat Customers to implement solutions around Continuous Integration/Continuous Delivery, cloud based applications, and system integration. He is a frequent contributor to several open source projects, and is an avid blogger who shares his knowledge and experience with the community.
To learn more, visit our Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets (e.g. containers), books (e.g. microservices), and product downloads that can help you with your microservices and/or container application development.