Dans cet exemple nous allons voir :
- comment créer un ejb 3.2 session stateless et remote. Cet ejb sera déployé dans Glassfish 4.
- comment tester cet ejb en ligne de commande. Pour cela, on utilisera la notion d'ACC (Application Client Container). Ainsi, via une classe java lancée en ligne de commande, on pourra solliciter l'EJB (d'où la déclaration d'un ejb de type remote car le client et l'ejb ne sont pas dans la même jvm).
1 Création du projet Maven de type jar contenant les interfaces
1.1 Le fichier pom.xml
Les interfaces à implémenter sont regroupées dans un projet maven dédié dont voici le pom :
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.prj1</groupId> <artifactId>inter</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>Prj1Interface</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
1.1 Création de l'interface
package com.mycompany.prj1.inter; import javax.ejb.Remote; @Remote public interface ISimpleSession { public String sayHello(); }
2 Création du projet Maven de type EJB
2.1 Le fichier pom.xml
Voici le fichier pom.xml correspondant :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.prj1</groupId> <artifactId>testEjbSession</artifactId> <packaging>ejb</packaging> <version>1.0</version> <name>Prj1TestEjbSession</name> <url>http://maven.apache.org</url> <dependencies>
<dependency> <groupId>com.mycompany.prj1</groupId> <artifactId>inter</artifactId> <version>1.0</version> <type>jar</type> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <version>2.3</version> <configuration> <ejbVersion>3.2</ejbVersion> </configuration> </plugin> </plugins> <finalName>testEjbSession</finalName> </build> </project>
2.2 Création de l'EJB de type stateless
package com.mycompany.prj1.ejb; import com.mycompany.prj1.inter.ISimpleSession; import javax.ejb.Stateless; @Stateless public class SimpleSessionBean implements ISimpleSession{ private final String message = "Hello"; public String sayHello() { return message; } }
1.3 Déploiement de l'EJB
Déployer cet ejb sur le serveur Glassfish.
3 Création du projet Maven pour tester l'EJB
3.1 Le fichier pom.xml
Voici le fichier pom.xml correspondant:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.prj1</groupId> <artifactId>testEjbSessionClient</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>Prj1TestEjbSessionClient</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.mycompany.prj1</groupId> <artifactId>inter</artifactId> <version>1.0</version> <type>jar</type> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>testEjbSessionClient</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass> com.mycompany.prj1.clientEjb.SessionBeanClient </mainClass> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <!-- NOTE: We don't need a groupId specification because the group is org.apache.maven.plugins ...which is assumed by default. --> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.mycompany.prj1.clientEjb.SessionBeanClient</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
3.2 Création du client de l'EJB
package com.mycompany.prj1.clientEjb; import com.mycompany.prj1.inter.ISimpleSession; import javax.ejb.EJB; import javax.naming.NamingException; public class SessionBeanClient { @EJB private static ISimpleSession simpleSession; private void invokeSessionBeanMethods() throws NamingException { System.out.println(simpleSession.sayHello()); System.out.println("\nSimpleSession is of type: " + simpleSession.getClass().getName()); } public static void main(String[] args) throws NamingException { new SessionBeanClient().invokeSessionBeanMethods(); } }
3.3 Génération du jar client avec maven assembly:assembly
mvn assembly:assembly
3.4 Exécution du client EJB avec appclient
appclient -client target/testEjbSessionClient-jar-with-dependencies.jar Hello SimpleSession is of type: com.mycompany.prj1.ejb._ISimpleSession_Wrapper