Distributable applications

Web applications in Java EE don't work in a distributable system by default. The specification provides a special tag to put in the web.xml descriptor file of the web application--the distributable. Here's a sample of a distributable application:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<distributable/>
</web-app>

Now we will put a test servlet in the application:

@WebServlet("/SessionServlet")
public class SessionServlet extends HttpServlet {
...
private final static String SHARED_ATTRIBUTE = "shared_attribute";

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
PrintWriter write = response.getWriter();
Boolean attribute = (Boolean) session.getAttribute(SHARED_ATTRIBUTE);
write.println("before: " + attribute);
if (attribute == null)
session.setAttribute(SHARED_ATTRIBUTE, true);
write.print("after: " + session.getAttribute(SHARED_ATTRIBUTE));
}
}

This simple servlet monitors the insertion of an attribute in the web session so that we can monitor whether the attribute is available in both the started application servers.

As the final operation before testing the servlet, we have to deploy the web application. The configuration of WildFly that we started is very basic, so we need to deploy the application in all the available nodes of the cluster. More sophisticated configurations will be seen in the next section Cluster domains.

Once we deploy the application in our WildFly instances a log as it will start on the log:

19:41:10,054 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-5) ISPN000078: Starting JGroups channel web
19:41:10,054 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-4) ISPN000078: Starting JGroups channel hibernate
19:41:10,055 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-8) ISPN000078: Starting JGroups channel server
19:41:10,055 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-6) ISPN000078: Starting JGroups channel ejb

Four channels are configured in the standalone-ha.xml. The first channel we see in the log is for the web sessions. Now, connect to the servlet of the first node. We will get this:

Try the same thing on the other node. The web session is replicated!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.0.192