aspose file tools*
The moose likes JBoss/WildFly and the fly likes purpose of InitialContext? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "purpose of InitialContext?" Watch "purpose of InitialContext?" New topic
Author

purpose of InitialContext?

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
hello all,

What 'InitialContext' can do?

With Jboss, The code like,


InitialContext context = new InitialContext();

context.lookup("look/into/jboss.xml");

The last statement looks into jboss.xml file and returns the ejb-name?

Thank You!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

Does this help http://www.coderanch.com/t/507616/java-EJB-SCBCD/certification/Remote-client-EJB#2293049

[My Blog] [JavaRanch Journal]
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You Jaikiran!

But we need to set properties to the context object?
then what is the purpose of jboss.xml?

Either define the properties or define jboss.xml or we have to define both?

Thank you again for your kind reply!

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

yuvaraj Mbr wrote:

But we need to set properties to the context object?

If you don't pass the properties to the constructor of InitialContext, then a file named jndi.properties is scanned for, in the classpath. The contents of the jndi.properties are expected to contain the context properties.
yuvaraj Mbr wrote:
then what is the purpose of jboss.xml?

jboss.xml is JBoss specific EJB deployment descriptor used to configure EJBs.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You jaikiran!

I have created the properties like,

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.RemoteInitialContextFactory");
props.put(Context.PROVIDER_URL,"http://164.10.10.10:8080");
Context ctx = new InitialContext(props);

My jboss AS is running on "http://164.10.10.10:8080", and made a lookup with this statement,

Object objref = ctx.lookup("java:comp/env/ejb/testbean/MyAdvice");

The things here are correct? If so, it is not working!


Note: I am using ejb 2.0 and jboss AS 6.0!
Vikram Saxena
Ranch Hand

Joined: Dec 16, 2008
Posts: 53
Yes,
For creating Initial Context in Jboss you have to use jnp and not http.
And the port has to be 1099 by default if you have not changed your settings.

So,
Try and use jnp://164.10.10.10:1099 and you should get the Initial context.

Too add to IC explanation :
Initial context is basically used to get a handle on the Application Servers JNDI tree. It provides the base from where you can look up your resources.

Hope it helps


Vikram
SCJP 5 , SCBCD [Prep Started ] , WLS 8.1 Server Admin
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You Vikram!

I have tried with jnp also. Jboss is running on remote system (164.10.10.10) with port 8080.

properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces");
properties.put(Context.PROVIDER_URL, "jnp://164.10.10.10:8080");

It shows some exception like,

Could not obtain connection to any of these urls: 164.10.10.10:8080 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException;


Any idea about this vikram?

Thank You again!



Vikram Saxena
Ranch Hand

Joined: Dec 16, 2008
Posts: 53
Yes,

8080 is the web port. You have to use the JNDI bind port which is mentioned in the jboss-service.xml in the
server\<server-name>\conf directly.

By default its 1099.

So if you have not hanged it the correct PROVIDER_URL would be :
jnp://164.10.10.10:1099

Also, please post the version of the Jboss App Server you are using.

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
JBOSS 6 vikram!

Ejb 2x.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

Did you try using 1099 port, as suggested by Vikram?
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
yes! i tried! If i use 1099, then it shows "ejb not bound".

If i use 8080, then it shows

"Could not obtain connection to any of these urls: 164.10.10.10:8080 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException;"

Thank You!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

yuvaraj Mbr wrote:yes! i tried! If i use 1099, then it shows "ejb not bound".

That indicates you are using a wrong JNDI name. Check the jndi name of your bean using the JNDIView MBean in the jmx-console and then use it as your lookup string.

yuvaraj Mbr wrote:
If i use 8080, then it shows


Don't use that. As has already been mentioned, that port is not the naming port.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You for your kind reply!

And sorry for asking questions again and again!


My jboss.xml contains,

<session>
<ejb-name>AdviceBean</ejb-name>
<jndi-name>testbean/MyAdvice</jndi-name>
</session>


and ejb-jar.xml has something like,

<session id="test_MyTestSession">

<display-name> AdviceBean </display-name>

<ejb-name>AdviceBean</ejb-name>

<home>com.testbean.beans.AdviceHome</home>
<remote>com.testbean.beans.Advice</remote>
<ejb-class>com.testbean.beans.AdviceBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>
</session>


And i call them from a servlet with these foloowing statements,

Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces");
properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context context = new InitialContext(properties);

Object objref = context.lookup("testbean/MyAdvice");




then why it shows "testbean not bound". It has anything wrong? If so, let me know please!

Thank you!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

yuvaraj Mbr wrote:

then why it shows "testbean not bound". It has anything wrong? If so, let me know please!



What's the output of the JNDIView MBean? Can you post that? Also, while posting code, xml or logs, make sure that you wrap them in a Code block (you can use the Code button in the message editor window).
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
sorry for this!

this is the JNDI tree i have!


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

It doesn't look like your bean is deployed. How are you packaging and deploying the bean? What does the console log show when you deploy your application?

P.S: By the way, is there any specific reason you are using EJB2.x? The latest supported version in AS6 is EJB3.1 and is much more simpler than EJB2.x
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You fro your reply kiran!

sorry to paste this long content. but need to know the problem in this!

This indicates? It s not a deployed structure?



servler.log has something like the following,




Note: No special reasons to use ejb2x. But now only started learning ejb. my senior said that "first try ejb 2x"
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

yuvaraj Mbr wrote:Thank You fro your reply kiran!

sorry to paste this long content. but need to know the problem in this!

This indicates? It s not a deployed structure?



That part from java: namespace indicates that your application has been deployed and the JavaEE6 spec mandated names have been initialized. However that does not indicate that the deployment was picked up as a EJB2.x deployment. The global jndi namespace should have contained the bean references, if it was considered as a EJB2.x deployment.


servler.log has something like the following,
2010-10-07 18:59:28,375 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@15716912{vfs:///D:/servers/jboss-6/server/default/deploy/testbean.ear/testbean.jar/}
2010-10-07 18:59:28,375 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@15716912{vfs:///D:/servers/jboss-6/server/default/deploy/testbean.ear/testbean.jar/}
2010-10-07 18:59:28,375 INFO [org.jboss.ejb3.deployers.Ejb3DependenciesDeployer] (HDScanner) Encountered deployment AbstractVFSDeploymentContext@15716912{vfs:///D:/servers/jboss-6/server/default/deploy/testbean.ear/testbean.jar/}

For some reason, the deployment is being considered as EJB3.x instead of EJB2.x. Just re-read your previous post in this thread about ejb-jar.xml. Is that the exact XML file you have in the testbean.jar/META-INF? It's missing the xsd/dtd declaration.


Note: No special reasons to use ejb2x. But now only started learning ejb. my senior said that "first try ejb 2x"

I personally would recommend you start with EJB3.x instead of EJB2. That way you'll save a lot of trouble trying to get past issues. Also, for JBoss AS, we have the EJB3 tutorials (including the source code) here
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thank You for your kind reply kiran!

this is the entire content of ejb-jar.xml, But now its not even ready to deploy.

just now i have pasted the dtd element!

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
Thanks to All!

problem resolved!

Especially to Jaikiran and Vikram!

EJB 2.1 could be deployed with jboss 6.0. And its working good.

Thank You again!



Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

Glad to know, you got it working!
 
 
subject: purpose of InitialContext?
 
Similar Threads
problem with DB2 connection in a stateful session bean and deployed by Websphere
lookup of JNDI bean reference is not working!
Could not perform loook up operation in JBoss..need urgent help
jndi lookup, returns only local ejb in clustered environment
Jboss and Ejb 2.1 ejb-ref lookup