Talip has posted 10 posts at DZone. View Full User Profile

Hazelcast 1.1 Released: Http Session Clustering and More

08.14.2008
| 4356 views |
  • submit to reddit
After a successful 1.0 release, Hazelcast, free data distribution platfom for Java, is now helping developers with new tools and features. So what is new in Hazelcast 1.1:


1. Http session clustering with Hazelcast Webapp Manager (HazelcastWM)
2. Support for java.util.concurrent.ConcurrentMap api in distributed map.
3. Support for full TCP/IP based clustering. (multicast discovery is default but not required)
4. Better and nicer documentation.
 
Hazelcast Webapp Manager (HazelcastWM)
 
Hazelcast Webapp Manager is a http session clustering and monitoring tool for Java web applications. In short, Hazelcast takes <yourApp>.war  (or ear) file and creates a clustered and monitorable version of it (clustered-<yourApp>.war).  No more losing sessions after crashes. It is that simple. To learn what Hazelcast does behind the scenes,  please check out the documentation.


Besides that, Hazelcast Webapp Manager (HazelcastWM) lets you get statistics on your running application so that you can monitor the health of your application. HazelcastWM collects statistics like number of created/destroyed sessions, average session data size, number of requests, average response time, min. and max. response times for a given time interval (x seconds). So it is like taking a snapshot of the running application every x seconds and passing it to the listeners as a SnapshotEvent . Here is a sample servlet that listens for snapshot events.
 

public class MonitoringServlet extends HttpServlet implements SnapshotListener {
     
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ServletContext servletContext = config.getServletConfig();
        Context hazelcastContext = (Context) servletContext.getAttribute(Context.ATTRIBUTE_NAME);
        hazelcastContext.addSnapshotListener(this);
    }


    public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {
        //do something here
    }

    public void handleSnapshot (SnapshotEvent snapshot) {
        System.out.println("===== Here are the metrics of the last 30 seconds =====");
        System.out.println(snapshot);
        System.out.println("ave.       response time : " + snapshot.getAverageResponseTime());
        System.out.println("min.       response time : " + snapshot.getMinResponseTime());
        System.out.println("max.      response time : " + snapshot.getMaxResponseTime());
        System.out.println("# of               requests : " + snapshot.getNumberOfRequests());
        System.out.println("# of         new sessions : " + snapshot.getCreatedSessionCount());
        System.out.println("# of destroyed sessions : " + snapshot.getDestroyedSessionCount());
        System.out.println("======================================");
    }
}

So you might want to send email if average response time gets bigger than ,say, 5 seconds or send an SMS to the system admin if there is no requests for that last 30 seconds to check if there is anything wrong. Or you may want to develop a front end to show these metrics with nice graphics, charts etc.

Distributed ConcurrentMap

 
ConcurrentMap methods such as ConcurrentMap.putIfAbsent(key,value) and ConcurrentMap.replace(key,value) are now supported in Hazelcast distributed map. Here is a sample code:
 
import com.hazelcast.core.Hazelcast;
import java.util.concurrent.ConcurrentMap;


Customer getCustomer (String id) {
    ConcurrentMap map = Hazelcast.getMap("customers");
    Customer customer = (Customer) map.get(id);
    if (customer == null) {
        customer = new Customer (id);
        customer = map.putIfAbsent(id, customer);
    }
    return customer;
}
public boolean updateCustomer (Customer customer) {
    ConcurrentMap map = Hazelcast.getMap("customers");
    return (map.replace(customer.getId(), customer) != null);           
}
public boolean removeCustomer (Customer customer) {
    ConcurrentMap map = Hazelcast.getMap("customers");
    return map.remove(customer.getId(), customer);           
}

Coming up next: Distributed java.util.concurrent.ExecutorService. "Execute your code on a cluster"


To find out more about Hazelcast: http://www.hazelcast.com/ 

 

0
Published at DZone with permission of its author, Talip Ozturk.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dmitry Namiot replied on Fri, 2008/08/15 - 1:51am

There are several light components for the sessions monitoring:

http://www.servletsuite.com/servlets/sesslist.htm

http://www.servletsuite.com/servlets/sessadm.htm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.