Many developers use memcached and MySQL to cache content as part of their web application. This article presents a simple example application that uses MySQL and accesses a memcached server. The application is deployed on the GlassFish application server. The example application was developed and deployed on a Sun Ultra 40 Workstation running the Solaris 10 Operating System. The format for commands and path names shown in the article are specific to the Solaris 10 OS, but you can easily adapt them to other platforms. Contents
Installing the Tools
Installing the ToolsYou must install the following software to successfully run the example application: MySQL Database. MySQL is the world's most popular open-source database software. GlassFish v2 Update 1. GlassFish is a free, open-source application server that implements the latest features of the Java EE 5 platform. Memcached Caching System. As defined on the memcached web site, memcached is a distributed-memory object-caching system, generic in nature but intended for use in speeding up dynamic web applications by alleviating database load. Portions of your web application in which the content changes infrequently are great candidates to be cached into a memcached server. By caching these portions, you increase the responsiveness of your application. A classic use-case scenario for memcached is a tag cloud. A tag cloud is a set of weighted tags, typically shown as words. The tags can appear in alphabetical order, in a random order, sorted by weight, and so on, and they are updated infrequently. The example application described in this article reuses components from the well-known example Java Pet Store application to create a cloud of tags associated with pets. Installing the GlassFish-MySQL Bundle The GlassFish application server and MySQL have been released as a convenient single bundle. You can download and install the bundle at no cost. Just follow these instructions:
After you have installed the bundle, be sure to follow the installation guide instructions in the section To Start MySQL. These instructions describe how to create the necessary MySQL database and users. You will find scripts to install the database and tables required for the application later in this article. Starting the GlassFish Application Server Start the GlassFish application server with the following command:
where the environment variable GlassFish Log Files You can access a log file containing information about the server at the following location on your system:
Installing Memcached If you are using the Solaris OS, you can install memcached with Cool Stack, an open-source software stack optimized for the Sun Solaris platform. Binaries for other platforms are available from the memcached web site. After a successful Cool Stack installation in the Solaris 10 environment, the memcached executable is available at the following location:
You can now launch the memcached server from the command line. For example, to launch the memcached server on a Solaris 10 machine with IP address 123.456.789.123 and accepting connections on port 11211, use the following command:
Downloading the Example Application Download the example application, which is provided in the form of a jar file, and expand it into a directory. For example, the following commands expand the jar file into the
For the remainder of this article, assume that the application directory is The application uses the Whalin memcached Java client, maintained by Greg Whalin, to access the memcached servers. Many other memcached Java clients are available. The authors of this article chose Whalin's client because of the strength of its documentation and active support. If you want to adapt the example application and scale it up to solve your own business problem, research the suitability of the Whalin client and, if necessary, choose another. Configuring the Application for the Memcached Server To make the example application work, you must edit one source file that specifies server information. In that file, named
Editing the Build Properties File Now, edit the
After you have made the changes, you can build the application. Building the Application Build the application with the following commands:
Creating a MySQL User and Database You must now create a MySQL user and database for use with the example application. You must also create database tables and load them with data.
Creating the JDBC Connection Pool and JNDI Resources You now must create the JDBC connection pool and JNDI resources that the example uses. The Use the following commands to create the required JDBC connection pool and JNDI resources:
Deploying the Application You are now ready to deploy the application using the application war file
If the deployment is successful, your browser should be able to access the application home page at the following URL:
If you cannot access the application home page, refer to the server log to learn about any errors that may have occurred. The log file is available at the following location:
Running the Example ApplicationTo launch the application, enter the following URL at the address line of a web browser:
Your browser should open a page like the one shown in Figure 1.
When you click Run the Sample, the
You can view statistics about the memcached servers that are known to this application by clicking on Memcached Stats in the upper right section of the display. In Figure 3, note that the application recognizes two memcached instances running on the same machine on different ports: 11211 and 11212.
At the bottom of the statistics page, note the link Remove TagCloud. When you click the link, the entry stored in the memcached servers for this tag cloud is removed, and a subsequent call to Note also the Tag link in the upper right corner of the page. Clicking this link enables you to create a new tag for the tag cloud. The application contains code that removes the cached tag cloud from the memcached servers when you create a new tag. Figure 4 shows the page where you can add a new tag.
The list of current tags displayed at the bottom of the page is obtained by querying the database using a Java Persistence API (JPA) call. This information is also a good candidate to be stored on the memcached server. Application OverviewAs mentioned earlier, this example reuses components from the Java Pet Store application to create a tag cloud that displays tags associated with pets. To keep the example as simple as possible, calls to the memcached servers have been incorporated into the JavaServer Pages (JSP) technology pages themselves. These are the three JSP files of interest:
You can examine each file to see how it interacts with the memcached server. The Open the
The first statement checks to see whether the memcached server has the tag cloud information, using the key
After the HTML string has been created, it is stored into the memcached server by the call to The Open the
This code inserts a new tag into the database. After doing so, the The Open the
The Refer to the Javadocs of the Whalin memcached client for more information. SummaryIn this article, you deployed a simple application that uses MySQL as its database and accesses a memcached server. You deployed the application on the GlassFish application server. With the techniques illustrated in the example, you can use the memcached distributed memory caching system to increase the performance of your own database-backed web applications. For More Information
Acknowledgments The authors wish to thank the following individuals for their help:
|
| |||||||||||||||||||||||||||||||||||||
|
| ||||||||||||