• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble in getting started with EJB3

 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have just started reading Manning -EJB3 in Action and am trying to make a working eg .
I am using JBoss4.2.3GA and MyEclipse5.1 .

The step I followed were:

Step 1.Create a remote interface "HelloUser.java"
Step 2.Create a stateless bean "HelloUserBean.java"
these two are listed as :

"HelloUser.java"
package com.ejb3inaction.actionbazaar.buslogic;
import javax.ejb.Remote;
@Remote
public interface HelloUser {
public void sayHello(String name);
}


"HelloUserBean.java"
package com.ejb3inaction.actionbazaar.buslogic;
import javax.ejb.Stateless;
@Stateless
public class HelloUserBean implements HelloUser {
public void sayHello(String name) {
System.out.println("Hello " + name + " welcome to EJB 3 In Action!");
}
}


Step 3.Deployed the application through MyEclipse on to the Jboss server .
Step 4.Started the JBoss server
Step5.Created a client class "HelloUserClient" listed below


package com.ejb3inaction.actionbazaar.client;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ejb3inaction.actionbazaar.buslogic.HelloUser;
import com.ejb3inaction.actionbazaar.buslogic.HelloUserBean;

public class HelloUserClient {
private static HelloUser helloUser;

public static void main(String[] args) {
try {
Context context = new InitialContext();
helloUser = (HelloUser) context.lookup("chapter1/"
+ HelloUserBean.class.getSimpleName() + "/remote");
helloUser.sayHello("Obama ");
} catch (NamingException e) {
e.printStackTrace();
}
}
}


All classes compiled OK and showed no eror message/warnings.
The error message that I get on Running "HelloUserClient" as a stand alone application is :


javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.ejb3inaction.actionbazaar.client.HelloUserClient.main(HelloUserClient.java:16)



I cant figure out how to remove this error ,did I miss a step some where ,please do explain.
Also I am new at JBoss server , so I cant confirm if the application was actually deployed
on the server or not.

Thanks a lot for helping (and going through such a big post )

 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>Context context = new InitialContext();

Most probably you should pass JNDI factory credential () to InitialContext constructor.

Hopefully book must have mentioned these steps somewhere.
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldnt find it there mate am searching the net for it .
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial


See this
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys ,
Got it working now with your help .As you pointed out the trouble was that I did not have jndi.properties in classpath .

So the working solution can be listed as :

Step 1.
Create "HelloUser.java"
package com.ejb3inaction.actionbazaar;
import javax.ejb.Remote;
@Remote
public interface HelloUser {
public void sayHello(String name);
}



Step 2.
Create "HelloUserBean.java"
package com.ejb3inaction.actionbazaar;
import javax.ejb.Stateless;
@Stateless
public class HelloUserBean implements HelloUser {
public void sayHello(String name) {
System.out.println("Hello " + name + " welcome to EJB 3 In Action!");
}
}


Step3.
Created a client class "HelloUserClient"

package com.ejb3inaction.actionbazaar.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejb3inaction.actionbazaar.buslogic.HelloUser;
import com.ejb3inaction.actionbazaar.buslogic.HelloUserBean;

public class HelloUserClient {
private static HelloUser helloUser;
public static void main(String[] args) {
try {
Context context = new InitialContext();
helloUser = (HelloUser) context.lookup(“HelloUserBean/remote");
helloUser.sayHello("Obama ");
} catch (NamingException e) {
e.printStackTrace();
}
}
}

Step 4.
Create jndi.properties in the souce folder (src folder)

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


Step 5.
Deploy the code ,start the server and run the client as a standalone java application.

PS:
Don’t forget to include jBossall-client.jar ,without it ClassNotFound exception is thrown .

thanks and cheers
 
Abhijit Rai
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An add on issue ,
I am trying to make the HelloUser interface Local ie

@Local
public interface HelloUser {
public void sayHello(String name);
}

I also changed jndi.properties to

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.namingrg.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
jnp.localAddress=jnp://localhost:1099
jnp.localPort=1099

The excepiton I get is :

javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Unresolved address [Root exception is java.net.SocketException: Unresolved address] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.UnknownHostException: jnp://localhost:1099: jnp://localhost:1099]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1562)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:634)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.ejb3inaction.actionbazaar.HelloUserClient.main(HelloUserClient.java:17)
Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.UnknownHostException: jnp://localhost:1099: jnp://localhost:1099]]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:274)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1533)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.UnknownHostException: jnp://localhost:1099: jnp://localhost:1099]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:248)
... 5 more
Caused by: java.net.UnknownHostException: jnp://localhost:1099: jnp://localhost:1099



What changes should be done (I guess to jndi.properties)to access the bean locally ?

Thanks all for helping out .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic