Now we will deploy a variant of our Java EE 6 Web application which is composed of the following components:

Application Components

An EJB Stateful Bean which is used to store a cache of data, by exposing a Local interface
A JSF Managed Bean which acts as a layer between the EJB and the GUI
A JSF view which is used to collect data


Let's start with coding the JSF Managed Bean class:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import com.sample.ejb.StatefulData;

@SessionScoped
@Named(value="manager")
public class PropertyManager implements Serializable {
 
    @Inject
    StatefulData ejb;

    ArrayList  cacheList  = new ArrayList ();

    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void save() {
        ejb.put(key, value);
    }

    public void clear() {
        ejb.delete();

    }
    public List getCacheList() {
        return ejb.getCache();
    }


}


And this is the Local Stateful EJB class which is annotated as @Stateful @Clustered @SessionScoped

package com.sample.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.LocalBean;

import javax.ejb.Stateful;
import javax.enterprise.context.SessionScoped;

import org.jboss.ejb3.annotation.Clustered;

import com.sample.model.Property;

@Stateful
@Clustered
@SessionScoped
@LocalBean
public class  LocalStatefulData implements StatefulData  {

    private  List<Property> cache;

    @Override
    @PostConstruct
    public void initCache(){
        this.cache = queryCache();
        if (cache == null) cache = new ArrayList<Property>();
    }


    @Override
    public void delete(){
        this.cache.clear();
    }

    @Override
    public void put(String key,String value){
        Property p = new Property();
        p.setKey(key);
        p.setValue(value);
        this.cache.add(p);
    }

    @Override
    public List<Property> getCache() {
        return cache;
    }

    @Override
    public List<Property>  queryCache(){

        return cache;          
    }

}

 

Important Notice The current release of JBoss Tools (3.3) does not includes by default the libraries where the @Clustered annotation is contained. Until this issue is solved, you have to include the jboss-ejb3-ext-api-2.0.0.jar manually as shown by the following picture:

jboss cluster tutorial software

For the sake of completeness we include also the Stateful SB interface:

package com.sample.ejb;

import java.util.List;
import com.sample.model.Property;

public interface StatefulData {

    public abstract void delete();
    public abstract void put(String key, String value);
   public abstract List<Property> getCache();
    public abstract List<Property> queryCache();

}

Then, this is the model class which consists of a simple POJO:

                                                                                                         

package com.sample.model;

import java.io.Serializable;

public class Property implements Serializable{

    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

Last piece of the application is the JSF home.xhtml page which is unchanghed from no clustered Java EE application:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    
</h:head>
<h:body>
    <h2>Cluster demo on JBoss 7</h2>
    <h:form id="jsfexample">
        <h:panelGrid columns="2" styleClass="default">

            <h:outputText value="Enter key:" />
            <h:inputText value="#{manager.key}" />

            <h:outputText value="Enter value:" />
            <h:inputText value="#{manager.value}" />

            <h:commandButton actionListener="#{manager.save}"
                styleClass="buttons" value="Save key/value" />
            <h:commandButton actionListener="#{manager.clear}"
                styleClass="buttons" value="Clear cache" />

            <h:messages />

        </h:panelGrid>


        <h:dataTable value="#{manager.cacheList}" var="item"
            styleClass="table" headerClass="table-header"
            rowClasses="table-odd-row,table-even-row">
            <h:column>
                <f:facet name="header">Key</f:facet>
                <h:outputText value="#{item.key}" />
            </h:column>
            <h:column>
                <f:facet name="header">Value</f:facet>
                <h:outputText value="#{item.value}" />
            </h:column>
        </h:dataTable>
    </h:form>
</h:body>
</html>

That’s all. Remeber to add an empty beans.xml file under the WEB-INF folder so that the Weld dependencies are triggered automatically by AS 7.
Here’s how your project should look like:
jboss as 7 cluster tutorial
Now launch your browser and test your Web application by pointing to the mod_cluster port that we have formerly configured:
http://192.168.1.0:6666/as7clustersample/home.xhtml

jboss as 7 cluster tutorial

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss Clustering a Web Application

Please Note: This article cover JBoss AS 4/5/6 releases. If you w

Clustering EJB 3 with JBoss AS

To cluster a stateless session bean in EJB 3 all you need to do i

JBoss monitoring HTTP Session replication

In this article we'll show how to monitor HTTPSession replication

How do I change multicast address of JBoss cluster ?

Since JBoss AS 4.0.3, the jboss.partition.udpGroup property can b

JBoss farming service

What is the farming service ? this article explains about it, als

JBoss HTTP Session replication

HTTP session replication is used to replicate the state associate