IBM Bluemix is a beta-grade product that will change as we continue to make the function more complete and more accessible. We will do our best to keep this article up to date, but it is not always in full progress. Thank you for your understanding. As a software architect, we know that clustering and load balancing are important topics in enterprise applications. However, we often do not have the resources to design and implement them. Good performance and scalability can be achieved without a well-designed session persistence framework. Fortunately, you can use the Sessioncache services provided in IBM bluemix™ to help manage scalability and performance. This article describes how to develop and deploy an application that leverages the Sessioncache service.
HTTP is one of the oldest Internet technologies. HTTP sessions allow information to persist between multiple HTTP requests. Information, also known as session data, is stored in the machine's physical memory, which is fine when the application is small and serves only a small number of users. However, for enterprise applications involving multiple servers and computer systems, session data can be shared across multiple machines for load balancing, clustering, failover, and other scalability usage scenarios. In this case, it is not enough to save the session data in the memory of a single machine.
Session persistence refers to storing session data in a file system shared by multiple machines. It is common practice to use a database to store session data that is accessible to multiple servers, but using a single database server to service multiple WEB servers can cause system bottlenecks. To achieve better results, you can use multiple database servers, but the frequency of data replication between database servers can be a challenge. Too frequent replication can often deplete system resources, while less replication can lead to data integration issues. Fortunately, IBM Sessioncache Service can help.
The Sessioncache Service allows session data to be stored in an object grid so that enterprise applications can easily scale and achieve higher performance, eliminating previously mentioned multiple database server problems.
Long ago, application developers realized session persistence. Today, because session persistence is so common, most application servers allow the use of server configuration to select a persistence target. The application developer interacts only with the HTTP session API, and the server configuration determines whether persistence is persisted and what technology is used to implement it.
Part 1th: Deploying and running the sample application
Before you deploy an application in Bluemix, you must register. Go to Bluemix and click LOGIN. Follow the instructions to register or manage your account, and then:
Install Cloud Foundry client. Create a folder in your environment, such as MYWS. Download the V6 version of the CF command to the folder you created. Run the downloaded file and follow the instructions to install the Cloud Foundry client in the same folder. Create a folder in the Cloud Foundry folder, such as "samples." Download the Sessioncachesample.war file from IBM DevOps Services and save it in the folder you just created. Note: This sample application requires a Sessioncache service called scsSampleSessionServiceDWDemo001. If you feel that this name may be causing conflicts, see "Add Server Configuration" in part 2nd of building the sample application to bind the application to a service with a different name. Deploy the WAR file to Bluemix. At the command prompt, change the directory to the newly created Samples folder. Connect to Bluemix using the command CF API Https://api.ng.bluemix.net. Log in to Bluemix using CF login and follow the prompts. Now deploy the war file using CF push <app name>-P Sessioncachesample.war, where <app name> is a unique application name. Similar messages are seen when the push command executes successfully and the application runs.
Figure 1.
Create a Sessioncache service instance for your application. Create a service instance using the CF create-service Sessioncache free <service instance name>, where free is the service plan name, <service instance name> is the unique name of the service instance. We use scsSampleSessionServiceDWDemo001 as the service instance name to match the configuration in the server XML file. After the application is deployed and service instances are available, they are bound together using the CF bind-service <app name in step c> <service instance name in step d>. When the binding is complete, it asks you to push the application again. Perform the commands in step C again. For more information, see Installing the CF tool. Test the sample application. Log in to Bluemix. You should see the deployed sample application on the dashboard. Click the link in the middle of the application icon to access the application and check for errors in the log. Try using the Retrieve session data button to get the sessions. Check service status and space usage. After you get the session data, return to the Bluemix dashboard. In the navigation panel, click Apps and select the deployed sample application. In the Services section, click the link in the middle of the service to bring up the service Status page. View space Used, throughput and other information. Optional: Remove sample applications and services. In Bluemix, click Dashboard at the top of the screen. Click the settings icon in the upper-right corner of the application box to select the Stop App. Click the settings icon in the upper-right corner of the application box to select the Delete App. In the window, check the boxes for your applications and services in the Services and ROUTES tabs. Click OK to remove the sample applications and services.
Part 2nd: Setting up the environment and building your own sample application
Before you start
To build the applications in this article, you need to have:
Basic knowledge of
Java™ technology The basics of the servlet Eclipse IDE for Java EE developers
The source code for the sample application is available through IBM DevOps Services. To load the sample code and understand its purpose, follow these steps.
1th step. Setting up the environment
To build an application that uses the Sessioncache service, you must set up the correct environment. The Cloud Foundry client must be installed to deploy the Sessioncache application in the IBM cloud environment. For development, you need to install Websphere®application Server Liberty profile and WebSphere eXtreme Scale. The Eclipse IDE must also be installed.
Install Cloud Foundry client. Create a folder in your environment, such as MYWS. Download the V6 version of the CF command to the folder you created. Run the downloaded file and follow the instructions to install the Cloud Foundry client in the same folder. Install Liberty profile. Download the WebSphere creator Server Liberty profile to the folder you created (for example, MYWS). Follow the instructions to install the Liberty profile in the same folder. If you are asked to provide an installation location, press Enter to accept the default settings. Installs the WebSphere Extreme Scale. Download the WebSphere eXtreme Scale for Developers Liberty profile to the folder you created. Follow the instructions to install the WebSphere eXtreme Scale in the same folder. If you are asked to provide an installation location, press Enter to accept the default settings. After installation, the folder structure resembles the following figure:
Figure 2.
Note: Wxs is located under the WLP folder.
Step 2nd. Building a sample application
Once you have finished setting up your environment, you can start building your Sessioncache application. There are two ways to build an application that runs in Bluemix:
applications deployed as standalone WAR files are deployed through Liberty build packages
This article describes how to build a Sessioncache application that is deployed as a stand-alone WAR file. For more information about building applications that are deployed through the Liberty build package, see "To bind a Datacache service instance to a Liberty application."
The source code for the sample application can be obtained through IBM DevOps Services.
To build an application that is deployed as a stand-alone WAR file, perform the following steps:
Add Library. You must add a library to your project: Servlet 3.0 Library. To add a Servlet 3.0 library, you can open the Properties window by right-clicking the project and selecting Properties. Select Java build Path and switch to the Libraries tab. Click Add External JARs, navigate to the Myws/wlp/dev/api/spec directory, and then select Com.ibm.ws.javaee.servlet.3.0_1.0.1.jar file.
Figure 3.
The following code provides the option to request session data: private string Getsessiondata (HttpSession sessions) {string response = ' <h2>information on your session:</h2>\n "+" <table border=1>\n "+" <TR> <th>info type</th> <th>value</ th> </tr>\n "+" <TR> <TD>ID</TD> <TD> "+ session.getid () +" </TD> </tr>\n "+ "<TR> <TD>isNew</TD> <TD>" + session.isnew () + "</TD> </tr>\n" + "<TR> <td >creation time</td> <TD> "+ newdate (Session.getcreationtime ()) +" </TD> </tr>\n "+" <tr > <td>time of last access</td> <TD> "+ New Date (Session.getlastaccessedtime ()) +" </TD> </ tr>\n "+" <TR> <td>max Inactive interval</td> <TD> "+ session.getmaxinactiveinterval () +" </TD> </tr>\n "+" <TR> <td>number of Previous accesses</td><td><b> "+ Session.getattribute (attr_accesscount) + </b></td> </tr>\n "+" </table>\n "; return response; }
When the
HTTP session API GetId () and isnew () are invoked, the application server uses the specified technology to obtain persisted session data, which in this case is Sessioncache service, based on the configuration in server XML. The following code provides the option to invalidate a session: Private String invalidatesession (HttpSession sessions) {session.invalidate (); String response = "<h2> +" session invalidated. Please visit again! "+" </h2>\n "; return response; }
When the
HTTP session API invalidate () is invoked, the application server will use the specified technology to remove the persisted session data, in this case the Sessioncache service, which is based on the configuration in server XML. The following lines in the Doget () method can be used to create or get a session: HttpSession sessions = Request.getsession (true);
In the background, the application server will perform session persistence and use the IBM Sessioncache service to create or get session data. For more information about configuring an application server to use the Sessioncache service, see the instructions that are associated with the server XML file later in this article. The following code determines whether a session is accessed for the first time: String heading = ""; Intwrapper Accesscount = (intwrapper) session.getattribute (Attr_accesscount); if (Accesscount = = null) {Accesscount = new Intwrapper (0); heading = "Welcome, newcomer.";} else {heading = "Welcome back."; Accesscount.incrementvalue (); Session.setattribute (Attr_accesscount, Accesscount);
When the
HTTP session API getattribute () and setattribute () are invoked, the application server performs session persistence using the specified technology, in this case the Sessioncache service, which is based on the configuration in server XML. Add server configuration. After you complete the implementation of the Sessioncache application, you must configure your application to use the Sessioncache service in Bluemix. The configuration is located in a file named Server.xml (.../sessioncacheservicesample/webcontent) next to the WebContent folder.
Note: It uses a sessioncache service called scsSampleSessionServiceDWDemo001, which was created at deployment time. Use a unique service name for your application. Similarly, we recommend that the Idreuse property in the httpSession tag be set to true.
<server description= "Session Cache Service Sample" > <featureManager> <feature>webprofile-6.0</ Feature> <feature>eXtremeScale.webapp-1.1</feature> <feature>icap:appstate-1.0</feature > </featureManager> <xswebapp id= "mysession" Objectgridname= "${ Cloud.services.scsSampleSessionServiceDWDemo001.connection.gridName} "objectgridtype=" REMOTE "cataloghostport=" $ {Cloud.services.scsSampleSessionServiceDWDemo001.connection.catalogEndPoint} ' securityenabled= true ' Credentialgeneratorclass= " Com.ibm.websphere.objectgrid.security.plugins.builtins.UserPasswordCredentialGenerator " Credentialgeneratorprops= "${cloud.services.scssamplesessionservicedwdemo001.connection.username}${ Cloud.services.scsSampleSessionServiceDWDemo001.connection.password} "/> <httpsession idreuse=" true "/> <application name= "Sessioncachesample context-root="/"location=". /.. /.. /.. /.. /"type=" war "/> </server>
Concluding
In this article, we show how to develop and deploy an application that uses the Sessioncache service in Bluemix. We hope you will find that the Sessioncache service is easy to use, and it helps to improve scalability and performance, who doesn't want to?