2017-02-15 98 views
0

我編寫了一個EJB模塊,並將其部署到GlassFish。然後,我寫了一個entreprise應用程序客戶端,並將其部署到GlassFish。對於部署的EJB,JNDI查找失敗

無狀態bean的代碼:

package remote; 

@Stateless 
@Remote(IPersonEJB.class) 
@LocalBean 
public class PersonEJB implements IPersonEJB{ 

@PersistenceContext(unitName = "JobsPU") 
private EntityManager em; 

@Override 
public Person giveJobToPerson(Job job, Person person) { 
    em.persist(person); 
    em.flush(); 
    job.setEmployeeID(person.getId()); 
    em.persist(job); 
    return person; 
} 

// Add business logic below. (Right-click in editor and choose 
// "Insert Code > Add Business Method") 

@Override 
public Person getPerson(Long id) { 
    return em.find(Person.class, id); 
} 
} 

的persistence.xml部署EBJ的:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
    <persistence-unit name="JobsPU" transaction-type="JTA"> 
    <jta-data-source>jdbc/__TimerPool</jta-data-source> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties> 
     <property name="eclipselink.logging.level" value="INFO"/> 
     <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> 
     <property name="eclipselink.target-database" value="Derby"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

我導入項目作爲一個庫到客戶端的項目(通過Netbeans的),才能使用我需要的類:

package jobsclient; 

import Entities.Address; 
import Entities.Job; 
import Entities.Person; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import remote.IPersonEJB; 

public class Main { 

    public static void main(String[] args) throws NamingException { 
     Context ctx = new InitialContext(); 
     IPersonEJB personEJB = (IPersonEJB) ctx.lookup("java:global/Jobs/IPersonEJB!remote.IPersonEJB"); 
     Address add = new Address("Fake street", "Fake city", "FC", "83100"); 
     Person p = new Person("Jack", "Black", add); 
     Job j = new Job("Programmer", 3500f); 

     Person persistente = personEJB.giveJobToPerson(j, p); 
     if (persistente.getId() != null) { 
      System.out.println("Creazione effettuata"); 
     } } 

} 

如果我嘗試運行客戶端,我得到這些異常S:

Warning: C:\Users\Paolo\Documents\NetBeansProjects\JobsClient\dist\gfdeploy\JobsClient does not exist. 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.glassfish.appclient.client.acc.AppClientContainer.launch(AppClientContainer.java:446) 
    at org.glassfish.appclient.client.AppClientFacade.main(AppClientFacade.java:166) 
Caused by: javax.naming.NamingException: Lookup failed for 'java:global/Jobs/IPersonEJB!remote.IPersonEJB' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: IPersonEJB!remote.IPersonEJB not found] 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491) 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438) 
    at javax.naming.InitialContext.lookup(InitialContext.java:417) 
    at jobsclient.Main.main(Main.java:28) 
    ... 6 more 
Caused by: javax.naming.NameNotFoundException: IPersonEJB!remote.IPersonEJB not found 
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208) 
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66) 
    at com.sun.enterprise.naming.impl.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:109) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143) 
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173) 
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528) 
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930) 
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496) 
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222) 
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497) 
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540) 
Java Result: 1 
run: 
BUILD SUCCESSFUL (total time: 9 seconds) 

我試圖找到一個解決方案,我看這樣一個在這裏#2的一些其他問題,但我沒能解決我的問題。我想這是JNDI名稱的問題?但是我找不到它在任何地方部署的名稱(我在glassfish管理控制檯中到處尋找),我認爲名稱字符串的格式是正確的(按照我閱讀的指南進行)。

我使用NetBeans的部署功能部署了兩個應用程序。也許我錯過了一些配置文件(ejb.xml或其他)?

回答

1

我剛剛發現錯誤:我正在查找遠程接口而不是bean本身。

IPersonEJB personEJB = (IPersonEJB) ctx.lookup("java:global/Jobs/IPersonEJB!remote.IPersonEJB"); 

應該

IPersonEJB personEJB = (IPersonEJB) ctx.lookup("java:global/Jobs/PersonEJB!remote.IPersonEJB");