Hello there, when I was preparing for SCEA (Sun Certified Enterprise Architect) there were lot of threads that I had google'd around and have some information which I want to share. Hope people who are planning to take SCEA 5 or now known as "Java Enterprise Edition 5 Enterprise Architect Certified Master " or OCMJEA or OCMJEA 5 or Oracle Certified Master will find this post useful.
In addition I will post some articles related to J2ee as and when I use/learn new stuff in this space.

Sunday, October 20, 2013

Migrating applications to JBOSS AS7 or JBOSS 7

Migrate to JBOSS7

Migrate to Jboss 7

Migrating to JBOSS EAP6

Migrating applications from Websphere to JBOSS AS 7

JBOSS 7 Migration guide

I recently had a task to migrate all of java based web applications, EJB’s and Webservices from Webspshere to JBOSS AS 7 or JBOSS EAP 6. This article is
applicable for anyone migrating there java based applications to JBOSS & EAP6 and is not specific to Websphere.

There are several aspects that are different in JBOSS 7 like

The way you load resources from classpath (Its not the usual java way weird J)

The classloading problem can be dealt easily with the jboss-deployment-structure.xml (Its awesome !!)

The speed at which jboss starts and stop’s is nowhere close Websphere . Jboss is superfast.

Let see how to set up data source, namespace bindings and configure Queues on JBOSS 7 (aka EAP 6)
In Jboss you use standalone mode if you have single instance of a server in cases where you have multiple servers you use domain mode. In standalone mode
Jboss server uses standalone.xml and in domain mode we end using domain.xml.

Assuming you have jboss deployed under C:\jboss-eap-6.0. Before you can declare a data source you need to install a driver.

Sweet we now have installed the drivers and we can now reference these drivers in standalone.xml (or domain.xml if its domain mode) which is the file that
Jboss reads to load your data sources, Queues, connection factories, name space bindings….etc
Adding a Datasource in JBOSS 7 or EAP 6
:

In your standalone.xml or domain.xml (eg: C:\jboss-eap-.0\standalone\configuration\standalone.xml) go the following section “<subsystem
xmlns="urn:jboss:domain:datasources:1.1">“
(This part of the section from standalone.xml)

Add your property file for your application under a folder called “main/TST” within main.

Your folder structure should look like these

How to write the
jboss-deployment-structure.xml?

Now
in your web/ejb/ear application you can reference these modules using
jboss-deployment-structure.xml as shown below.

<?xml version="1.0" encoding="UTF-8"?><jboss-deployment-structurexmlns:p="urn:jboss:deployment-structure:1.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ear-subdeployments-isolated>true</ear-subdeployments-isolated><deployment><dependencies><!-- load Custom Properties module.
By saying true we are telling that this module is available for any war file or ejb jar
Files if this xml is part of ear file
--><modulename="custom_properties"export="true"/></dependencies></deployment></jboss-deployment-structure>

Where to add
jboss-deployment-structure.xml?

As
mentioned earlier you can copy the jboss-deployments-structure.xml to META-INF
folder if it’s a EAR project or EJB project or in WEB-INF folder if it’s a war
file.

We
can influence class loading in Jboss server by including or excluding any
modules that Jboss loads on start up. You can add this xml at EAR or level or
at WAR level depending on whether you have to influence entire EAR project or
localize to just WAR project

Eg:
Say you are running a JSF 1.2 based application and you want to exclude JSF2.0
jars that JBOSS loads by default you do this.

jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure><deployment><dependencies><!-- Name of module that points to custom properties --><modulename="custom_properties"export="true"/></dependencies><!--
1.Excludes JSF 2.x which is one of the default modules in JBOSS.
The property slot refers to version.
--><exclusions><modulename="javax.faces.api"slot="main"/><modulename="com.sun.jsf-impl"slot="main"/></exclusions></deployment></jboss-deployment-structure>

So
you may wonder how we got these values

name="javax.faces.api"
slot="main"

and
name="com.sun.jsf-impl"
slot="main"

Well
these are the values from module.xml for jsf 2.0 jars in JBOSS which is at

All we had to do was exclude the default web service
modules that jboss was supplying and also to wanted to exclude com/sun/org/apache/xml/internal/security/transforms/implementations
provided by sun jdk module which JBOSS 7 loads.

Also by default the sun.jdk module in Jboss doesn’t load
the following

<path name="sun/security/x509" />

<path name="sun/net/www/protocol/http" />

<path name="com/sun/net/ssl/internal/ssl"
/>

Jboss-deployment-structrue.xml

(Shows how to redefine an existing
jboss module by excluding some default classes)

How to
troubleshoot “startupfailed due to previous errors” in JBOSS 7?

Whenever
you see your ear deployment failing with the error “Startup failed due to previous
error” and with no other exception in the server.log its very annoying. By
default Jboss doesn’t show you all the errors when one of the modules with ear
fails which is weird?

Why
would anyone hide the exceptions and just say “Something went wrong”

Well
there is way we can tell JBOSS to spill the beans (I mean to show exception for
each failed module within the ear)

If you want errors to show for individual artifacts (like
war or ejb-jar within ear)

Add the following
entry "-Dorg.jboss.as.logging.per-deployment=false"

You
can add this in to your servers start up script or if you are using some IDE
like eclipse

-Double click on the server

-Once the server pane opens click one “Edit launch configuration”

Go
to Arguments tab and within that go to “VM arguments “ and add "-Dorg.jboss.as.logging.per-deployment=false"

How To enable
logging of JBOSS class loading ?

Or

How to explore
which classes are loaded from which JARs?

Or

How to find which
jars and in what order are loaded by a classloader?

In case you want to troubleshoot JBOSS Classloading issue
and see which class is being loaded from which jar and you don’t want to change any code ( I will show a trick how to find the jar
file name a class belongs to in case you can change the code )

In your standalone.xml or domain.xml (or
standalone-ha.xml or standalone-full-ha.xml depending on which file you are
using) go to the below section as shown here

<profile>

<subsystem xmlns="urn:jboss:domain:logging:1.1">

<console-handler name="CONSOLE">

<level name="DEBUG"/> =è
default will be INFO

Also to JAVA_OPTS add "-verbose:class" this is parameter to java command don’t use
–D.

Either you can edit standalone.conf.bat or add this param using eclipse by opening
the launch configuration.

NOTE:
This option generates too much information of every class that is being loaded
and should only be used for troubleshooting.

However
if you can change the code and want to see a specific classes and where they
are being loaded from here is the trick.

Use
this line of code on your object or class and you should see the jar file name
in most cases.

I
tried it on JBOSS 7 and it worked and on other hand the same line of code did
not work in Websphere 7.

yourobject.getClass().getProtectionDomain().getCodeSource()

e.g.:
I wanted to from where I was loading the FOPFactory from

FopFactory fopFactory = FopFactory.newInstance();

System.out.println("fopFactory was loaded from :"+
fopFactory.getClass().getProtectionDomain().getCodeSource());

In
addition if you need to make any more changes you can modify the following
properties which are in jboss-web.xml (This file is part of your web
application)

NOTE:
By default these are the values that JBOSS uses. You don’t need mention these
values are this entire section <replication-config> to </replication-config> unless you have strong reason to change the
values. When I tested I did not include this section in my jboss-web.xml

Copy
paste the below few lines into jsp page and let’s call it as SessionInfo.jsp.
Now if you have two nodes just start one node and use your application generic
url and you will see say session id : abc
and server name : server1 and later start node 2 and shutdown node1 and
refresh the page you will see that the server name on the jsp page will change
to server2 but session id will remain same. Proving the session migrated from
server1 to server 2.

Before you test there is one last change on server side
we need to make to specify the cache mode and number of owners. Based on what I read here it looks like DIST
mode is better and number of owners should be set to “2”

1. Go to command prompt in windows or shell prompt in
unix/linux.

2. cd JBOSS_HOME/bin

3. ./jboss-cli.sh
(In case you change the host
name from localhost to some other name you may need to update the following in <host>localhost</host>
in jboss-cli.xml )

JBOSS
EAP 6 by default comes with both JSF 1.2 and JSF 2.0 jars. If you want to force
your web application to load the JSF jar that’s in your web-inf/lib folder you
need to exclude the JBOSS version of JSF jars.

Add
jboss-deployment-structure.xml with the following contents to WEB-INF if its
war or META-INF if its ear file. I added
it to my EAR file

Add
the following to web.xml.This tells my faces to use the JSF jar that we are bundling with war
file.

<context-param>

<param-name>org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL</param-name>

<param-value>true</param-value>

</context-param>

For
all Ajax calls I was getting this error.

java.lang.IllegalStateException:
Parameters processing failed.

at
org.apache.tomcat.util.http.Parameters.processParameters(Parameters.java:407)
[jbossweb-7.0.16.Final-redhat-1.jar:]

Root Cause:EAP 6.0.0.BETA used JBoss
Web 7.0.10.FINAL and as such some changes were made between the BETA and the
GA. Included in these changes was some refactoring of code to improve
performance and this included a flag to register parsing errors. If an improper
http parameter was received it throws an IllegalStateException. In the BETA
this error was just logged as it was not critical. Now this change is breaking
existing applications using certain browsers.

There
is not much you need to do to migrate web services from other servers to JBOSS
as long you are using annotations. Since our applications are deployed on
multiples servers we have one generic url for webservice. We use mod cluster to
load balance the traffic to individual servers. However the rendered WSDL was
returning us the ip address and port number of individual servers instead of
the generic Webservice URL

How to set JBOSS Webservice
endpoint to have the address that client requested?

This
is what we have to do to get the correct end point address in WSDL. In your
Standlaone.xml or domain.xml set
wsdl-host to jbossws.undefined.host