Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Are Your Handhelds Safe in J2EE Hands?

Are Your Handhelds Safe in J2EE Hands?

As J2EE usage proliferates into new domains, software engineers are realizing that out-of-the-box components cannot be used to solve problems for which they were not designed. One such example is state and session management (SSM) for applications serving wireless devices. We'll look at why traditional techniques don't work and how you can create your own scalable, fault-tolerant SSM solution.

Despite what your stock portfolio says, there is life out there in the high-tech world. One such example is all the software being created for wireless operator consumption. It's becoming more of a wireless world out there and new specifications are emerging. One example is the Wireless Village initiative created by Nokia, Ericsson, and Motorola. Essentially, this joint venture has created a specification for wireless devices. With its emphasis on interoperability, it addresses instant messaging and presence services (IMPS). This feeds into the expanding universe of mobile information appliances and their associated protocols, paradigms, and policies. Now more than ever, software engineers must think beyond the browser-based client.

J2EE is at a crossroads and facing increasing competition from .NET. Which one of these platforms will dominate the wireless frontier? Is J2EE up to the challenge?

Browser-Based SSM
What's so different about architecting an application serving wireless devices vs. one serving browsers? The big difference is state and session management - or what I like to call "SSM." To fully appreciate this, let's look at how SSM works for good old browsers.

To communicate with servers, browsers use HTTP, which is by nature a connectionless and stateless protocol. With HTTP alone, the server cannot maintain a relationship among the series of requests originating from a browser. There are times, of course, when stateful connections are needed, as in the case of a shopping cart in an online store. Each browser must be identified on subsequent requests in order to access its own shopping cart stored as an object in the server. Browser identity is commonly achieved through the use of HTTP cookies. When a browser visits a Web site, the server's response can include a cookie that contains a unique session ID. The browser will automatically pass this back on subsequent requests. If a browser does not accept cookies for security reasons, the server can use URL rewriting. Essentially, this means that the session ID is encoded right into the URL.

Whichever technique is used, ultimately a stateful connection is established via a session ID, which should have the following properties:

  • Be a long alphanumeric string (80-100 characters)
  • Be randomly generated (nonsequential)
These properties lead to session IDs that are difficult to guess and make it challenging for a hacker to masquerade as legitimate user. This requires the attacker to hijack the session, by guessing and using someone else's session ID, to gain access to the system.

Once this is in place, we have a mechanism that allows the server to establish browser identification, create a session, and hold state (i.e., the shopping cart) on behalf of its client. These are the fundamentals of SSM. On a small-scale application, SSM can be fairly straightforward. However, on a big-time, highly available, fault-tolerant, and scalable application, this is quite another matter. Let's take a look at some techniques that WebLogic (version 6.0 or higher) uses to fulfill these big-time requirements.

Performance and Scalability
No matter how much CPU power, memory, or disk space is available, a host has a finite load capacity. If the load increases beyond that point, a cluster of hosts is needed to achieve further scalability. This, of course, adds complexity to SSM. Let's look at how this works in a clustered environment. (Note that this is just one of the various configurations supported by WebLogic.)

In Figure 1, the application is hosted by a cluster composed of four identical and interchangeable hosts - meaning a request can be served by any host. Each is a WebLogic server hosting a servlet and EJB container. The cluster is accessed by a dedicated Web server (such as an Apache HTTP server or Microsoft Internet information server) that serves static content. Dynamically generated content, on the other hand, is dispatched to the WebLogic cluster. The Web server contains a WebLogic-specific plug-in (a.k.a. HttpClusterServlet) that load balances each request to a cluster member. Note that a client's session state is stored in only one particular server. Therefore, load-balancing algorithms, such as round-robin, can cause the request to be sent to a host that does not contain the session state. How will session state be accessed in this situation?

Session state could be accessed using network calls (via RMI) but this would incur extra network traffic and reduce performance. Alternatively, replicating the session state on every host so that it is available everywhere would also be nonviable because as the cluster grows, replication becomes more and more costly. N-host replication hinders scalability.

WebLogic has a clever solution. The session ID, stored in the cookie, has an embedded host name representing the session's home host. The plug-in uses this information to route the request to the home host. This guarantees that accessing the state will be a local, non-RMI call. Typically, processing requests requires many read/writes to session state. When you compare remote calls to local calls, which are 100 times more expensive, this is a key design advantage.

Fault-tolerance and state replication
Another benefit of clustered hosts is their inherent fault-tolerant quality. When one host fails, another one steps in to serve the request. All this happens transparently from the client's perspective. Let's look at how SSM fits into this.

In actuality, a particular session's state is stored on exactly one host (on whichever host the session was created), but is constantly being replicated on exactly one backup host (see Figure 2). Each time an element in the session state is created, updated, or deleted, WebLogic automatically replicates the state on a backup host. Furthermore, to minimize network traffic, only the state delta is replicated.

If the primary (home) host fails, the plug-in will transparently route the request to a secondary (backup) host. Conveniently, all of the session's state will be available and up-to-date right on that host. The secondary will now become the session's primary host and a new secondary will be assigned.

In an effort to avoid n-host state replication, WebLogic employs a one-host replication scheme, or what is referred to as "in-memory replication." As the cluster grows, scalability is not hindered by state replication since there is still only one secondary. The only drawback is that if both primary and secondary fail simultaneously, session data will be lost. WebLogic does offer alternatives, such as database replication, but to the detriment of performance. According to tests, this can represent an 83% drop in performance. In reality, the odds of having two hosts simultaneously fail and losing both session replicas are indeed slim. So in-memory replication is sufficient for most applications.

In retrospect, this is quite powerful functionality. It's all available in J2EE through Sun's servlet specification classes and WebLogic's load-balancing and fail-over mechanisms. Now that I've exposed the inner working of a browser-based client serving, let's see how this relates to wireless clients.

Why Out-of-the-Box Components Won't Do
Unlike the Web browser in the desktop world, there is no ubiquitous counterpart in the wireless world. Wireless devices come in many shapes and sizes and communicate using various protocols. Some are HTTP-based, some are not. All use proprietary networks and all client-to-server communication is mediated by a device-specific gateway. Furthermore, wireless devices must be served differently due to their inherent limitations in screen size, bandwidth, memory, and processing power.

Suffice it to say that despite efforts to make wireless devices behave like desktop browsers (WAP and HDML come to mind), they are a different kind of animal. As far as SSM is concerned, out-of-the-box Web components were specifically designed for HTTP clients. Until the specification addresses this, engineers can choose between two platform-leveraging solutions:

  • Solution 1: Create an adapter component that converts from the native protocol to HTTP - and get all the goodies J2EE has to offer
  • Solution 2: Build upon the platform and roll a custom solution

    These can be viewed as "poor man's solution" vs. "rich man's solution" respectively. Don't get me wrong, I'm not passing any judgements in terms of the affluence of these solutions. The first may be adequate for applications that don't require stateful connections. It can also be good enough if the adapter's performance is not an issue. There are caveats, though, and we'll examine them in detail. We'll also look at a best-of-breed solution.

    Creating an Adapter Component (Solution 1)
    To put things in perspective, let's say you're designing a server-side application that supplies content to SMS-based cell phones upon request. Assume that a stateful connection between client and server is required. The application would most likely use short message peer-to-peer (SMPP) protocol - the most widely used SMS gateway protocol. As Figure 3 demonstrates, the adapter component would convert every request from SMPP to HTTP.

    So where do the problems lurk? First, processing requests incurs the extra costs associated with piping requests across two network connections; one between gateway and adapter and another between adapter and application. Second, and more important, there would be two levels of SSM to maintain: the servlet-based SSM in the servlet container and the custom, adapter-based SSM. Essentially, since SMS gateways have no concept of HTTP cookies, this solution requires the adapter to emulate a browser. That is, save cookies and pass them back to the application. However, unlike the browser, which stores cookies for only one user, the adapter would need to store cookies for every connected client.

    So, how can we do this? A simple solution is to leverage your database's powerful storage and lookup features to store cookies. Naturally, extra costs would be incurred with database lookups on every request. Furthermore, the adapter would also be responsible for garbage collection. A custom eviction policy would need to be designed and implemented.

    There is another potential side effect. Transactions do not propagate across HTTP boundaries. Therefore, components on either side of the divide could not participate in the same transaction. Consequently, if the adapter would read and lock database tables needed by other components, database deadlocks would ensue. Transactions need to be carefully demarcated.

    Alternatively, memory could be used to store cookies. However, with memory, fault-tolerance and replication are now the main concern. If the host fails, the cookie cache must be replicated and available elsewhere.

    In summary, this solution can be sufficient for some applications but clearly falls short of servlet-based SSM.

    Rolling Your Own (Solution 2)
    The custom solution will have the following design ideals:

  • Maximize the amount of functionality that can be leveraged from the platform (write as little code as possible)
  • Have all the features and performance of servlet-based SSM

    Since servlet-based SSM will serve as a model, let's review the major players involved:

  • HttpSession: Holder of session state. Any serializable object can be stored. WebLogic automatically replicates state to a secondary host.
  • Servlet container: Manages the HttpSession cache including the accessibility of HttpSession and garbage collection.
  • Plug-in: Load-balances requests and manages fail over.
  • Cookie: Used by the plug-in to route requests. Contains the primary and secondary hosts for routing.

    The solution will consist primarily of moving these responsibilities to other J2EE components and writing custom code where this is not possible. In place of the HttpSession will be a stateful session bean (SFSB) in the EJB container. All that's needed is a custom bean class that allows clients to get and set any serializable object representing session state. Note that SFSBs and HttpSessions share the same replication mechanisms. When their hosts fail, and in-memory replication is enabled, their state is available on a secondary host. Unlike HttpSessions, however, SFSBs state replication is controlled by the transactional context. This gives SFSBs a net advantage in keeping session state cohernt across the cluster.

    In managing the cache of SFSBs, the EJB container is far more flexible than its servlet counterpart. The EJB specification defines the bean life cycle but allows vendors to implement their own cache management schemes. In WebLogic, you can control how aggressively the trash is taken out. First, define a timeout value for the bean. This tells WebLogic when an unused SFSB can be removed either temporarily (via passivation) or permanently (via eviction). Second, choose a cache management policy. Under the not-recently-used (NRU) policy, SFSBs are only passivated when the cache is running out of space. Conversely, the least-recently-used (LRU) policy will passivate upon timeout even if the cache is not full. The timeout value also affects the time of eviction.

    As for fail over and load balancing, this will be managed by SFSB's remote stub object. Remote stubs are objects that act as proxies to the actual session bean. Clients invoke methods on these stubs, which in turn invoke on the actual session bean. In WebLogic, stubs are "replica-aware" objects capable of routing the request to the host containing the SFSB. Similar to cookies, they also contain primary and secondary host data used for routing. When a primary host fails, they automatically route to the secondary host and update themselves to reflect the change. If a host fails during a request, however, the remote stub will throw an exception. The client need only retry and the stub will automatically adjust itself and subsequently function correctly.

    Like HttpSessions, EJB stubs must be cached and made accessible to clients. Caching can be done by retrieving the "handle" from the EJB stub and storing it in its serialized form. The stub can be re-created at anytime by deserializing the handle. However, since SFSB instances and their corresponding stub objects have different life cycles, the stub cache may not be in sync with the SFSB to which they refer. Making use of the SFSB's call-back methods (ejbActivate, ejbPassivate, ejbCreate, ejbRemove) is the key to keeping the cache in sync with the SFSBs. Figure 4 shows the complete solution using the same application shown earlier.

    Implementation Caveats
    The EJB specification states that a SFSB instance must not be accessed concurrently or an exception is thrown by the container. WebLogic has extended the spec and allows concurrent client invocations via the <allow-concurrent-calls> element in the WebLogic deployment descriptor. When activated, access is serialized at the bean level. This feature only works for SFSBs whose transactions are container-managed.

    Notice that as in solution 1, you still need to manage your own cache sessions (containing EJB headless rather than HTTP cookies).

    This can be implemented in a variety of ways. The simplest is to make it a database cache. If performance is an issue, you can purchase a third-party hardware cache box which can give you better performance, since these tend to be memory-based caches. The Java Community Process is addressing caching through its jcache component; this will certainly fill a void in J2EE if and when it becomes standard.

    Accessing State Locally
    One of the keys to this design is that, as with servlets, request processing is sent to the data rather than vice-versa. The goal is to access session state locally. To achieve this, the SFSB mediates all communications between the client (protocol adapter) and the application (a collection of EJBs replicated in the cluster). Each coarse-grain service offered by the application can only be accessed by a Java object whose sole purpose is to call the service on behalf of the client. This object is called the service executor and employs a "Strategy design pattern." The client creates an instance of the service-specific executor and passes it to the SFSB. In turn, the SFSB invokes a generic method on the executor which ultimately calls the back-end service. Using this framework guarantees that the request made from the protocol adapter will be processed on the host that contains the session's state.

    Conclusion
    Whichever solution you choose, much can be leveraged from WebLogic and J2EE. This can make a big difference in delivering your product faster using dependable, out-of-the-box components.

    Resources

  • Gridley, Michael; Woolen, Rob; Emerson, Sandra L. (2002). J2EE Applications and BEA Weblogic Server. Prentice-Hall.
  • Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
  • Failover and replication in a cluster: http://e-docs.bea.com/wls/docs70/cluster/failover.html
  • Wireless Village and the IMPS initiative: www.wireless-village.org
  • The SMS forum: http://smsforum.net
  • More Stories By Nick Maiorano

    Nick Maiorano is a Sun-certified J2EE architect with over 10 years of experience in software development. He is currently a senior developer and co-architect of a wireless, instant messaging application built atop J2EE WebKogic technology.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    @ThingsExpo Stories
    "Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    "Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...
    "There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
    "IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
    Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
    SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
    "MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
    In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
    "Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
    A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
    SYS-CON Events announced today that Evatronix will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
    Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
    To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
    An increasing number of companies are creating products that combine data with analytical capabilities. Running interactive queries on Big Data requires complex architectures to store and query data effectively, typically involving data streams, an choosing efficient file format/database and multiple independent systems that are tied together through custom-engineered pipelines. In his session at @BigDataExpo at @ThingsExpo, Tomer Levi, a senior software engineer at Intel’s Advanced Analytics gr...
    When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things’). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing? IoT is not about the devices, it’s about the data consumed and generated. The devices are tools, mechanisms, conduits. In his session at Internet of Things at Cloud Expo | DXWor...
    Everything run by electricity will eventually be connected to the Internet. Get ahead of the Internet of Things revolution. In his session at @ThingsExpo, Akvelon expert and IoT industry leader Sergey Grebnov provided an educational dive into the world of managing your home, workplace and all the devices they contain with the power of machine-based AI and intelligent Bot services for a completely streamlined experience.