IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Glassfish et Payara Java Discussion :

Dialogue client et projet EJB 3.0 sous Glassfish


Sujet :

Glassfish et Payara Java

  1. #1
    Candidat au Club
    Inscrit en
    Octobre 2009
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Dialogue client et projet EJB 3.0 sous Glassfish
    Bonjour,

    Je débute sur un mini-projet qui est architecturé comme suit :
    DB Oracle, Serveur Glassfish 2.1 et IDE Netbeans 6.5. Tout se passe dans un premier temps en localhost, mais la condition est qu'il faut séparer les projets : 1 projets pur EJB et 1 projet client qui va aller faire du CRUD.
    J'ai créé un premier projet EJB qui parvient bien à encapsuler avec des Entity deux tables (c'est basique mais c'est un mini-projet) de la DB Oracle. Le pool de connexion marche bien. Ce projet comporte un fichier xml de persistence.
    J'ai ensuite créé un second projet que j'appelle "client". Dans ce projet, j'ai créé une classe BusinessDelegate liée à la table Personne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
     
    public class PersonneDelegate {
     
        // ======================================
        // =     Personne Business methods     =
        // ======================================
     
        public static void createPersonne(Personne personne) {
            getPersonneRemote().create(personne);
        }
     
        public static Personne findPersonne(Long personneId) {
            return getPersonneRemote().find(personneId);
        }
     
        public static void deletePersonne(Personne personne) {
            getPersonneRemote().remove(personne);
        }
     
        public static void updatePersonne(Personne personne) {
            getPersonneRemote().edit(personne);
        }
     
        public static List<Personne> findPersonnes() {
            return getPersonneRemote().findAll();
        }
     
        // ======================================
        // =            Private methods         =
        // ======================================
        private static PersonneFacadeLocal getPersonneRemote() {
           PersonneFacadeLocal personneFacadeLocal = null;
            personneFacadeLocal = (PersonneFacadeLocal) ServiceLocator.getInstance().getRemoteInterface("Personne");
            return personneFacadeLocal;
        }
    }
    Et le service locator suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    public class ServiceLocator {
     
        // ======================================
        // =             Attributes             =
        // ======================================
     
        private Context initialContext;
        private Map<String, Object> cache;
     
        // ======================================
        // =             Singleton              =
        // ======================================
        private static ServiceLocator instance = null;
     
        public static ServiceLocator getInstance() {
            if (instance == null)
                instance = new ServiceLocator();
     
            return instance;
        }
     
        private ServiceLocator() throws ServiceLocatorException {
            try {
                initialContext = new InitialContext(PropertyGlassfish.getInstance().getProperties());
                cache = new HashMap<String, Object>();
            } catch (Exception e) {
                throw new ServiceLocatorException(e);
            }
        }
     
        // ======================================
        // =           Business methods         =
        // ======================================
        /**
         * will get the ejb Local home factory. If this ejb home factory has already been
         * clients need to cast to the type of EJBHome they desire
         *
         * @param jndiName JNDI name of the EJB that we are looking for
         * @return the EJB Home corresponding to the homeName
         * @throws ServiceLocatorException thrown if lookup problems
         */
        public Object getRemoteInterface(String jndiName) throws ServiceLocatorException {
            Object remoteInterface = getRemoteObject(jndiName);
            return remoteInterface;
        }
     
         // ======================================
        // =            Private methods         =
        // ======================================
        private synchronized Object getRemoteObject(String jndiName) throws ServiceLocatorException {
            Object remoteObject = cache.get(jndiName);
            if (remoteObject == null) {
                try {
                    remoteObject = initialContext.lookup(jndiName);
                    cache.put(jndiName, remoteObject);
                } catch (Exception e) {
                    throw new ServiceLocatorException(e);
                }
            }
            return remoteObject;
        }
    J'ai lié les deux projets et j'ai compris qu'il fallait passer au constructeur de l'InitialContext des informations :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public class PropertyGlassfish {
     
        private Properties props = null;
     
        // ======================================
        // =             Singleton              =
        // ======================================
        private static PropertyGlassfish instance = null;    
     
        public static PropertyGlassfish getInstance() {
            if (instance == null)
                instance = new PropertyGlassfish();
            return instance;
        }
     
        public Properties getProperties() {
            props = new Properties();
            props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            props.setProperty("java.naming.provider.url","localhost");
            return props;
        }
     
    }
    J'ai ajouté des librairies au projet client et j'ai créé un main comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    public class TestClient { 
     
        public static void main(String... strings) {
              List<Personne> list = PersonneDelegate.findPersonnes();
              Iterator it = list.iterator();
              while(it.hasNext())
                  System.out.println(it.next());
        }
    }
    J'ai fait grâce des différents "import" de début de classe pour alléger le code.
    Quand je lance le main, il y a une erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Exception in thread "main" locator.ServiceLocatorException: javax.naming.NameNotFoundException: Personne not found
            at locator.ServiceLocator.getRemoteObject(ServiceLocator.java:63)
            at locator.ServiceLocator.getRemoteInterface(ServiceLocator.java:49)
            at delegate.PersonneDelegate.getPersonneRemote(PersonneDelegate.java:41)
            at delegate.PersonneDelegate.findPersonnes(PersonneDelegate.java:33)
            at Main.TestClient.main(TestClient.java:11)
    Caused by: javax.naming.NameNotFoundException: Personne not found
    Je pense qu'il doit manquer un autre fichier xml dans le projet client mais lequel ? où faut-il le placer ?
    Merci si qq'un peut m'éclairer.

  2. #2
    Candidat au Club
    Inscrit en
    Octobre 2009
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Après qqs recherches, je pense que le fichier manquant doit être le descripteur de déployement qui doit être placé dans le projet client.
    Ce fichier est un fichier XML, je viens d'en voir un sur le net :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     
    <description>Describe here the content of this file</description>
     
      <display-name>helloEJB</display-name>
     
     
     
      <enterprise-beans>
     
        <session>
     
          <description>Describe here the session bean hello</description>
     
          <display-name>helloEJB/helloEJB</display-name>
     
          <ejb-name>helloEJB</ejb-name>
     
          <home>hw.helloHome</home>
     
          <remote>hw.hello</remote>
     
          <ejb-class>hw.helloEJB</ejb-class>
     
          <session-type>Stateless</session-type>
     
          <transaction-type>Container</transaction-type>
     
        </session>
     
      </enterprise-beans>
    Comment mon projet client peut prendre en compte ce genre de fichier car il est constitué que de classes java et d'un main ?
    Je patauge, je patauge....

  3. #3
    Membre émérite
    Avatar de alexismp
    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    1 503
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 1 503
    Points : 2 777
    Points
    2 777
    Par défaut
    Pourquoi autant de couches? C'est un vrai labyrinthe!

    Cette commande te donnera les EJB distants disponibles dans l'arbre JNDI de GlassFish :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    % asadmin list-jndi-entries
    Le mieux c'est quand même d'utiliser des injections @EJB. Si vraiment tu veux un client Java SE, alors regarde du coté de l'ACC.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Chargement d'un fichier dans un projet EJB sous Eclipse
    Par flamant dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 04/10/2014, 09h22
  2. [EJB3] [Eclipse] problème de références entre un projet EJB et son client
    Par olivier57b dans le forum Java EE
    Réponses: 2
    Dernier message: 17/03/2010, 16h00
  3. reférencer un projet EJB dans un client web
    Par amonray dans le forum Java EE
    Réponses: 1
    Dernier message: 05/10/2009, 10h05
  4. [TClientSocket] Dialogue client/serveur Delphi 6
    Par Hikaru dans le forum Web & réseau
    Réponses: 6
    Dernier message: 31/03/2009, 10h28
  5. client/serveur tcp/ip en c sous unix
    Par oclone dans le forum Développement
    Réponses: 8
    Dernier message: 19/04/2005, 18h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo