• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Running EJB 3.0 Session Bean in JBoss

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I�m facing problems in running a simple Session bean example based on EJB 3.0 spec. For better understand, I have enumerated the following points:-

1.I have installed JBoss AS 4 with EJB support
2.I have set the environment variables (CLASSPATH, JBOSS_HOME etc) appropriately for the required JAR files.
3.Following are the source code files-
//SimpleSession.java
package beans;

import javax.ejb.Remote;

@Remote
public interface SimpleSession
{
public String getEchoString(String clientString);
}

//SimpleSessionBean.java
package beans;

import javax.ejb.Stateless;

@Stateless
public class SimpleSessionBean implements SimpleSession {
public String getEchoString(String clientString) {
return clientString + " - from session bean";
}
}

//impleSessionClient.java
package client;

import beans.SimpleSession;
import javax.naming.InitialContext;

public class SimpleSessionClient {
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
SimpleSession simpleSession
= (SimpleSession) ctx.lookup(SimpleSession.class.getName());
for (int i = 0; i < args.length; i++) {
String returnedString = simpleSession.getEchoString(args[i]);
System.out.println("sent string: " + args[i] +", received string: " + returnedString);
}
}
}

4.This is how I have compiled my code
javac -d . client/*.java
javac -d . beans/*.java

5.And, this is how I have created the ejb3 JAR file
jar cf SimpleSessionApp.ejb3 beans\*.java

6.For deployment, I have copied the JAR file created in step 5 inside the %JBOSS_HOME%/server/all/deploy directory
7.Finally, I�m running the client as shown below:
java
-Djava.naming.factory.initial=
org.jnp.interfaces.NamingContextFactory
-Djava.naming.factory.url.pkgs=
org.jboss.naming rg.jnp.interfaces
-Djava.naming.provider.url=
localhost client.SimpleSessionClient
Now is the time for all good men

But, unfortunately I�m getting the following run-time error
Exception in thread "main" javax.naming.NameNotFoundException: beans.SimpleSession not bound

Can anybody pls let me know the cause with suggestions to recover from the errors.

In anticipation

--Reema
SCJP
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check if the EJB is deployed or not.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am facing the same problem. Have been trying this for past 2 days. What is the solution.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please provide more details about your application and the exception that you are seeing.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"JBOSS_HOME"

It does not require you to have such an environment variable, but you do need a JAVA_HOME, but you already have that too, otherwise you wouldn't have the app server started.

Your problem is the lookup string you are using. You are getting using the className, which isn't how JBoss binds it in the JNDI tree.

I don't recall off the top of my head what that string is, something I try not to clog my brain with. Jaikiran will know it for sure.

Mark
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't recall off the top of my head what that string is, something I try not to clog my brain with. Jaikiran will know it for sure.




For EJB3, by default, the jndi name is a based on the combination of ApplicationName/BeanImplementationClassName/local or ApplicationName/BeanImplementationClassName/remote.

For example, if the application is MyApp and the bean implementation class is org.myapp.ejb.SimpleSession and if you are doing a lookup of remote interface then the default jndi name would be MyApp/SimpleSession/remote
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the following error when I changed the JNDI name

javax.naming.NameNotFoundException: Remote not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.webage.client.TestClient.runTest(TestClient.java:17)
at com.webage.client.TestClient.main(TestClient.java:25)
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am again iterating the steps which I have done to create ejb

1. I created a package com.webage.ejbs
i. Created an Interface

package com.webage.ejbs;
import javax.ejb.*;

@Remote
public interface SimpleBean {
public String sayHello(String name);
}


ii. Create the class for implementing the interface


package com.webage.ejbs;

import javax.ejb.Stateless;

@Stateless
public class SimpleBeanImpl implements SimpleBean {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}


2. I Packed the above class in beans.jar

3. I created a folder and copied the beans.jar in it.

4. I also created a META-INF Folder & copied a application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" version="1.4"
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/application_1_4.xsd">]http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">[/url]
<display-name>Simple</display-name>
<description>Simple EJB3</description>

<module>
<ejb>beans.jar</ejb>
</module>

</application>

5. I named the folder as firstebj3.ear

6. Final Folder Structure
firstebj3.ear
META-INF
application.xml
manifest.mf
beans.jar


7. I copied the firstebj3.ear in <JSOSS_HOME>\server\default\deploy folder

8. I started the server.

9. Following is the snap shot of http://localhost:8080/jmx-console

Searched For: jboss.j2ee
ear=firstebj3.ear,jar=beans.jar,name=SimpleBeanImpl,service=EJB3
module=beans.jar,service=EJB3
service=ClientDeployer
service=EARDeployer
service=EARDeployment,url='firstebj3.ear'
service=EARDeployment,url='jbossejb30.ear'

10. I am trying to call the above ejb from local client

package com.webage.client;

import java.util.Properties;

import javax.naming.*;
import com.webage.ejbs.SimpleBean;

public class TestClient {

public void runTest() throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");
props.setProperty("java.naming.provider.url", "jnp://127.0.0.1:1099");

InitialContext ctx = new InitialContext(props);
SimpleBean bean = (SimpleBean) ctx.lookup("firstebj3/SimpleBeanImpl/Remote");
String result = bean.sayHello("Billy Bob");
System.out.println(result);
}

public static void main(String[] args) {
try {
TestClient cli = new TestClient();
cli.runTest();

} catch (Exception e) {
e.printStackTrace();
}
}
}


WHAT AM I MISSING?



PLEASE HELP!!!
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the JNDI lookup name to firstebj3/SimpleBeanImpl

I got this name from jmx console.!!

I guess now the client is able to do the look up. Now I get error saying that..

java.lang.ClassCastException: org.jnp.interfaces.NamingContext
at com.webage.client.TestClient.runTest(TestClient.java:21)
at com.webage.client.TestClient.main(TestClient.java:29)

Any suggestions!!
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess, you are still using the wrong jndi name for the lookup. You mentioned, you used firstebj3/SimpleBeanImpl/Remote to do the lookup. Change it to:



Note, the lowercase 'r' in Remote. Give it a try.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give jndi name like this

code:

ctx.lookup("SimpleBeanImpl/remote");

it will work fine...
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yups...it worked with ctx.lookup("firstebj3/SimpleBeanImpl/remote");


thanks...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shashank,

Could you post the contents of your ejb-jar.xml?

thanks
 
Nikhil Jain
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

There is no ejbjar.xml. ejb-jar.xml is not mandatory with Ejb3.0
 
Grow a forest with seedballs and this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic