0

I'm a newbie to ejb .I want to know is can two session beans implement the same remote(local) interface, if not why?

The code example is welcome.

Thanks for any help!

Vu NG
  • 280
  • 3
  • 20

2 Answers2

2

Yes, they can.

Example:

public interface NodeService {

    public void start();
}

First implementation:

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(NodeService.class)
public class NodeService1 implements NodeService {

    @Override
    public void start() {
    }

}

Second implemenation:

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(NodeService.class)
public class NodeService2 implements NodeService {

    @Override
    public void start() {
    }

}

See also:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
1

Yes you can.

You can implement any interface(local and remote) by any number of beans, but then you need to specify which particular beans you are injecting.

For simple example you can use beanName attribute:

@Remote
public interface Worker {}    

//

@Stateless(name = "firstBean")
public class Bean1 implements Worker {}

//

@Stateless(name = "secondBean")
public class Bean2 implements Worker {}

//
@Stateless
public class LogicBean {
  @EJB(beanName = "firstBean")
  private Worker worker1;

  @EJB(beanName = "secondBean")
  private Worker worker2;
}

Also you can play around with jndi names through mappedName attribute.

See also:

https://developer.jboss.org/thread/230291?tstart=0

ar4ers
  • 740
  • 5
  • 19
  • with `mappedName` can a host dynamically load a library from a broker? That is, the library is only on the broker. Any hosts using the library find it with JNDI lookups. Is that even possible? – Thufir Mar 11 '15 at 08:42
  • @Thufir, I don't understand you question. Can you reformulate it, please? – ar4ers Mar 11 '15 at 12:05
  • host and broker on different systems, different JVM's. The host uses JMS to communicate with the broker. The library is **only** on the broker. The client, which is on the host, needs the library. Can the client load or find the library with JNDI? (probably not.) – Thufir Mar 11 '15 at 12:15
  • @Thufir, i think it's possible, but it's little more complicated that just `mappedName` attribute. – ar4ers Mar 12 '15 at 19:46