Thursday, 26 June 2008

Terracotta - JBoss AS - EJB3

I am going to use terracotta just to speed up the system.
I still need JBoss AS and its EJB 3 implementation.
Terracotta alone is too flexible which is not appropriate for agile development which heavily depends on forced blueprint like EJB to ensure design quality.

Wednesday, 25 June 2008

Terracotta. What?

While I am enjoying clustering solution using JBoss, I heard about terracotta. An interesting word, so I opened the website.

Somebody said terracotta hundreds time faster than JBoss clustering, and more transparent solution to clustering that the software components you build don't have to be EJBs. Well, I don't know yet.

I am very enthusiastic about learning terracotta tonight, but wait.. A friend needs me to compile and integrate our old java source code tonight :-(

jboss-web.xml for session replication

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE jboss-web PUBLIC
"-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">

<jboss-web>
<replication-config>
<replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger>
<replication-granularity>SESSION</replication-granularity>
<replication-field-batch-mode>true</replication-field-batch-mode>
</replication-config>
</jboss-web>

Installing Web Load Balancing to JBoss 5

After setting up JBoss clusters as explained in Setting up JBoss 5 AS cluster nodes, we can setup the web load balancing.

Install Apache 2. You can get it from http://httpd.apache.org.

Install mod_jk to the apache. You can get it from http://www.apache.org/dist/jakarta/tomcat-connectors/jk/binaries/

Let us say that you have added node1 in mod_jk installation. To the first node of your JBoss 5 cluster edit the file deployers/jbossweb.deployer/server.xml add attribute jvmRoute="node1" to the element <Engine>, so the element will look like this: <Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">.

Edit deployers/jbossweb.deployer/META-INF/war-deployers-beans.xml, to the bean WebAppClusteringDefaultsDeployer add property: true

Do the same to other nodes.

Edit your web applications web.xml. Add <distributable/>

Edit your web applications jboss-web.xml

Installing mod_jk

Get the mod_jk binary. You can get it from http://www.apache.org/dist/jakarta/tomcat-connectors/jk/binaries/.

Copy mod_jk.so to modules directory of your apache web server.

Put mod-jk.conf template in directory conf/extra of apache. Edit the file, replace MyApps with your application url string components.

Edit conf/httpd.conf, add one line: Include conf/extra/mod-jk.conf

Put uriworkermap.properties in directory conf/extra.

Put workers.properties in directory conf/extra. The template assuming that you have two nodes; node1 and node2 residing in host 172.16.1.37 and 172.16.1.195

workers.properties

# Define list of workers that will be used
# for mapping requests
worker.list=loadbalancer,status
# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=172.16.1.37
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.cachesize=10

# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host=172.16.1.195
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.cachesize=10

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
#worker.loadbalancer.balance_workers=node1
worker.loadbalancer.sticky_session=0

#worker.list=loadbalancer
# Status worker for managing load balancer
worker.status.type=status

uriworkermap.properties

# Simple worker configuration file
# Mount the Servlet context to the ajp13 worker
/jmx-console=loadbalancer
/jmx-console/*=loadbalancer
/web-console=loadbalancer
/web-console/*=loadbalancer

mod-jk.conf

# 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/extra/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 /MyApps/* loadbalancer

# 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/extra/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

JkMount status
Order deny,allow
Deny from all
Allow from 127.0.0.1


Tuesday, 24 June 2008

Setting up JBoss 5 AS cluster nodes

Edit deploy/messaging/messaging-service.xml set the value of ServerPeerID with a unique integer to identify the node. If you have two nodes, you can set the value to 0 for the first node and 1 for the second node.

All nodes will share a database for management. Remove the file deploy/messaging/hsqldb-persistence-service.xml. Copy the template file thirdparty/jboss/messaging/resources/mysql-persistence-service.xml to the directory deploy/messaging. Edit the file change all DefaultDS to the mysql data source you have created as explained in JTA Data Source: MySQL Database.

Adding a new message queue to the JBoss 5 AS

Go to deploy/messaging directory.

Create a new XML file, e.g. myqueue-service.xml

if the queue name is MyQueue, the content of the file will be like this:

<server>
<mbean code="org.jboss.jms.server.destination.QueueService"
name="jboss.messaging.destination:service=Queue,name=MyQueue"
xmbean-dd="xmdesc/Queue-xmbean.xml">
<attribute name="JNDIName">queue/MyQueue</attribute>
<annotation>@org.jboss.system.deployers.managed.ManagementObjectClass(code=org.jboss.jms.server.destination.QueueServiceMO)</annotation>
<depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
<depends>jboss.messaging:service=PostOffice</depends>
</mbean>
</server>

JTA Data Source: MySQL Database

To use a mysql database in JBoss, edit or add the XML document with root element <datasources>. The easiest way to do it is by creating a new XML in the deploy directory. For mysql data sources, I will name the file mysql-ds.xml. If my mysql database name is testdb and the mysql server is in localhost:3306 and the user/password is root/secret123 then the content of the file will be like this:

<?xml version=”1.0″ encoding=”UTF-8″?>
<datasources>
<local-tx-datasource>
<jndi-name>MBSDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/testdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>secret123</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>







Creating the server side of mobile banking system

Starting 1 may 2008, we are creating Mobile Banking System.

After some experiments I decided to use JBoss 5 as the application server so I can use the JBoss server clustering and EJB3 implementation feature.

For the web access load balancing, we decided to use apache mod_jk.

I think JBoss 5 and EJB3 is great.

Avoiding Dependency Error in Redeployment

I am using JBoss to create high available servers running 24×7 without needing the application server to be restarted if a component needs to be replaced. to fulfill this requirement, the component should be deployed in a specific manner.
A bean Interface could be needed by other beans. If the bean interface is removed from the hot deploy directory, JBoss class loader will also remove the interface definition from its interface collection. If the interface needed by other beans is missing, the JVM would generate exception.
To avoid the situation, bean Interface and Implementation should be treated as different deployment units.
The interface deployment unit is the unit that rarely changed. The interfaces should be designed well enough so hopefully it will not need to be changed.
The bean implementation deployment unit is the unit allowed to be changed often. The redeployment of this unit will not break the system dependency.