2

How can I call a Local Session Bean inside an EAR from another EAR, both deployed in the same Glassfish v3 domain?

This is the structure:

Glassfish v3 Domain1

    EAR1
            EAR1-EJB.jar
                    class TestSessionBean           <-- @Stateless
            common.jar
                    interface TestSessionLocal      <-- @Local

    EAR2
            EAR2-EJB.jar
                    class TestSessionBeanClient     <-- @Singleton, @LocalBean
            common.jar
                    interface TestSessionLocal      <-- @Local

TestSessionBean implements TestSessionLocal, boths EARs has common.jar.

I need to use TestSessionBean from TestSessionBeanClient. I would like to take advantage of local session bean because of performance.

I know I can't use a simple @EJB call in the TestSessionBeanClient, so I tried to lookup like this:

InitialContext ic = new InitialContext();
TestSessionLocal tsl = ic.lookup("java:global/EAR1/EAR1-EJB/TestSessionBean!org.test.TestSessionLocal");

That will throw a ClassCastException because the returned object will not be TestSessionLocal but a proxy class like:

TestSessionLocal_1389930137

that to be able to call its methos I must do reflection to find its methods.

Please help.

Thank you in advance.

Xavier Callejas
  • 131
  • 2
  • 11

3 Answers3

7

Per 3.2.2 of the EJB 3.1 specification:

Access to an enterprise bean through the local client view is only required to be supported for local clients packaged within the same application as the enterprise bean that provides the local client view. Compliant implementations of this specification may optionally support access to the local client view of an enterprise bean from a local client packaged in a different application. The configuration requirements for inter-application access to the local client view are vendor-specific and are outside the scope of this specification. Applications relying on inter-application access to the local client view are non-portable.

Here is the GlassFish FAQ: I have an EJB component with a Local interface. Can I access it from a web component in a different application?

(That said, you could try packaging your interface such that it is loaded by a ClassLoader that is common to both applications.)

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
4

You really don't want to do that. as another answer stated, it's not required to be supported. one of the many reasons it's problematic is because it can cause classloader issues. if you have classes in one ear with references to classes in another ear, all kinds of bad things can happen (e.g. having cross-classloader references which will become invalid if the other ear gets redeployed).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Could you explain that or is this just a belief? – ymajoros Feb 05 '13 at 15:01
  • @ymajoros - i thought i did explain it? when you get a reference to a local bean in another ear, you have a direct reference to a Class loaded from that other classloader. what happens to that reference when that other ear is redeployed? you downvoted me because you didn't understand my answer? – jtahlborn Feb 05 '13 at 15:17
  • @ymajoros - added specific example for you. – jtahlborn Feb 05 '13 at 19:27
0

This is the first message I post on Stackoverflow but I admit I read it often. By the way, sorry in advance for my english.

I think I found an alternative solution to this issue :

I have annotated my EJB with @Remote and here's my sun-ejb config.

sun-ejb-jar.xml

  <ejb>
    <ejb-name>XXX</ejb-name>

    <ior-security-config>
        <transport-config>
            <integrity>required</integrity> 
            <confidentiality>required</confidentiality>
            <establish-trust-in-target>supported</establish-trust-in-target>
            <establish-trust-in-client>required</establish-trust-in-client>
        </transport-config>

        <sas-context>
            <caller-propagation>supported</caller-propagation>
        </sas-context>
    </ior-security-config>
  </ejb>

After some tests, it appears that the EJB is not accessible by a client without a known certificate. The others EARs can access to this EJB without any authentication.

Edit: I tried the ClassLoader solution but it's not viable for my project

Ludovic Guillaume
  • 3,237
  • 1
  • 24
  • 41
  • The question is about a local interface. You're answering with a remote one and some glassfish-specific remoting security. – ymajoros Feb 05 '13 at 21:53
  • Yes. As I said, this is an **alternative** solution, not THE solution. Thanks for -1 my post -_- – Ludovic Guillaume Feb 11 '13 at 13:12
  • Sorry, but your answer has nothing to do with the question. -1 is not an insult, it's what keeps this site's quality compared to a random forum. If I'm wrong, be sure that you'll get +1s from other users. – ymajoros Feb 11 '13 at 16:23