Location of JNDI file - java

Since I am new to EJB, I do not know where to search for the JNDI file of an EJB 3.0 project. I am told that in order to access the EJB classes, we need to lookup the class using the JNDI. So can anyone please suggest where do I usually find the JNDI file for an EJB project?
Note : Since EJB 3.0 is used, the concept of ejb-jar.xml is not used. So please suggest.
Thanks!

there are is no JNDI files, it allow to find objects via the name

there is no JNDI file.but the JNDI names can be found for the EJBs in weblogic server for EJB 2.0. on the other hand, for EJB 3.0, the JNDIs can be found in web.xml file

Related

Is it possible to query/browse the JNDI tree in WebSphere Liberty?

I have a EJB with a remote interface that I want to access from a client. I have difficulties to build the string for the JNDI lookup. Is there a command line tool which shows all register enterprise beans in the naming service? Or can I see this in a log file?
The JNDI name should look like this:
corbaname:localhost:2809#ejb/global/MyApp/MyModule/EJBName!full.package.remote.interface.Name
I’m also not really sure if my app and module name in the string is correct.
As far as I know there is no way to dump the JNDI namespace on WebSphere Liberty (there is a way to do this in WebSphere traditional).
The easiest way to check what JNDI name your EJBs are registered at, is to check the messages.log file. In the messages.log file you should see some CNTR0167I messages which indicate where the server has bound your EJBs. The messages.log file can be found in ${server.config.dir}/logs/messages.log.
Example EJB binding message:
CNTR0167I: The server is binding the com.example.DatabaseBean interface of the DatabaseBean enterprise bean in the TestProject.war module of the TestProject application. The binding location is: java:global/TestProject/DatabaseBean!com.example.DatabaseBean
For more info, check out the IBM doc:
Using enterprise JavaBeans with remote interfaces on Liberty

How to define own JNDI name for EJB in an ear

I have an ear which has ejb.jar, core.jar.
core.jar use lookup to find the bean at run time. Now the jndi path is mysystem-server-component-ear-0.0.1-SNAPSHOT/MyServiceBean/local which means <earname>/<beanname>/<localinterface>
I want to define JNDI like MyServiceBean/local. Where my MyServiceBean is the ejb bean and it use local interface.
I use EJB 3.1 without ejb-jar.xml and jboss.xml. All the wiring done through annotation.
How can I define my own JNDI name which I can lookup
Did you try #EJB(mappedName = "yourJndiName")?
If your JNDI lookups are not working as expected, then you should always check your JNDI tree from within the administration frontend (should be somewhere in jmx-console/JBoss/service=JNDIView or similar). Sometimes the name of the remote interface, etc. is added to your JNDI name by your application server.

How to correctly reference annotated EJB3 beans from JNDI in WAR-project

We have a multimodule Java EE 5 project running on Weblogic 10.3.x. One module has the EJBs and our batch processor is running from the web-module. Since we don't have CDI in JavaEE5, we have to do a JNDI-lookup on the EJBs. The EJBs are defined with #Stateless on the class and #Remote on the interface.
I have succeeded accessing the EJBs by looking the following string:
ejb/batchService#com.example.service.batch.ejb.BatchServiceRemote
However, I belive this is highly platformdependent, and I suspect I should have put something inside the web.xml and probably into the weblogic.xml at least in the web-module - maybe even in the EJB module...
Could anyone enlighten me how to do this propperly? Or is this the best way available?
JNDI format of local bean is
java:comp/env/BeanClassName
JNDI format for remote bean is
mappedName#com.package.BeanClassName
for
#Stateless(mappedName = "mappedName")
public class BeanClassName {
PS. This format supported by WebLogic 10.3. Behaviour of another application servers may be differentю
Prior to EJB 3.1 / EE 6, there are no standardized lookup strings for EJBs. Since they're not standardized, hard-coding the actual binding name of the EJB does make your project product-specific.
The best solution is to create another level of indirection: declare an <ejb-local-ref> in web.xml (or as #EJB/#EJBs on a servlet or other component class), and then use java:comp/env/xyz to lookup the ref. Then, use platform-specific bindings for the EJB ref.

Is it possible to use #EJB annotation to inject EJBs through different servers?

I have 2 session beans, OrderBean and InventoryBean which are deployed at different weblogic servers.
The OrderBean needs to access the InventoryBean to check if the supply is sufficient.
Currently, I use JNDI look up to locate the InventoryBean and it works fine.
Now I'm wondering if it is possible to use #EJB to inject InventoryBean by providing the JNDI name and the URL in xml or somewhere else.
Finally I found a way to do this.
i. Configure the foreign JNDI on the weblogic server and link the remote EJB to a local JNDI name.
For example:
Local JNDI:
InventoryBean#com.pkg.InventoryBean (MAPPEDNAME#FULLNAME)
link to
Remote JNDI:
ServiceBean#com.pkg.InventoryBean
ii. Configure ejb-ref in ejb-jar.xml
ejb-ref-name -> ejb/InventoryBean
remote -> com.pkg.InventoryService
mapped-name -> InventoryBean
iii. Add the #EJB annotation in OrderBean
#EJB(name = "ejb/InventoryBean")
private InventoryService inventoryService;
I think it is not possible through EJB annotations, but you can configure foreign JNDI on your WebLogic server and refer to your remote EJB as a local JNDI name. Though, I never tried that, but I think it should work.
It is very AS-specific.
JBoss 7+ makes it possible if you:
Define outbound-socket-binding, security-realm and remote-outbound-connection in the standalone file (all referring to the remote JBoss instance).
Add a jboss-ejb-client.xml to the META-INF folder of your packaged application, with a remoting-ejb-receiver for every connection needed by the application.
Inject the remote EJB with#EJB(lookup = "<jndi_name>")
Let me know if further details are needed.
Give a look at:
JBoss configuration for remote connections
An article about Glassfish-Glassfish remote connections

Help configuring JNDI with embedded JBoss in Tomcat 5.5.x

When I try the following lookup in my code:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (DataSource) envCtx.lookup("jdbc/mydb");
I get the following exception:
java.sql.SQLException: QueryResults: Unable to initialize naming context:
Name java:comp is not bound in this Context at
com.onsitemanager.database.ThreadLocalConnection.getConnection
(ThreadLocalConnection.java:130) at
...
I installed embedded JBoss following the JBoss wiki instructions. And I configured Tomcat using the "Scanning every WAR by default" deployment as specified in the configuration wiki page.
Quoting the config page:
JNDI
Embedded JBoss components like connection pooling, EJB, JPA, and transactions make
extensive use of JNDI to publish services. Embedded JBoss overrides Tomcat's JNDI
implementation by layering itself on top of Tomcat's JNDI instantiation. There are a few > reasons for this:
To avoid having to declare each and every one of these services within server.xml
To allow seemeless integration of the java:comp namespace between web apps and
EJBs.
Tomcat's JNDI implementation has a few critical bugs in it that hamper some JBoss
components ability to work
We want to provide the option for you of remoting EJBs and other services that can > be remotely looked up
Anyone have any thoughts on how I can configure the JBoss naming service which according to the above quote is overriding Tomcat's JNDI implementation so that I can do a lookup on java:comp/env?
FYI - My environment Tomcat 5.5.9, Seam 2.0.2sp, Embedded JBoss (Beta 3),
Note: I do have a -ds.xml file for my database connection properly setup and accessible on the class path per the instructions.
Also note: I have posted this question in embedded Jboss forum and seam user forum.
Thanks for the response toolkit.... yes, I can access my datasource by going directly to java:jdbc/mydb, but I'm using an existing code base that connects via the ENC. Here's some interesting info that I've found out ....
The above code works with JBoss 4.2.2.GA and here's the JNDI ctx parameters being used:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces:
org.jboss.naming:org.jnp.interfaces
The above code works with Tomcat 5.5.x and here's the JNDI ctx parameters being used:
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
java.naming.factory.url.pkgs=org.apache.naming
The above code fails with Embedded JBoss (Beta 3) in Tomcat 5.5.x with the above error message.
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
java.naming.factory.url.pkgs=org.apache.namingThe above code fails with the above error using JBoss Embedded in tomcat 5.5.x
Anyone have any thoughts I what I need to do with configuring embedded JBoss JNDI configuration?
java:comp/env is known as the Enterprise Naming Context (ENC) and is not globally visible. See here for more information. You will need to locate the global JNDI name which your datasource is regsitered at.
The easiest way to do this is to navigate to JBoss' web-based JMX console and look for a 'JNDIView' (not exactly sure of the name - currently at home) mbean. This mbean should have a list method which you can invoke, which will display the context path for all of the JNDI-bound objects.
I had some similar issue with Jboss Embedded and i finally fix playing in the file:
test-Datasource-ds.xml
adding
<mbean code="org.jboss.naming.NamingAlias" name="jboss.jmx:alias=testDatasource">
<attribute name="FromName">jdbc/Example DataSource</attribute>
<attribute name="ToName">java:/testDatasource</attribute>
</mbean>
The problem was jboss add the prefix java:/ for all data source declared. So finally i had a datasource named testDatasource, overrided with that directive to jdbc/Example DataSource
Hope it works

Categories