Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Tip: Migrating from JBoss 4 to JBoss 5

Smooth the way with XML configuration changes

Stephen B Morris, Consultant, Omey Communications
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.

Summary:  Trying to migrate your EJB code to JBoss 5? Can't get the code to deploy and run? If so, this tip is for you. Or, are you new to JBoss 5 but you want to get a feel for the EJB 3 environment? This tip details the required XML deployment files to build, deploy, and run EJB3 code on JBoss5.

View more content in this series

Date:  21 Jul 2009
Level:  Intermediate PDF:  A4 and Letter (29KB | 8 pages)Get Adobe® Reader®
Also available in:   Chinese  Korean  Portuguese

Activity:  41516 views
Comments:  

Frequently used acronyms

  • EJB: Enterprise Java Bean
  • JDK: Java Development Kit
  • XML: Extensible Markup Language

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.

Upgrade paths matter

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.


Conclusion

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.


Resources

Learn

Get products and technologies

Discuss

About the author

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Java technology
ArticleID=413383
ArticleTitle=Tip: Migrating from JBoss 4 to JBoss 5
publish-date=07212009
author1-email=stephenbjm@yahoo.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers