Example Code


This chapter is example driven. In it, you will work with a crime portal application that stores information about imaginary criminal organizations. The data model you will work with is shown in Figure 11.1.

Figure 11.1. The crime portal example classes.


The source code for the crime portal is available in the src/main/org/jboss/cmp2 directory of the example code. To build the example code, you run Ant, as shown here:

 [examples]$ ant -Dchap=cmp2 config 

This command builds and deploys the application to the JBoss server. When you start your JBoss server, or if it is already running, you should see the following deployment messages:

 15:46:36,704 INFO  [OrganizationBean$Proxy] Creating organization Yakuza, Japanese Gangsters 15:46:36,790 INFO  [OrganizationBean$Proxy] Creating organization Mafia, Italian Bad Guys 15:46:36,797 INFO  [OrganizationBean$Proxy] Creating organization Triads, Kung Fu Movie Extras 15:46:36,877 INFO  [GangsterBean$Proxy] Creating Gangster 0 'Bodyguard' Yojimbo 15:46:37,003 INFO  [GangsterBean$Proxy] Creating Gangster 1 'Master' Takeshi 15:46:37,021 INFO  [GangsterBean$Proxy] Creating Gangster 2 'Four finger' Yuriko 15:46:37,040 INFO  [GangsterBean$Proxy] Creating Gangster 3 'Killer' Chow 15:46:37,106 INFO  [GangsterBean$Proxy] Creating Gangster 4 'Lightning' Shogi 15:46:37,118 INFO  [GangsterBean$Proxy] Creating Gangster 5 'Pizza-Face' Valentino 15:46:37,133 INFO  [GangsterBean$Proxy] Creating Gangster 6 'Toothless' Toni 15:46:37,208 INFO  [GangsterBean$Proxy] Creating Gangster 7 'Godfather' Corleone 15:46:37,238 INFO  [JobBean$Proxy] Creating Job 10th Street Jeweler Heist 15:46:37,247 INFO  [JobBean$Proxy] Creating Job The Great Train Robbery 15:46:37,257 INFO [JobBean$Proxy] Creating Job Cheap Liquor Snatch and Grab 

Because the beans in the examples are configured to have their tables removed on undeployment, any time you restart the JBoss server, you need to rerun the config target to reload the example data and redeploy the application.

Enabling CMP Debug Logging

To get meaningful feedback from the example code, you will want to increase the log level of the CMP subsystem before running the test. To enable debug logging, you need to add the following category to your log4j.xml file:

 <category name="org.jboss.ejb.plugins.cmp">     <priority value="DEBUG"/> </category> 

In addition, you need to decrease the threshold on the CONSOLE appender to allow debug-level messages to be logged to the console. You also need to apply the following changes to the log4j.xml file:

 <appender name="CONSOLE" >     <errorHandler />     <param name="Target"    value="System.out"/>     <param name="Threshold" value="DEBUG" />     <layout >         <!-- The default pattern: Date Priority [Category] Message\n -->         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>     </layout> </appender> 

To see the full workings of the CMP engine, you need to enable the custom TRACE level priority on the org.jboss.ejb.plugins.cmp category, as shown here:

 <category name="org.jboss.ejb.plugins.cmp">     <priority value="TRACE" /> </category> 

Running the Examples

The first test target illustrates a number of the customization features that are discussed in this chapter. To run these tests, you execute the following Ant target:

 [examples]$ ant -Dchap=cmp2 -Dex=test run-example 

The output log will show the following output:

[View full width]

22:30:09,862 DEBUG [OrganizationEJB#findByPrimaryKey] Executing SQL: SELECT t0_OrganizationEJB.name FROM ORGANIZATION t0_OrganizationEJB WHERE t0_OrganizationEJB.name=? 22:30:09,927 DEBUG [OrganizationEJB] Executing SQL: SELECT desc, the_boss FROM ORGANIZATION WHERE (name=?) 22:30:09,931 DEBUG [OrganizationEJB] load relation SQL: SELECT id FROM GANGSTER WHERE (organization=?) 22:30:09,947 DEBUG [StatelessSessionContainer] Useless invocation of remove() for stateless session bean 22:30:10,086 DEBUG [GangsterEJB#findBadDudes_ejbql] Executing SQL: SELECT t0_g.id FROM GANGSTER t0_g WHERE (t0_g.badness > ?) 22:30:10,097 DEBUG [GangsterEJB#findByPrimaryKey] Executing SQL: SELECT t0_GangsterEJB.id FROM GANGSTER t0_GangsterEJB WHERE t0_GangsterEJB.id=? 22:30:10,102 DEBUG [GangsterEJB#findByPrimaryKey] Executing SQL: SELECT t0_GangsterEJB.id FROM GANGSTER t0_GangsterEJB WHERE t0_GangsterEJB.id=?

These tests, which are mentioned throughout this chapter, exercise various finders, selectors, and object-to-table mapping issues.

The other main target runs a set of tests to demonstrate the optimized loading configurations presented in the section "Optimized Loading," later in this chapter. When the logging is set up correctly, the read-ahead tests will display useful information about the queries performed. Note that you do not have to restart the JBoss server for it to recognize the changes to the log4j.xml file, but it may take a minute or so. Running the following shows the actual execution of the read-ahead client:

 [examples]$ ant -Dchap=cmp2 -Dex=readahead run-example 

When the read-ahead client is executed, all the SQL queries executed during the test are displayed in the JBoss server console. The important items of note when analyzing the output are the number of queries executed, the columns selected, and the number of rows loaded. The following shows the read-ahead none portion of the JBoss server console output from read-ahead:

[View full width]

22:44:31,570 INFO [ReadAheadTest] ######################################################## ### read-ahead none ### 22:44:31,582 DEBUG [GangsterEJB#findAll_none] Executing SQL: SELECT t0_g.id FROM GANGSTER t0_g ORDER BY t0_g.id ASC 22:44:31,604 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,615 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,622 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,628 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,635 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,644 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,649 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,658 DEBUG [GangsterEJB] Executing SQL: SELECT name, nick_name, badness, organization, hangout FROM GANGSTER WHERE (id=?) 22:44:31,670 INFO [ReadAheadTest] ### ######################################################## ...

We will revisit this example and explore the output when we discuss the settings for optimized loading later in this chapter.



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net