My own experience has taught me always to expect problems when I build Java™ source code. Sure enough, I found a few problems when I tried to run my EJB3 code on JBoss5, but I was able to get around all of them and I'll tell you how I did it in this article. If you want to follow along, I suggest installing two copies of JBoss: version 4 and version 5 (see Resources for links). Also, you need to have JDK version 1.5 installed, with the JAVA_HOME variable pointing to the installation folder—for example, JAVA_HOME= C:\java\jdk1.5.0_06.
Before you begin the migration, start by looking at the EJB3 code that currently runs on JBoss4. Listing 1 shows a simple EJB3 entity class called GreetingCard.
Listing 1. The EJB3 GreetingCard class
@Entity
@Table(name="GREETING_CARD")
public class GreetingCard implements java.io.Serializable
{
private int id;
private String greeting;
private int colour;
@Id
@Column(name="ID")
public int getId()
{
return id;
}
public void setId(int pk)
{
id = pk;
}
@Column(name="NAME")
public String getGreeting()
{
return greeting;
}
public void setGreeting(String str)
{
greeting = str;
}
@Column(name="COLOUR")
public int getColour()
{
return colour;
}
public void setColour(int colour)
{
this.colour = colour;
}
} |
Listing 2 shows a stateless bean class.
Listing 2. A stateless entity bean
@Stateless
public class CardShopBean implements CardShopRemote
{
@PersistenceContext(unitName="cardshop") private EntityManager manager;
public void createGreetingCard(GreetingCard greetingCard)
{
manager.persist(greetingCard);
}
public GreetingCard findGreetingCard(int pKey)
{
return manager.find(GreetingCard.class, pKey);
}
public void removeGreetingCard(GreetingCard greetingCard)
{
manager.remove(greetingCard);
}
public void flushGreetingCard()
{
manager.flush();
}
public void mergeGreetingCard(GreetingCard greetingCard)
{
manager.merge(greetingCard);
}
} |
The simple program class in Listing 3 runs this code.
Listing 3. Running the code
public static void main(String [] args)
{
try
{
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CardShopBean/remote");
CardShopRemote dao = (CardShopRemote)ref;
GreetingCard oldGreetingCard = dao.findGreetingCard(1);
if (oldGreetingCard != null)
{
dao.mergeGreetingCard(oldGreetingCard);
dao.removeGreetingCard(oldGreetingCard);
dao.flushGreetingCard();
}
GreetingCard greetingCard_1 = new GreetingCard();
greetingCard_1.setId(1);
greetingCard_1.setGreeting("Seasons Greetings from Terry Dactyll");
greetingCard_1.setColour(1);
dao.createGreetingCard(greetingCard_1);
GreetingCard greetingCard_2 = dao.findGreetingCard(1);
System.out.println("Greeting card name: " + greetingCard_2.getGreeting());
System.out.println("Greeting card colour: " + greetingCard_2.getColour());
}
catch (javax.naming.NamingException ne)
{
ne.printStackTrace();
}
}
|
When you run this code in JBoss version 4, you get the client-side results in Listing 4.
Listing 4. Running the EJB3 code in JBoss 4
ant run.client
Buildfile: build.xml
run.client:
[java] Greeting card name: Seasons Greetings from Terry Dactyll
[java] Greeting card colour: 1
BUILD SUCCESSFUL
Total time: 10 seconds
|
No surprises there! Now, try to run the same code under JBoss 5. It's easy enough to move between the two environments. The first step is simply to modify the value of the JBOSS_HOME environment variable. On my system, this means I change from JBOSS_HOME=C:\java\jboss4\JEMS-jboss-4.0.5.GA to JBOSS_HOME=C:\java\jboss5\jboss-5.0.0.GA.
Now, when you run ant -p, you get the result in Listing 5.
Listing 5. Problems with paths in JBoss 5
build.xml:35: C:\java\jboss5\jboss-5.0.0.GA\server\default\deploy\ejb3.deployer not found. |
The small change to build.xml illustrated in Listing 6 fixes the problem in Listing 5.
Listing 6. First change to build.xml
<fileset dir="${jboss.home}/server/default/deployers/ejb3.deployer">
|
Running ant -p again uncovers another (similar) error, illustrated in Listing 7.
Listing 7. One more path issue
build.xml:35: C:\java\jboss5\jboss-5.0.0.GA\server\default\deploy\jboss-aop-jdk50.deployer not found. |
As for the previous change, this is also easily fixed (see Listing 8).
Listing 8. Second change to build.xml
<fileset dir="${jboss.home}/server/default/deployers/jboss-aop-jboss5.deployer">
|
At this point, ant -p runs with no problems. It's now time to build the EJB3 code in JBoss 5. Unfortunately, running ant compile results in the problem illustrated in Listing 9. (Note that Listing 9 is a tiny excerpt from the actual output.)
Listing 9. Compilation issues
ant compile
compile:
[javac] Compiling 4 source files to C:\java\jbossmigration\mycode-jboss5\build\classes
[javac] C:\java\jbossmigration\mycode-jboss5\src\main\com\cardsrus\cardshop\
CardShopBean.java:3: package javax.ejb does not exist
[javac] import javax.ejb.Stateless;
[javac] ^
[javac] C:\java\jbossmigration\mycode-jboss5\src\main\com\cardsrus\cardshop\
CardShopBean.java:4: package javax.persistence does not exist
[javac] import javax.persistence.EntityManager;
[javac] ^
|
The problem in Listing 9 is caused by a missing library import. The fix requires another change to build.xml, illustrated in Listing 10. Add the code in Listing 10 into the <fileset> elements in the classpath construction target.
Listing 10. Adding a library import
<fileset dir="${jboss.home}/common/lib">
<include name="*.jar"/>
</fileset>
|
After you make the change in Listing 10, the Java code successfully compiles. Does it deploy on JBoss 5? Unfortunately, it doesn't, as illustrated in the server-side log excerpt in Listing 11.
Listing 11. Deployment problems
16:44:03,093 ERROR [AbstractKernelController] Error installing to Parse: name=vfszip:/C:/java/jboss5/jboss-5.0.0.GA/server/default/deploy/cardsrus.jar state=Not Installed mode=Manual requiredState=Parse org.jboss.deployers.spi.DeploymentException: Error creating managed object for vfszip:/C:/java/jboss5/jboss-5.0.0.GA/server/default/deploy/cardsrus.jar at org.jboss.deployers.spi.DeploymentException.rethrowAsDeploymentException (DeploymentException.java:49) at org.jboss.deployers.spi.deployer.helpers.AbstractParsingDeployerWithOutput. createMetaData (AbstractParsingDeployerWithOutput.java:337) |
To fix the deployment problem requires a modification to the persistence.xml file, as illustrated in Listing 12. In the original persistence.xml file, it was necessary to replace the line <persistence> with the contents of Listing 12.
Listing 12. Modification to persistence.xml
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0" >
|
After you make the change in Listing 12, the code deploys and runs under JBoss 5, with client output similar to what you saw earlier in Listing 4.
So, you're finally out of the woods, having migrated the EJB3 code from JBoss 4 to JBoss 5.
Moving EJB3 code between JBoss 4 and JBoss 5 is not a simple matter. You might need a migration tool such as an Eclipse plug-in to automate some or all of the code changes required. Until then, I hope this article helps make your migration a smooth one.
Learn
- IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
- XML technical library: See the developerWorks XML Zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- The technology bookstore: Browse for books on these and other technical topics.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- developerWorks
podcasts: Listen to interesting interviews and discussions for software developers.
Get products and technologies
- JBoss: Learn more about and download the JBoss products you need.
- IBM product evaluation versions: Download or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
- XML zone discussion forums: Participate in any of several XML-related discussions.
- developerWorks blogs: Check out these blogs and get involved in the developerWorks community.
Stephen Morris is an independent writer/consultant based in Ireland. Widely experienced in enterprise development and networking applications, Stephen has worked for some of the world's biggest networking companies on projects such as Java EE and J2SE-based network management systems, billing applications, financial systems, porting/developing SNMP entities, network device technologies, and several mobile computing applications. He holds a master's degree in computer science and holds three patents in the area of network management. He is the author of Moving Your Career Up the Value Chain: Building Specialized Development Skills in a Global Economy as well as numerous articles on network management and other topics.




