2012-08-31 46 views
5

美好的一天!我是JavaEE 6中的新成員。我嘗試使用以下教程創建一個已啓動的項目:Creating and Running an Application Client on the GlassFish Server。我一步一步地創建了四個項目:JavaSE lib來存儲EJB遠程接口,一個EJB模塊,以及本教程中介紹的其他方法。Hello World!與遠程無狀態會話Bean

我的平臺:

  • 經銷商ID:Ubuntu的說明:Ubuntu的10.10發行版:10.10 代號:特立獨行
  • jdk1.6.0_26
  • Netbean7.2(構建201207171143)
  • GlassFish應用服務器3+

我的遠程接口

package ejb.started; 
    //  
    import javax.ejb.Remote; 
    // 
    @Remote 
    public interface StartedSessionBeanRemote { 

     String getHelloWorld(); 

    } 

它的實現:

package ejb.started; 
// 
import javax.ejb.Stateless; 
// 
@Stateless(name="StartedSessionBeanRemote") 
public class StartedSessionBean implements StartedSessionBeanRemote { 

    @Override 
    public String getHelloWorld() { 
     return ("Hello world, EJB!"); 
    } 

} 

而且客戶端代碼:

package startedentappclient; 
// 
import ejb.started.StartedSessionBeanRemote; 
import javax.ejb.EJB; 
// 
public class AppClientMain { 
    @EJB 
    private static StartedSessionBeanRemote startedSessionBean; 
    public static void main(String[] args) { 
     System.err.println("EJB: "+startedSessionBean.getHelloWorld()); 
    } 
} 

當我嘗試運行遠程客戶端凸出,我收到以下輸出:

ant -f /home/ilya/Workspace/git/workspace/StartedEntAppClient -DforceRedeploy=false run 
init: 
EJBRemoteInterface.init: 
Deleting: /home/ilya/Workspace/git/workspace/EJBRemoteInterface/build/built-jar.properties 
EJBRemoteInterface.deps-jar: 
Updating property file: /home/ilya/Workspace/git/workspace/EJBRemoteInterface/build/built-jar.properties 
EJBRemoteInterface.compile: 
EJBRemoteInterface.jar: 
deps-jar: 
compile: 
library-inclusion-in-archive: 
dist: 
pre-run-deploy: 
Redeploying /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/StartedEntAppClient.jar 
Initializing... 
post-run-deploy: 
run-deploy: 
Copying 1 file to /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist 
Copying 2 files to /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/StartedEntAppClientClient 
Warning: /home/ilya/Workspace/git/workspace/StartedEntAppClient/dist/gfdeploy/StartedEntAppClient does not exist. 
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session into class startedentappclient.AppClientMain: Lookup failed for 'java:comp/env/startedentappclient.AppClientMain/startedSessionBean' 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} 
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703) 
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470) 
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:213) 
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectClass(InjectionManagerImpl.java:205) 
    at org.glassfish.appclient.client.acc.AppClientContainer$ClientMainClassSetting.getClientMainClass(AppClientContainer.java:625) 
    at org.glassfish.appclient.client.acc.AppClientContainer.getMainMethod(AppClientContainer.java:517) 
    at org.glassfish.appclient.client.acc.AppClientContainer.completePreparation(AppClientContainer.java:411) 
    at org.glassfish.appclient.client.acc.AppClientContainer.prepare(AppClientContainer.java:319) 
    at org.glassfish.appclient.client.AppClientFacade.prepareACC(AppClientFacade.java:278) 
    at org.glassfish.appclient.client.acc.agent.AppClientContainerAgent.premain(AppClientContainerAgent.java:82) 
    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 sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:323) 
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338) 
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/startedentappclient.AppClientMain/startedSessionBean' 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.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' 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: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found]]] 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518) 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599) 
    ... 15 more 
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=startedentappclient.AppClientMain/startedSessionBean,Remote 3.x interface =ejb.started.StartedSessionBeanRemote,ejb-link=null,lookup=,mappedName=,jndi-name=ejb.started.StartedSessionBeanRemote,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' [Root exception is javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' 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: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found]] 
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:191) 
    at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1109) 
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776) 
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744) 
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169) 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498) 
    ... 18 more 
Caused by: javax.naming.NamingException: Lookup failed for 'ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote' 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: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found] 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518) 
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:186) 
    ... 23 more 
Caused by: javax.naming.NameNotFoundException: ejb.started.StartedSessionBeanRemote#ejb.started.StartedSessionBeanRemote not found 
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215) 
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77) 
    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:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144) 
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:174) 
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:528) 
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:199) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1624) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1486) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:990) 
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:214) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:742) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:539) 
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2324) 
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497) 
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540) 
Java Result: 1 
run: 
BUILD SUCCESSFUL (total time: 11 seconds) 

我不能認爲是什麼問題。我在第二臺個人電腦上建立了相同的項目,併成功完成所有工作那個擁有相同的軟件平臺。

+0

我發現教程有瑕疵。他們建議做一個企業應用程序。相反,如果您只製作一個EJB模塊,它應該像宣傳的那樣工作。請參閱:http://stackoverflow.com/questions/25828542/how-to-create-a-remote-session-ejb-from-a-client/25829907#25829907 – Thufir

回答

4

當你寫:

@EJB 
private static StartedSessionBeanRemote startedSessionBean; 

您的客戶端嘗試執行EJB的injection。在你的情況下,顯然不成功:

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject... 

你可以通過使用mappedName構造來幫助自己。重寫代碼的這些行以下列方式:

@Stateless(mappedName="ejb/StartedSessionBeanRemote") 
public class StartedSessionBean implements StartedSessionBeanRemote { 

@EJB(mappedName="ejb/StartedSessionBeanRemote") 
private static StartedSessionBeanRemote startedSessionBean; 

最後,確保你的EJB是在客戶端之前部署的,因爲沒有EJB客戶端不能注入它:)

+0

是的,我忘記部署應用程序客戶端和企業應用程序)我糾正它,一切開始工作。謝謝! – ilya8891

+0

但它現在正在爲我工​​作。 Yar .....我嘗試了很多例子,但沒有注入'Remote Ejb'。我通過https://netbeans.org/kb/docs/javaee/entappclient。html#Exercise_32示例 – Yubaraj

4

我相信你不能使用主方法中的@EJB註釋來訪問EJB,因爲該方法不受服務器管理。相反,你應該使用JNDI。

How do I access a Remote EJB component from a stand-alone java client?

+0

非常感謝。正如我寫的,我是進入者。因此,在特定情況下,我將非常感謝所有與JavaEE和JavaBeans有關的有用材料。 – ilya8891

+1

@carcaret:在獨立Java應用程序中是這樣,但在企業應用程序客戶端的情況下則如此,正如ilya8891的問題中所述。您可以在這裏正常使用注射。 –

+0

無效鏈接...... – Thufir

相關問題