Debugging and profiling portal and portlet applications
by user
Comments
Transcript
Debugging and profiling portal and portlet applications
Debugging and profiling portal and portlet applications in IBM Rational Application Developer V7 Part of a series on portal and portlet development By Gaurav Bhattacharjee Software Engineer IBM Corporation Level: Introductory August 2007 Debugging and profiling portals and portlets, Page 2 of 21 Contents Abstract ........................................................................................................................... 3 Overview ......................................................................................................................... 3 Setup............................................................................................................................... 3 Import the sample ............................................................................................................. 3 Set up a portal server configuration ...................................................................................... 4 Debugging portal and portlet applications ....................................................................... 6 Create portal and portlet projects ......................................................................................... 6 Add functionality to the DateHelper class ............................................................................... 8 Profiling portal or portlet applications ............................................................................ 11 The Rational Profiling tool ................................................................................................ 11 Profiling the portal or portlet projects ................................................................................... 12 Start monitoring your application on the server ...................................................................... 16 Other articles in this series ............................................................................................ 19 Resources ..................................................................................................................... 20 About the author............................................................................................................ 21 Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 3 of 21 Abstract This article aims to explore and showcase capabilities that IBM® Rational® Application Developer V7 and later provide for debugging and profiling the portal or portlet applications. It covers the basics of using the Rational Application Developer/server debugging and profiling features concentrating specifically on portal or portlet related applications in IBM Rational Application Developer V7. The aim of the article is to help in use of the tools that enable to pinpoint performance and memory use problems in the portal or portlet applications. Overview IBM® Rational® Application Developer is a premium product that is part of the IBM® Rational® Software Delivery Platform. Its many benefits cut across various dimensions that affect developers. It is a true integrated development environment (IDE), because it provides a single environment for designing, building, testing, and deploying, all from the same workbench. Rational Application Developer V7 provides the capability of profiling and debugging Java™ applications and J2EE-based applications (Java™ 2 Platform, Enterprise Edition, or J2EE). Debugging is the process with which you can detect and diagnose errors in applications that are running locally or remotely. Profiling gives you insight into the performance characteristics of your application so that you can pinpoint the performance and memory use problems. This gives you the knowledge about your application's run-time behavior for better understanding of its resource requirements and potential bottlenecks that can help to guide you designing, implementing, and updating decisions throughout the project lifecycle. This article shows how to use the debugging and profiling tools provided by Rational Application Developer for portal and portlet applications on IBM® WebSphere® Portal 6.0. Setup To get started, you need to get the sample files for this article and configure your server. Import the sample From the Resources section of this article, download and import the sample.zip file into your Rational Application Developer workspace. This file contains the MyNewPortal and MyPortlet projects, along with their corresponding EAR files. These projects are the sample projects that you will use to follow the explanations of debugging and profiling in this article. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 4 of 21 Set up a portal server configuration For both debugging and profiling, you will need to create a portal server configuration in Rational Application Developer. Follow these steps to create a configuration: 1. On your Rational Application Developer workbench, click Windows > Open Perspective > Other > J2EE. This will open the Java™ 2 Platform, Enterprise Edition (J2EE) perspective. 2. Next, click Windows > Show View > Servers. This will open the Servers view inside the J2EE perspective. 3. Right-click the Servers view and select New > Server. This will open a New Server wizard that Figure 1 shows. (continued on next page) Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 5 of 21 Figure 1. The New Server wizard 4. Choose WebSphere Portal V6.0 server, and then define the server by using the wizard. 5. The configuration will now appear on your Servers view. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 6 of 21 Debugging portal and portlet applications Debugging a portal or portlet project on a server is quite similar to debugging code in the workspace. The only difference is that, instead of debugging a stand-alone application, you debug the code that is running on the portal server. You set break points in your Java™ source code and the Java™Server Pages (JSP) files, and then launch the server in debug mode. As soon as any of the break points are hit, you will be given the option to switch to the Debug perspective. Note: For debugging on a remote server, the server should be running in debug mode. Also, if your server is not configured to run in debug mode, then you need to configure it for that (see Resources for a link to instructions). Create portal and portlet projects You need to do two things to get ready: • Create a portal project named MyNewPortal. • Now create a new portlet project named MyPortlet. You will be adding the portlet created in this project to the MyNewPortal’s Portal Designer and setting debug points in the portlet JSP and Java code, and then you will debug the portal project on the server. 1. First, create a new package called com.test.sample under the MyPortlet > JavaResources folder. 2. Inside of that folder, create a class called DateHelper.java. This Java file is very simple Java class with only one constructor. 3. Now open the MyPortletView.jsp source file and add this JSP scriptlet: <% new DateHelper(); %> Now add a break point in the line new DateHelper(); This will add a breakpoint in the specified line of the JSP as shown in Figure 2. Figure 2. Break point in the JSP Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 7 of 21 Now you have added a breakpoint to your portlet’s JSP, which you will be using to debug on the server. 4. Now open the Portal Designer for MyNewPortal by double-clicking the MyNewPortal > Portal Configuration node (see Figure 3). Figure 3. Portal Configuration node 5. Add MyPortlet to the Portal Designer by dragging it onto the designer or by right-clicking the portlet container and then choosing Insert Portlet as shown in the Figure 4. Figure 4. Portal Designer for MyPortlet After you have finished all of this, you are ready to debug your application. 6. Just right-click MyNewPortal in the Project Explorer, and select Debug > Debug on server. This will launch the server in debug mode and open a browser running the portal application. 7. As soon as the break point set in the portlet JSP is hit, you will be prompted to change to the Debug perspective. After switching to it, you can debug the code in the usual manner, by using options such as Step Into, Step Over, and so forth. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 8 of 21 Similarly, you can add break points to your Java code and use the debug options to debug your Java code. This is just a small example of how you can use the Debug on Server option to debug and review your code. Add functionality to the DateHelper class Now, let’s add some functionality to your DateHelper class, and then use the debug points to debug the Java code. 1. Go to the DateHelper class. This class includes the following code: package com.test.sample; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateHelper { private Date date; public DateHelper() { } public String getDate() throws ParseException { SimpleDateFormat sd = new SimpleDateFormat(EEE MMM dd HH:mm:ss zzz yyyy); Date now = new Date(); String dateString=now.toString(); Date parsedDate = sd.parse(dateString); return parsedDate.toString(); } public void setDate(Date date) { this.date = date; } } In this class, a getDate() method implements the logic of retrieving the current date in better user-readable format. This method will be called when the portlet JSP runs and the jsp:getProperty tag is encountered. After removing all other break points (see Step 2), we will set a debug point on the line Date parsedDate = sd.parse(dateString); (also shown in Figure 5): Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 9 of 21 Figure 5. Adding a breakpoint in the DateHelper class Date parsedDate = sd.parse(dateString); 2. Before doing this, however, remove all of the other break points in the portlet JSP file. 3. Now add a JSP bean into your portlet JSP. Click on your JSP in Design view and from the main menu choose JSP > Insert Bean. This will launch the Insert Bean dialog(see fig. 6). Type a name for the ID field. say myDateHelper. Choose the DateHelper class. 4. Set the scope if necessary, and click OK Figure 6. Insert JSP Bean dialog This will insert a tag like this in your JSP source. <jsp:useBean id=”myDateHelper”class="com.test.sample.DateHelper"> </jsp:useBean> Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 10 of 21 5. Now. from the main menu, click JSP > Insert Get Property. This will launch the Insert JSP Get Property dialog. 6. Select myDateHelper and expand it, and then select the property date (see Figure 7) and click OK. Figure 7. Insert JSP Get Property dialog This will insert a getProperty tag in your JSP code as shown here: <jsp:getProperty name="myDateHelper" property="date" /> 7. Now right-click the MyNewPortal project and again choose Debug > Debug on Server. The moment the break point is hit, you will be prompted to move to the Debug perspective (Figure 8). 8. After this, you can go through your code debug JSP break point. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 11 of 21 Figure 8. Debug perspective Now you know how to use the debugging options provided by Rational Application Developer to debug your Java code and JSP files on the portal server. This will help in reviewing your code and in controlling and tracing the execution of the portal or portlets. Profiling portal or portlet applications This section describes tools included in Rational Application Developer or that you can use within it, as well as the profiling process. The Rational Profiling tool The Rational Profiling tool (also called profiler) consists of the Profiling and Logging perspectives and several views. You can profile various types of applications, including Java and Web applications, regardless of what application server you are using. The Profiling tool collects data related to Java or J2EE applications and can present the collected data in tabular and graphical views. This enables you to examine your applications for performance and excessive memory use problems. The profiler interacts with the Agent Controller provided by the Eclipse Test & Performance Tools Platform (TPTP) data collection framework. This framework provides a structure under which a client (for example, the Rational Application Developer workbench) interacts with data provider applications known as agents, with the help of the Agent Controller. An agent and the Agent Controller always coexist on the same system. The client and the Agent Controller are not required to reside on the same system. The Agent Controller manages all of the details of startup and communication with each agent. The Agent Controller can interact with one or more Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 12 of 21 agents simultaneously. A client is not associated with a particular agent until it gets a handle to the agent from the Agent Controller. You can use the Rational Application Developer profiler to gather information on a stand-alone Java application or an application running within a J2EE-based application server, such as IBM® WebSphere® Portal. In both cases, profiling can be done either locally or remotely relative to the application's target host. Furthermore, profiling can occur across multiple Java™ Virtual Machines (JVMs), which means you can profile different applications residing at different places from a single workbench. Profiling the portal or portlet projects Portal or portlet projects can be profiled on the local servers, as well as on the remote servers. For the local servers, the client, agents, and Agent Controller will be on the same machine, but for remote servers, the agent and Agent Controller will reside on the remote machine. Steps for profiling on the server 1. Install the Agent Controller. To use profiling tools to analyze the performance of a portal or portlet project running on the portal server, you must first install the Agent Controller on the same system as the portal server. See Resources for more information on installing the Rational Agent Controller. Generally, you have the option of installing the Rational Agent Controller while installing the portal server. 2. Identify the portal or portal projects that you want to profile. Let’s continue with the MyNewPortal and MyPortlet projects that we used while debugging on the server and use these same projects for profiling. 3. Right-click the MyNewPortal project and select Profile As > Profile on Server. o 4. Select your portal server. Note: For profiling on a remote server, the server should be running in the profiling mode. This starts the portal server in Profile mode. After a while, a Profile on server window will appear 5. Select the agents that you want to monitor and add to the list of Selected agents (Figure 9). The process ID (PID) values indicated here correspond to the portal server V60 PID allocated by the operating system at server startup. The agents shown in Figure 9 basically interact with the JVM and collect the profiling data. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 13 of 21 • • The Java Profiling Agent is a library that provides services to the host process to capture and record the behavior of a Java application and make this data available to attached clients. The J2EE Request Profiler is an agent that operates in an application server process and that collects data relating to the execution of the Web application, such as execution of EJBs (Enterprise Java™Beans) and servlets. These agents use the Java™ Virtual Machine Profiler Interface (JVMPI) to connect with the JVM. Figure 9. Profile on server window with selected agents 6. Next, click the Monitor tab (see Figure 10). Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 14 of 21 Figure 10 Profile on server 7. Choose the monitoring options: Basic Memory Analysis and Execution Time Analysis. 8. Select Basic Memory Analysis and click Edit Options, and then select the Collect instance level Information check box (Figure 11) to collect the instance level information, too. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 15 of 21 Figure 11. Edit Profiling Options view 9. You can edit the options for Execution Time Analysis similarly. 10. Click Finish to close the Profile on server window. 11. This will prompt you to switch to the Profiling and Logging perspective. Press Yes to continue. The configured profiling agents are displayed in the Profiling Monitor view, as shown in Figure 12. Figure 12. Profiling view Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 16 of 21 Start monitoring your application on the server Now, to start monitoring your portal or portlet application on the server, you will need to enable monitoring. 1. In the Profiling Monitor view, right-click the Profiling agent and select Start Monitoring (Figure 13). This will enable the Profiler to collect data while you perform certain operations on your portal or portlet applications. Figure 13. Start Monitoring option in the Profiling Monitor view 2. Now, open your portal applications in the browser and perform an operation. For example, just refresh the page. What you are concerned here is about collecting the profile data for the DateHelper class that we created earlier. Note: In the Profiling Monitor view, as you perform some operations, the Profiling agent shows the status as <monitoring……collecting>. This means that the profiler is collecting the data when you perform the operation (see Figure 14). Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 17 of 21 Figure 14. Monitoring view 3. After your page refreshes, right-click the profiling agent and select Pause Monitoring. If you want to restart the monitoring later, just select Start Monitoring again. Now that you have profiled the portal or portlet application, it’s time to see the profiled data. The Rational profiler offers various views to see the Profiled data. These views give you insight into the various packages, classes, and methods used in your applications. The Profiling tool also provides information about the number of calls to the packages, classes, and methods inside your application, in addition to information about the memory use of objects in your applications. 4. Right-click the Profiling agent and select Open With > Execution Statistics. This will open the view that Figure 15 shows. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 18 of 21 Figure 15. Execution Statistics view In this view, you can find out how many times your classes or methods have been called and other relevant information. As you can see in the screen capture that Figure 15 shows, this view contains the profiled information for the DateHelper class. As another useful option, you can open the Memory Statistics view by right-clicking the profiling agent and selecting Open With > Memory Statistics. Again, you can see the information about the objects used in your applications. Figure 16 shows the information about the DateHelper class object. Figure 16 Memory Statistics view Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 19 of 21 There are also other views, such as a Coverage Statistics view that displays use statistics for a selected type of object, and the Object References view, which indicates the number of references that a class makes to other classes. These views will give you more insight with the profiled data. Inside all of these views you can also see the package level information.(which is the default), as well as the class level, method level, and instance level information by clicking appropriate icons, as shown in Figure17. Figure 17. Class level, method level, and instance level icons Other articles in this series This completes our description of debugging and profiling the portal and portlet projects on the server. However, check the One-stop guide to portal and portlet application development using Rational Application Developer V7 and WebSphere Portal V6 for summaries of the nine other articles in this series and links to download each of them in PDF format. Topics covered include the predevelopment resources, portlet tooling features, portal design tools, and the testing, deploying, and debugging capabilities of Rational Application Developer. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 20 of 21 Resources Learn • • • • • • • Discuss • • Get information on IBM Rational Application Developer and how to use it: o What's new in IBM Rational Application Developer V7.0 o IBM Rational Application Developer Version 7.0 Information Center o Rational Application Developer page on IBM® developerWorks® Find out What’s new in WebSphere Portal Version 6. This developerWorks article by Stefan Hepper, Stefan Liesche, Gregory Melahn, and Thomas Stober (July 2006) describes the highlights in IBM® WebSphere® Portal Version 6.0. You see how WebSphere Portal helps you create a serviceoriented architecture (SOA) environment, and you learn about the technical enhancements that speed up your development projects, providing quick business value and ease-of-use. This article is as a good starting point to understand the version content and the improvements since the previous releases. Installing Rational Agent Controller Eclipse Test & Performance Tools Platform (TPTP). TPTP provides powerful frameworks and services for an open platform upon which developers build unique test and performance tools—both open source and commercial—that easily integrate with Eclipse and other tools and address the entire test and performance lifecycle, from developer testing through production monitoring. Configuring remote WebSphere Portal servers for testing and debugging Visit the Rational page on developerWorks to find technical resources and learn about best practices for the Rational Software Delivery Platform. Subscribe to The Rational Edge weekly newsletter. Rational Software Architect, Data Architect, Software Modeler, Systems Developer, Application Developer and Web Developer forum. Choose most relevant forum and annotate by using the abstract on that page, if you’d like. WebSphere Portal forum. Link to other software relevant to this article, and annotate by using the abstract on that page, if you’d like. Download • Sample.zip file for the examples used in this article (available in the Downloads section of the One-stop guide). • Get the evaluation version of Rational Application Developer. • Get evaluation versions of WebSphere software from the WebSphere downloads page. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®. Debugging and profiling portals and portlets, Page 21 of 21 About the author Gaurav Bhattacharjee is a Staff software engineer at IBM India Software Labs in Delhi, India. He works with the Rational Application Developer Portal Tooling team in the WebSphere Portal Lotus Collaboration Software group. Copyright © 2007, IBM® Corporation. All rights reserved. Published by IBM® developerWorks®.