Client call to EJB error: javax.naming.NoInitialContextException

强颜欢笑 提交于 2020-04-30 08:45:31

问题


Calling my EJB from class Main:

MyService myService = (MyService) ctx.lookup(MyService.class.getName());

Gives error:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.ejb.calculator.Main.main(Main.java:26)

How can I call my EJB?

Tried two different JNDI calls:

JNDI_NAME = "java:global/ejb/MyService";
MyService myService = (MyService) ctx.lookup(JNDI_NAME);

and

MyService myService = (MyService) ctx.lookup(MyService.class.getName());

Code:

Source link

https://bitbucket.org/powder366/ejb/src/master/ 

Glassfish commands:

asadmin start-domain --verbose
asadmin stop-domain --verbose 
http://localhost:8080/
http://localhost:4848/common/index.jsf
mvn package
asadmin deploy ejb-1.0-SNAPSHOT.jar

Screenshots:

Note:

My test cases works with the embedded container, but I can't call my external running container.

Used versions are Java8, EJB3.0, Glassfish5.0.1, Java EE8.0.1

Update1: Added log during deplyoment container-deploy-log.txt. See git remote.

Update2: Pushed the working changes to git remote.

Update3: Pushed MDB example to git remote.


回答1:


First of all, if you want to access your EJB from an external client, you need to declare a remote view.

As your EJB only has the @Local annotation, it only offers a local view. You should add the @Remote annotation.

@Local
@Remote
@Stateless
public class MyService implements IMyService {
    public String getMessage() {
        return "Hello!";
    }
}

The global JNDI name is formed by:

java:global/[EAR module]/[EJB module]/[EJB name]

in your case, as there is no EAR, would be:

java:global/ejb-1.0-SNAPSHOT/MyService

This test should work:

Context ctx = new InitialContext();
IMyService myService = (IMyService) ctx.lookup("java:global/ejb-1.0-SNAPSHOT/MyService");
Assert.assertEquals(myService.getMessage(), "Hello!");

UPDATE

You also need to add the glassfish client libraries to the classpath to run the Main class.

I originally tested it with a JUnit test, it worked for me because the project already declared a test dependency on glassfish-embeded-all that includes the glassfish client. But IntelliJ doesn't add test libraries when running a Main class.

You could either change the scope of glassfish-embeded-all to runtime or add a new dependency:

<dependency>
    <groupId>org.glassfish.main.appclient</groupId>
    <artifactId>gf-client</artifactId>
    <version>5.1.0</version>
    <scope>runtime</scope>
</dependency>


来源:https://stackoverflow.com/questions/61307891/client-call-to-ejb-error-javax-naming-noinitialcontextexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!