File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Problem with Stateless EJB in JBOSS 4.0.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "Problem with Stateless EJB in JBOSS 4.0.4" Watch "Problem with Stateless EJB in JBOSS 4.0.4" New topic
Author

Problem with Stateless EJB in JBOSS 4.0.4

Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi there,

I�m trying to run na EJB application developed with JBOSS IDE 2.0 and JBOSS 4.0.4 GA. I have an remote stateless session bean named TestStatelessBean that inherit from TestStateless. Here is the code for them:

package com.test.ejbs;

import javax.ejb.Remote;

@Remote
public interface TestStateless {
public String testBean();
}

----------------------------------------------------------------------------
package com.test.ejbs;

import javax.ejb.*;
import com.test.ejbs.TestStateless;

public @Stateless class TestStatelessBean implements TestStateless {

public String testBean() {
return "the server returned this string";
}

}
------------------------------------------------------------------------------

I have a servlet that calls the EJB:

package com.test.web;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.io.*;

import com.test.ejbs.*;

public class TestStatelessEJBServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;
private TestStatelessBean statelessBean;

public void init() {
try {
Context context = new InitialContext();
System.out.println("Before lookup");
//statelessBean = (TestStatelessBean) context.lookup("TestStatelessBean/remote");
statelessBean = (TestStatelessBean) context.lookup(TestStatelessBean.class.getName());
System.out.println("After lookup");

} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("The stateless bean returned this string: " +
statelessBean.testBean());
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
this.doGet(req, resp);
}
}

--------------------------------------------------------------------

If I try to get the bean with the code �statelessBean = (TestStatelessBean) context.lookup("TestStatelessBean/remote");� I get a class cast exception.

If try to get the bean with the code �statelessBean = (TestStatelessBean) context.lookup(TestStatelessBean.class.getName());� I get a Bean not bound exception.

I deployed my bean with the JBOSS IDE in the files TestInstallation.ejb3 and TetsInstallation.war. My project name is ejb3teste. These two files are in $JBOSS_HOME\server\default\deploy. The content of jndi.properties is:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming rg.jnp.interfaces
java.naming.provider.url=localhost:1099

The web.xml file contains:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee [url=http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">[/url]
<servlet>
<servlet-name>TestStatelessEJBServlet</servlet-name>
<servlet-class>com.test.web.TestStatelessEJBServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestStatelessEJBServlet</servlet-name>
<url-pattern>/testStatelessEJB</url-pattern>
</servlet-mapping>
</web-app>

The content of the file packaging-buid.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Packaging Generator" default="_packaging_generation_">
<target name="_packaging_generation_" depends="N65540,N65565"/>
<target name="N65540" description="TestInstallation.ejb3">
<jar destfile="TestInstallation.ejb3">
<zipfileset dir="src">
<include name="jndi.properties"/>
</zipfileset>
<zipfileset dir="bin" includes="**/ejbs/*"/>
</jar>
</target>
<target name="N65565" description="TestInstallation.war">
<jar destfile="TestInstallation.war">
<zipfileset dir="src" prefix="WEB-INF">
<include name="web.xml"/>
</zipfileset>
<zipfileset dir="bin" prefix="WEB-INF/classes" includes="**/web/*"/>
</jar>
</target>
</project>

In the jmx-console I have:

jboss.j2ee

* jar=TestInstallation.ejb3,name=TestStatelessBean,service=EJB3
* module=TestInstallation.ejb3,service=EJB3
* service=ClientDeployer
* service=EARDeployer

jboss.web

* J2EEApplication=none,J2EEServer=none,WebModule=//localhost/TestInstallation,j2eeType=Servlet,name=TestStatelessEJBServlet

----------------------------------------------------------

The JNDI Namespace

Global JNDI Namespace

+- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
+- TestStatelessBean (class: org.jnp.interfaces.NamingContext)
| +- remote (proxy: $Proxy58 implements interface com.test.ejbs.TestStateless,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

----------------------------------------------------------------------

I�m calling the servlet with the command line �http://mymachine:8080/TestInstallation/testStatelessEJB�. I have to use this URL because the URL �http://localhost:8080/TestInstallation/testStatelessEJB� doesn�t work. Do you think this can be the cause of the error?

Thank you very much.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17234
    
    1

You should be casting to the interface, not to the actual Bean.

So the first one you got the class cast exception, change it to cast to the interface instead.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

And note that you're probably getting the home interface of the bean, not the bean itself.


[My Blog]
All roads lead to JavaRanch
Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Thank you very much Mark.

I changed the code according your instructions and it worked fine.

Nei
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8145
    
  52

And note that you're probably getting the home interface of the bean, not the bean itself.


No longer in EJB3


[My Blog] [JavaRanch Journal]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

oops sorry, I didn't realize there were annotations in the first post
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem with Stateless EJB in JBOSS 4.0.4
 
Similar Threads
Simplest App ever - problem
Help with EJB DI
code using the filter is not working
Configure tomcat resource factory
Calling controller servlet form a jsp page through an anchor tag