users@glassfish.java.net

RE: Debug _at_EJB (javax.ejb.EJB) annotation in Web Application

From: Martin Gainty <mgainty_at_hotmail.com>
Date: Tue, 9 Jun 2009 09:19:39 -0400

this is the best example i found
(courtesy of William DeCoste of JBoss)

/META-INF/ejb-jar.xml
  <enterprise-beans>
      <session>
         <ejb-name>DeploymentDescriptorSession1</ejb-name> <home>org.jboss.tutorial.ejb21_client_adaptors.bean.Session1RemoteHome</home>
         <remote>org.jboss.tutorial.ejb21_client_adaptors.bean.Session1Remote</remote>
      <ejb-class>org.jboss.tutorial.ejb21_client_adaptors.bean.DeploymentDescriptorSession1Bean</ejb-class>
         <session-type>Stateful</session-type>
       <init-method>
            <create-method>
               <method-name>ejbCreate</method-name>
            </create-method>
            <bean-method>
               <method-name>ejbCreate</method-name>
            </bean-method>
         </init-method>
         <transaction-type>Container</transaction-type>

<!-- localHome reference you want to pay attention to -->
         <ejb-local-ref>
            <ejb-ref-name>session2</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <local>org.jboss.tutorial.ejb21_client_adaptors.bean.Session2LocalHome</local>
            <ejb-link>DeploymentDescriptorSession2</ejb-link>
         </ejb-local-ref>
<!-- end localHome reference you pay attention to -->
      </session>

import javax.ejb.*;
import java.rmi.*;
/*** @version <tt>$Revision: 1.1.2.7 $</tt>
 * @author <a href="mailto:bdecoste_at_jboss.com">William DeCoste</a>*/

public interface Session2LocalHome extends EJBHome
{
//a simple create method for LocalHome is all you need as illustrated here
public Session2Local create(String initValue) throws java.rmi.RemoteException, javax.ejb.CreateException;
}

//Session2Bean class which implements Session2LocalHome.class
import javax.ejb.Stateful;
import javax.ejb.Local;
import javax.ejb.Init;
import javax.ejb.LocalHome;
import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.logging.Logger;
/**
 * @version <tt>$Revision: 1.1.2.7 $</tt>
 * @author <a href="mailto:bdecoste_at_jboss.com">William DeCoste</a>
 */
@Stateful(name="Session2")
@Local(Session2Local.class)
@LocalHome(Session2LocalHome.class)
@LocalBinding(jndiBinding = "Session2Local")
public class Session2Bean
{
   private static final Logger log = Logger.getLogger(Session2Bean.class);
   
   private String initValue = null;
   
   public String getInitValue()
   {
      return initValue;
   }
   @Init
   public void ejbCreate(String initValue)
   {
      this.initValue = initValue;
   }
}

///*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA USA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

import javax.naming.*;
import javax.ejb.EJB;
import javax.ejb.EJBs;
import javax.ejb.*;
import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.ejb3.Container;
import org.jboss.logging.Logger;

/** @version <tt>$Revision: 1.1.2.8 $</tt>
 * @author <a href="mailto:bdecoste_at_jboss.com">William DeCoste</a>*/
@Stateful(name="Session1")
@Remote(Session1Remote.class)
@RemoteHome(Session1RemoteHome.class)
@RemoteBinding(jndiBinding = "Session1Remote")
@EJBs({_at_EJB(name="session2", businessInterface=org.jboss.tutorial.ejb21_client_adaptors.bean.Session2LocalHome.class, beanName="Session2")})
public class Session1Bean
{
   private static final Logger log = Logger.getLogger(Session1Bean.class);
   
   private String initValue = null;
   
   public String getInitValue()
   {
      return initValue;
   }
   
   public String getLocalSession2InitValue() throws Exception
   {
      InitialContext jndiContext = new InitialContext();

//Container.ENC_CTX_NAME = "java:comp.ejb3"
      Object s = jndiContext.lookup(Container.ENC_CTX_NAME + "/env/session2");
//the full call looks like jndiContext.lookup("java:comp.ejb3/env/session2");
//notice the ejb3

//Container.ENC_CTX_NAME = "java:comp.ejb3"
      Session2LocalHome home = (Session2LocalHome)jndiContext.lookup(Container.ENC_CTX_NAME + "/env/session2");
//the full call looks like jndiContext.lookup("java:comp.ejb3/env/session2");
//notice the ejb3

//local reference uses the create method to initialise LocalBean as seen here
      Session2Local session2 = home.create("initialized");

      return session2.getInitValue();
   }
   
   @Init
   public void ejbCreate()
   {
      initValue = "initialized";
   }
}

HTH!
Martin Gainty
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Tue, 9 Jun 2009 04:39:26 -0700
> From: glassfish_at_javadesktop.org
> To: users_at_glassfish.dev.java.net
> Subject: Re: Debug @EJB (javax.ejb.EJB) annotation in Web Application
>
> Hello,
>
> Situation: REST WebService is in the same .ear-File as other EJBs and i want to call the EJBs local, but Glassfish doesn't let me do that ;)
>
> So i have the same problem ...and everytime and every variation of this suggestions above doesn't work...
>
> my web.xml looks like this:
>
> [i]
> <?xml version="1.0" encoding="UTF-8"?>
>
> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
> <servlet>
> <display-name>JAX-RS REST Servlet</display-name>
> <servlet-name>REST-Servlet</servlet-name>
> <servlet-class>
> com.sun.jersey.spi.container.servlet.ServletContainer
> </servlet-class>
> <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
> <servlet-name>REST-Servlet</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
>
> <ejb-local-ref>
> <ejb-ref-name>com/inside/foo/impl/FooBean/local</ejb-ref-name>
> <ejb-ref-type>Session</ejb-ref-type>
> <local-home>com.inside.foo.Foo</local-home>
> <local>com.inside.foo.Foo</local>
> <ejb-link>FooBean</ejb-link>
> </ejb-local-ref>
>
> </web-app>
> [/i]
>
> and my Call looks like this:
>
> [i]
> InitialContext ic = new InitialContext();
> Foo bean = (Foo) ic.lookup("java:comp/env/com/inside/foo/impl/FooBean/local");
> [/i]
>
> i am using GlassFish nServer v2.1 and everytime when i try this the variable "bean" is null...where is my mistake?
>
> regards Hannes
> [Message sent by forum member 'szeminator' (szeminator)]
>
> http://forums.java.net/jive/thread.jspa?messageID=349963
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>

_________________________________________________________________
Hotmail® has ever-growing storage! Don’t worry about storage limits.
http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009