0

I have EJB bean

Interface:

package com.xlab.ice.ejb.sessionbean;
import javax.ejb.Remote;

@Remote
public interface Session {
    public String getMessage();
}

Bean:

package com.xlab.ice.ejb.sessionbean;
import javax.ejb.Stateless;

@Stateless
public class SessionBean implements Session {
    public SessionBean() {
    }
    public String getMessage() {
        return "Hello!";
    }
}

It is successfully deployed into Glassfhish 4. But I cannot access it via simple client:

package com.xlab.ice.ejb.sessionbean;
import javax.ejb.EJB;

public class Client {

    @EJB
    private static SessionBean sessionBean;

    public void getMsg() {
        System.out.print(sessionBean.getMessage());
    }

    public static void main(String[] args) {
        new Client().getMsg();
    }
}

When I'm trying to run it via: appclient -client SessionBeanClient.jar I'm getting error. Here is stack trace - http://pastebin.com/JuHRcQp5

What I'm doing wrong?

Alexey Anufriev
  • 415
  • 6
  • 19

4 Answers4

1

You are trying to access the ejb through a standalone client.

This requires specific jndi manual lookup, for which if you are starting off with ejb, would be best place to start off before getting the inns of ejb.

That said:

You need to know the host and CORBA port on which the ejb is deployed.

create an inital context and do a manual lookup.

See this stackoverflow question on how to go about it.

Cant access EJB from a Java SE client - Lookup Failed Error

Community
  • 1
  • 1
maress
  • 3,533
  • 1
  • 19
  • 37
0

Do you deploy the client and the bean on the same server? Than you dont have to use an interface, you can annotate your bean with '@LocalBean', so it's possible to call it from the same JVM. To your problem: You need to inject the bean as follow:

public class Client {

    @EJB
    private static SessionBean sessionBean;
    private final String jndiName = "java:global/find/this/out";

    public void getMsg() {
        InitialContext ctx = new InitialContext();
        sessionBean = (SessionBean) ctx.lookup(ejb_path);
        System.out.print(sessionBean.getMessage());
    }

    public static void main(String[] args) {
        new Client().getMsg();
    }
}

This works only for a local bean, for a remote bean you have to configure the access, look here. You can find the JNDI name in the log file of your glassfish server, after deploying the bean.

jcomouth
  • 324
  • 3
  • 16
0

You should look at what maress said in his answer. Also, you can't inject an EJB into a static field, see this question for the links and references to the EJB spec and why it is not allowed.

Community
  • 1
  • 1
nicoschl
  • 2,346
  • 1
  • 17
  • 17
0
private static SessionBean sessionBean;

should be changed to

private static Session sessionBean;
Alexey Anufriev
  • 415
  • 6
  • 19