Plug Bean to JBoss with JNDI

2019-08-27 22:59发布

问题:

I wonder how object (if it matter I need EJB) can be plugged to JBoss (5.0) with JNDI?

I have following bean definition in my Spring applicationContext.xml:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<context:annotation-config/>

<bean id="myServiceFacade" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="MyServiceFacadeBean/remote"/>
    <property name="cache" value="true"/>
    <property name="lookupOnStartup" value="true"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory
            </prop>
            <prop key="java.naming.provider.url">localhost:1099</prop>
            <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces
            </prop>
        </props>
    </property>
    <property name="proxyInterface" value="my.company.service.facade.MyServiceFacade"/>
</bean>

when I try to run JBoss I get:

Caused by: javax.naming.NameNotFoundException: MyServiceFacadeBean/remote
at org.jboss.ha.jndi.HAJNDI.lookupRemotely(HAJNDI.java:264)
at org.jboss.ha.jndi.HAJNDI.lookup(HAJNDI.java:205)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.ha.framework.interfaces.HARMIClient.invoke(HARMIClient.java:318)
at $Proxy165.lookup(Unknown Source)

Maybe some additional steps should be made for registering objects with JBoss/JNDI?

Note, I've tried already to put ejb specific files to JBoss (jboss.xml, ejb-jar.xml) but that doesn't help.

回答1:

How do you loop-up remote in your DataSource? But I am sure you can't get it from your MyServiceFacadeBean.

in applicationContext.xml:

<property name="jndiName" value="remote"/>

While loooping up,

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/remote");
DataSource ds = (DataSource)envCtx.lookup("remote");

or you could make an intermediate step so you don't have to specify "java:comp/env" for every resource you retrieve:

Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/remote");
DataSource ds = (DataSource)envCtx.lookup("remote");

Hope it helps.