Tag: JBoss AS 6

Configuring mod_jk to connect to JBoss


Here is a simple step by step procedure to connect to JBoss instance for load balancing purpose through apache webserver. In most of the production environments this is the most common need.

The prerequisite for this exercise is to have the the Apache WebServer installed already in your Machine and the JBoss should be installed in your environment. The “mod_jk” module can be downloaded later to work on this setup. If you want to download the apache software and the mod_jk then you can refer to the link mentioned in the Step6):

JBoss Cluster Creation/Configuration Part:

Step1). As “all” and “production” profiles which are present inside the “E:jboss-AS6-5.1jboss-asserver” location are by default clustering enabled so you can chose any one of them to create your own clustered node. In this exercise we will use “all” profile to create JBoss Clusters.

So first copy the “all” profile and paste it in the same location “E:jboss-AS6-5.1jboss-asserver” with some different name like “all_node1” and “all_node2”. like this your 2 JBoss Clustered Nodes are ready.

Step2). Now open the “server.xml” file present inside the “E:jboss-AS6-5.1jboss-asserverall_node1deployjbossweb.sar” location and then add the “jvmRoute” attribute of the Engine Tag like following:By default the line which you are going to change will look like following:
=============By default it will look like below==================

<Engine name="jboss.web" defaultHost="localhost">

=============After Change it will look like below==================

<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">

Step3). Same changes we need to do for “all_node2” profile as well. So open the “server.xml” file present inside the “E:jboss-AS6-5.1jboss-asserverall_node2deployjbossweb.sar” location and then add the “jvmRoute” attribute of the Engine Tag like following:
=============By default it will look like below==================

<Engine name="jboss.web" defaultHost="localhost">

=============After Change it will look like below==================

<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node2">

Why We need “jvmRoute”?

jvmRoute Identifier must be used in load balancing scenarios to enable session affinity. The identifier, which must be unique across all Tomcat 6 servers which participate in the cluster, will be appended to the generated session identifier, therefore allowing the front end proxy to always forward a particular session to the same Tomcat 6 OR JBoss instance.

More information regarding this attribute can be found in the following link:

Step4). Also please make sure that the AJP Connector Tag is uncommented in “E:jboss-AS6-5.1jboss-asserverall_node2deployjbossweb.sarserver.xml” file for both “all_node1” & “all_node2” profiles, Because the communication between Apache and JBoss will happen through this AJP connector port. Like following:

      <!-- A AJP 1.3 Connector on port 8009 -->
      <Connector protocol="AJP/1.3" port="8009" address="${jboss.bind.address}"
         redirectPort="8443" />

Step5). Now start Both the JBoss Instances by running the following command from the following dir “E:jboss-AS6-5.1jboss-asbin”:

run.bat -c all_node1 -b -g ClusterA -Djboss.service.binding.set=ports-01 -Djboss.messaging.ServerPeerID=1
run.bat -c all_node2 -b -g ClusterA -Djboss.service.binding.set=ports-02 -Djboss.messaging.ServerPeerID=2

Here following are the meaning of the flags which we have used :
-c = The jboss profile which we want to start

-g = The name of the cluster. The same name of the cluster should be used by the profiles which want to join the same cluster formation.

-Djboss.service.binding.set = The advantage of this way is you don’t have to take (and maintain) a separate copy of the all folder if you want to, say, start up multiple instances of JBoss running as a cluster. For more information on this system property please refer to the following : http://community.jboss.org/wiki/ConfigurePorts

-Djboss.messaging.ServerPeerID = Each node must have a unique ServerPeerID for clustering to work!. It can be any unique integer value among all the cluster members. Means every cluster node should have a unique peer ID.

-b = The bind address in which you want to run your profile. o.o.o.o represents it will listen to all the IP Addresses associated with that box. better if we can specify a particular “IP Address” or “localhost” there rather than using “”

So suppose if you are running your JBoss on two separate physical boxes which has different IP Addresses like and then please run the servers like following

run.bat -c all_node1 -b -g ClusterA -Djboss.service.binding.set=ports-01 -Djboss.messaging.ServerPeerID=1
run.bat -c all_node2 -b -g ClusterA -Djboss.service.binding.set=ports-02 -Djboss.messaging.ServerPeerID=2

Apache WebServer Configuration Part:

Step6). Download & Install the apache webserver from the following link along with the “mod_jk” file:

Step7). Now paste the “mod_jk.so” file inside the modules directory of your apache which may be “C:Program FilesApache GroupApache2modules”

Step8). Now add the following line of configuration somewhere at the bottom inside your “C:Program FilesApache GroupApache2confhttpd.conf” file to include the mod_jk configuration file:

##### JBOSS #############
LoadModule jk_module modules/mod_jk.so

# Include mod_jk's specific configuration file
Include conf/mod_jk.conf

Step9). Now create a file with name “mod_jk.conf” inside the directory “C:Program FilesApache GroupApache2conf” as following:

##### JBOSS #############
# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk logs
JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat  "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

# Mount your applications
JkMount /* failover

# You can use external file for mount points.
# It will be checked for updates each 60 seconds.
# The format of the file is: /url=worker
# /examples/*=loadbalancer
#JkMountFile conf/uriworkermap.properties

# Add shared memory.
# This directive is present with 1.2.10 and
# later versions of mod_jk, and is needed for
# for load balancing to work properly
JkShmFile logs/jk.shm

# Add jkstatus for managing runtime data
<Location /jkstatus/>
    JkMount status
    Order deny,allow
    Deny from all
    Allow from

Step10). Now create another most important file “worker.properties” inside “C:Program FilesApache GroupApache2conf” to tell apache to load balance between what all nodes of the JBoss clusters:

##### JBOSS #############
# Define list of workers that will be used
# for mapping requests

# Define Node2
# modify the host as your host IP or DNS name.

# Define Node1
# modify the host as your host IP or DNS name.

# Load-balancing behaviour

# Status worker for managing load balancer

NOTE: Make sure that you are setting the correct “Hostname” and “AJP Port” for your JBoss in the above file.

Step 11). Now restart your apache server and then hit the following URL to access the JBoss:


Middleware Magic

Developing and Running a Stateless Session Bean 3.0 in JBoss AS6


In this article we can see how to develop and test a simple Stateless Session Bean in JBoss and what all JARs are needed in the CLASSPATH of a standalone client to invoke an EJB. In this demonstration we will not use any IDE to create the EJBs we will just notepad or gedit of editors to develop the testcase on our own, this is just to see how to manually set the classpath and path and many other things.

Step1). Create a directory somewhere in your filesystem like : “/home/testuser/Demos/MyEJB3.jar” Here “MyEJB3.jar” is a Directory & Not a file.

Step2). Now create a remote Interface for your Stateless Bean inside “/home/testuser/Demos/MyEJB3.jar” with name “TestSLSB_RemoteIntf.java” like following:

package ejb3;
import javax.ejb.Remote;
public interface TestSLSB_RemoteIntf {
  public String sayHello(String name);

Step3). Now we need to write the Stateless Bean class, which implements the above interface as following “TestSLSBean.java” :

package ejb3;
import javax.ejb.*;
public class TestSLSBean implements  TestSLSB_RemoteIntf
  public TestSLSBean() {
      System.out.println("nt TestSLSBean-->  TestSLSBean() invoked");

   public String sayHello(String name){
      System.out.println("nt TestSLSBean --->  public String sayHello(String name)  invoked");
      return "Hello Mr. "+name;

Step4). Now we need to set the CLASSPATH to compile the above two classes. In order to set the classpath please open a shell prompt and then run the following commands:

 export PATH=/home/testuser/MyJdks/jdk1.6.0_21/bin:$PATH:
 export CLASSPATH=`/home/testuser/jboss-AS6/jboss-as/bin/classpath.sh -s`

Step5). Now compile the EJBs like following:

javac    -d   .   TestSLSBean.java
javac    -d    .   TestSLSB_RemoteIntf.java

Step6). Now Copy the “MyEJB3.jar” and then paste it inside your “$PROFILE/deploy” directory so that it will be deployed on the JBoss instance. As soon as you will deploy it on JBoss you will find following kind of STDOUT entry in the server’s console:

18:06:47,564 INFO  [JBossASKernel] Created KernelDeployment for: MyEJB3.jar
18:06:47,565 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3
18:06:47,565 INFO  [JBossASKernel]   with dependencies:
18:06:47,565 INFO  [JBossASKernel]   and demands:
18:06:47,565 INFO  [JBossASKernel] 	jboss.ejb:service=EJBTimerService
18:06:47,565 INFO  [JBossASKernel]   and supplies:
18:06:47,565 INFO  [JBossASKernel] 	Class:ejb3.TestSLSB_RemoteIntf
18:06:47,565 INFO  [JBossASKernel] 	jndi:TestSLSBean/remote
18:06:47,565 INFO  [JBossASKernel] 	jndi:TestSLSBean/remote-ejb3.TestSLSB_RemoteIntf
18:06:47,565 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3) to KernelDeployment of: MyEJB3.jar
18:06:47,626 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3
18:06:47,632 INFO  [EJBContainer] STARTED EJB: ejb3.TestSLSBean ejbName: TestSLSBean
18:06:47,692 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

	TestSLSBean/remote - EJB3.x Default Remote Business Interface
	TestSLSBean/remote-ejb3.TestSLSB_RemoteIntf - EJB3.x Remote Business Interface

Step7). Now we need to write the EJB Client to invoke the Stateless Session Bean which is deployed on the Server. So we need to write “Client.java” program as following inside “/home/testuser/Demos” directory.

import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import javax.rmi.PortableRemoteObject;
public class Client
   public static void main(String[] args) throws Exception
             //JBoss' default remote jndi: <ejb-name>/remote
             final String jndiName = "TestSLSBean/remote";
             Hashtable ht=new Hashtable();

             Context ic = new InitialContext(ht);
             System.out.println("ntabout to look up jndi name " + jndiName);
             Object obj = ic.lookup(jndiName);
             System.out.println("ntlookup returned " + obj);

             ejb3.TestSLSB_RemoteIntf remote=(ejb3.TestSLSB_RemoteIntf)PortableRemoteObject.narrow(obj,ejb3.TestSLSB_RemoteIntf.class);
             System.out.println("ntCalling Remote Method:sayHello(String): "+remote.sayHello("JBossUser"));

Step8). Now again we can open a separate Shell Prompt to set the Client CLASSPATH like following:

 export PATH=/home/testuser/MyJdks/jdk1.6.0_21/bin:$PATH:
 export CLASSPATH=`/home/testuser/jboss-AS6/jboss-as/bin/classpath.sh -c`

NOTE: “$JBOSS_HOME/bin/classpath.sh -c” here “-c” represents Clients Classpath same we can use “-s” flag with this script to display the required Server Classpath Jars/Classpath setting.

Step9). Now compile and then run the client in the same shell prompt as following:

 java Client

	about to look up jndi name TestSLSBean/remote
log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
log4j:WARN Please initialize the log4j system properly.

	lookup returned Proxy to jboss.j2ee:jar=MyEJB3.jar,name=TestSLSBean,service=EJB3 implementing [interface ejb3.TestSLSB_RemoteIntf]

	Calling Remote Method:sayHello(String): Hello Mr. JBossUser

Middleware Magic

What are the minimum set of Jars required for a JBoss EJB client JBoss AS6?


Many times we fall into one issue when we try to invoke the EJBs which are deployed on JBoss Server but the EJB Client modules are deployed on any Non-JBoss server like Tomcat/WebLogic or WebSphere. So in this case we need a minimum set of Jars which we can place at the client side to invoke the EJBs which are running on JBoss.

JBoss also provides “$JBOSS_HOME/client/jbossall-client.jar” file which comtains a “META-INF/MANIFEST.MF” file entry to refer to all the other Jars which are present in the “$JBOSS_HOME/client” directory. But the problem with this Jar is this Jar actually doesnot contain any Classes rather it contains only the classpath reference of other JARs so this single JAR file is of no use. We will need to either place all the “$JBOSS_HOME/client/*.jar” inside the Jboss Client application (Which will unnecessarily increase the size of Client application) or We have to selectively find out the required Jars.

If the required Jars are not set properly at the Client side then usually we get the following kind of StackTrace of the exception while performing a lookup to the JBoss JNDI:

Caused by: java.lang.ClassCastException: javax.naming.Reference
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java: ...)

To avoid this kind of exceptions while lookup we must make sure that the Classpath of the client is set properly. It is better to have the following set og JARs at the client side to avoid these kind of exceptions to be thrown at the EJB invocation time.

If you don’t have JBoss installed on the Client Box then please Use the Jars mentioned in the following scripts for your EJB Client Application’s.

For Windows Operating System if you want to run EJB Client then please make sure that the following JARs are present in the Clients classpath Or you can also run the following BAT file to set the JBoss EJB Client classpath properly:

set JAVA_HOME=C:MyJDKsjdk160_05

set CLIENT_JARS_DIR=E:/jboss-AS6/jboss-as/client

set CLASSPATH=%CLIENT_JARS_DIR%/jboss-logging-spi.jar;%CLIENT_JARS_DIR%/jnp-client.jar;%CLIENT_JARS_DIR%/jboss-ejb3-proxy-impl-client.jar;%CLIENT_JARS_DIR%/jboss-ejb3-common-client.jar;;%CLIENT_JARS_DIR%/jboss-aspect-jdk50-client.jar;%CLIENT_JARS_DIR%/jboss-remoting.jar;%CLIENT_JARS_DIR%/jboss-aop-client.jar;%CLIENT_JARS_DIR%/jboss-common-core.jar;%CLIENT_JARS_DIR%/jboss-serialization.jar;%CLIENT_JARS_DIR%/jboss-ejb3-security-client.jar;%CLIENT_JARS_DIR%/jbosssx-client.jar;%CLIENT_JARS_DIR%/jboss-security-spi.jar;%CLIENT_JARS_DIR%/jboss-javaee.jar;%CLIENT_JARS_DIR%/ejb3-persistence.jar;%CLIENT_JARS_DIR%/jboss-ejb3-security-client.jar;%CLIENT_JARS_DIR%/jboss-ejb3-vfs-spi.jar;%CLIENT_JARS_DIR%/concurrent.jar;%CLIENT_JARS_DIR%/jboss-ejb3-proxy-spi-client.jar;%CLIENT_JARS_DIR%/jboss-ejb3-core-client.jar;%CLIENT_JARS_DIR%/jboss-integration.jar;.;


For Unix Based OS you can run the following Shell script to set the JBoss EJB Client classpath properly:

export JAVA_HOME=/home/testuser/MyJDKs/jdk160_05
export PATH=${JAVA_HOME}/bin:${PATH}:



export CLASSPATH="${CLIENT_JARS_DIR}/jboss-logging-spi.jar:${CLIENT_JARS_DIR}/jnp-client.jar:${CLIENT_JARS_DIR}/jboss-ejb3-proxy-impl-client.jar:${CLIENT_JARS_DIR}/jboss-ejb3-common-client.jar::${CLIENT_JARS_DIR}/jboss-aspect-jdk50-client.jar:${CLIENT_JARS_DIR}/jboss-remoting.jar:${CLIENT_JARS_DIR}/jboss-aop-client.jar:${CLIENT_JARS_DIR}/jboss-common-core.jar:${CLIENT_JARS_DIR}/jboss-serialization.jar:${CLIENT_JARS_DIR}/jboss-ejb3-security-client.jar:${CLIENT_JARS_DIR}/jbosssx-client.jar:${CLIENT_JARS_DIR}/jboss-security-spi.jar:${CLIENT_JARS_DIR}/jboss-javaee.jar:${CLIENT_JARS_DIR}/ejb3-persistence.jar:${CLIENT_JARS_DIR}/jboss-ejb3-security-client.jar:${CLIENT_JARS_DIR}/jboss-ejb3-vfs-spi.jar:${CLIENT_JARS_DIR}/concurrent.jar:${CLIENT_JARS_DIR}/jboss-ejb3-proxy-spi-client.jar:${CLIENT_JARS_DIR}/jboss-ejb3-core-client.jar:${CLIENT_JARS_DIR}/jboss-integration.jar"


Middleware Magic

Copyright © 2010-2012 Middleware Magic. All rights reserved. |