Example EJB for jboss5 in docker container

ejbjarFiles.png

   1 docker exec -it jboss5_container bash
   2 cd /app/ejbjee5
   3 mvn clean compile package
   4 cp /app/ejbjee5/target/ejbjee5-0.0.1.jar /app/jboss-5.1.0.GA/server/default/deploy/

18:04:14,955 INFO  [JBossASKernel] Created KernelDeployment for: ejbjee5-0.0.1.jar
18:04:14,955 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=ejbjee5-0.0.1.jar,name=ScheduleWS,service=EJB3
18:04:14,955 INFO  [JBossASKernel]   with dependencies:
18:04:14,955 INFO  [JBossASKernel]   and demands:
18:04:14,955 INFO  [JBossASKernel]      jboss.ejb:service=EJBTimerService
18:04:14,955 INFO  [JBossASKernel]   and supplies:
18:04:14,956 INFO  [JBossASKernel]      Class:org.allowed.bitarus.ScheduleRemote
18:04:14,956 INFO  [JBossASKernel]      jndi:ScheduleWS/local-org.allowed.bitarus.ScheduleLocal
18:04:14,956 INFO  [JBossASKernel]      jndi:ScheduleWS/remote
18:04:14,956 INFO  [JBossASKernel]      Class:org.allowed.bitarus.ScheduleLocal
18:04:14,956 INFO  [JBossASKernel]      jndi:ScheduleWS/local
18:04:14,956 INFO  [JBossASKernel]      jndi:ScheduleWS/remote-org.allowed.bitarus.ScheduleRemote
18:04:14,956 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=ejbjee5-0.0.1.jar,name=ScheduleWS,service=EJB3) to KernelDeployment of: ejbjee5-0.0.1.jar
18:04:14,957 INFO  [EJB3EndpointDeployer] Deploy AbstractBeanMetaData@542c21cb{name=jboss.j2ee:jar=ejbjee5-0.0.1.jar,name=ScheduleWS,service=EJB3_endpoint bean=org.jboss.ejb3.endpoint.deployers.impl.EndpointImpl properties=[container] constructor=null autowireCandidate=true}
18:04:15,020 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=ejbjee5-0.0.1.jar,name=ScheduleWS,service=EJB3
18:04:15,020 INFO  [EJBContainer] STARTED EJB: org.allowed.bitarus.ScheduleWS ejbName: ScheduleWS
18:04:15,041 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

        ScheduleWS/remote - EJB3.x Default Remote Business Interface
        ScheduleWS/remote-org.allowed.bitarus.ScheduleRemote - EJB3.x Remote Business Interface
        ScheduleWS/local - EJB3.x Default Local Business Interface
        ScheduleWS/local-org.allowed.bitarus.ScheduleLocal - EJB3.x Local Business Interface

18:04:15,097 INFO  [DefaultEndpointRegistry] register: jboss.ws:context=ejbjee5-0.0.1,endpoint=ScheduleWS
18:04:15,225 INFO  [WSDLFilePublisher] WSDL published to: file:/app/jboss-5.1.0.GA/server/default/data/wsdl/ejbjee5-0.0.1.jar/ScheduleWSService4435570151338675382.wsdl
18:04:15,248 INFO  [TomcatDeployment] deploy, ctxPath=/ejbjee5-0.0.1

./src/main/java/org/allowed/bitarus/ScheduleRemote.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Remote;
   4 
   5 @Remote
   6 public interface ScheduleRemote {
   7         int addSchedule(int val1, int val2);
   8 }

./src/main/java/org/allowed/bitarus/ScheduleLocal.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Local;
   4 
   5 @Local
   6 public interface ScheduleLocal {
   7         int addSchedule(int val1, int val2) ;
   8 }

./src/main/java/org/allowed/bitarus/ScheduleWS.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Stateless;
   4 import javax.jws.WebMethod;
   5 import javax.jws.WebService;
   6 import org.allowed.bitarus.ScheduleRemote;
   7 
   8 @Stateless
   9 @WebService
  10 public class ScheduleWS implements ScheduleRemote {
  11 
  12         @WebMethod
  13         public int addSchedule(int val1, int val2) {
  14                 return val1 + val2;
  15         }
  16 }

./src/main/resources/META-INF/ejb-jar.xml

   1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
   4         version="3.0">
   5         <enterprise-beans>
   6                 <session>
   7                         <ejb-name>ScheduleWS</ejb-name>
   8                         <business-local>org.allowed.bitarus.ScheduleLocal</business-local>
   9                         <business-remote>org.allowed.bitarus.ScheduleRemote</business-remote>
  10                         <ejb-class>org.allowed.bitarus.ScheduleWS</ejb-class>
  11                         <session-type>Stateless</session-type>
  12                         <transaction-type>Container</transaction-type>
  13                 </session>
  14         </enterprise-beans>
  15 </ejb-jar>

./pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>ejbjee5</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>ejb</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>javaee</groupId>
  12                         <artifactId>javaee-api</artifactId>
  13                         <version>5</version>
  14                         <scope>provided</scope>
  15                 </dependency>
  16         </dependencies>
  17         <build>
  18                 <sourceDirectory>src/main/java</sourceDirectory>
  19                 <plugins>
  20                         <plugin>
  21                                 <artifactId>maven-compiler-plugin</artifactId>
  22                                 <configuration>
  23                                         <source>1.5</source>
  24                                         <target>1.5</target>
  25                                 </configuration>
  26                         </plugin>
  27                 </plugins>
  28         </build>
  29 </project>

   1 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bit="http://bitarus.allowed.org/">
   2    <soapenv:Header/>
   3    <soapenv:Body>
   4       <bit:addSchedule>
   5          <arg0>1</arg0>
   6          <arg1>2</arg1>
   7       </bit:addSchedule>
   8    </soapenv:Body>
   9 </soapenv:Envelope>

   1 <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   2    <env:Header/>
   3    <env:Body>
   4       <ns2:addScheduleResponse xmlns:ns2="http://bitarus.allowed.org/">
   5          <return>3</return>
   6       </ns2:addScheduleResponse>
   7    </env:Body>
   8 </env:Envelope>

EJB 3.0 Contacts, JPA

src/main/java/org/allowed/bitarus/ContactsLocal.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Local;
   4 import java.util.List;
   5 
   6 @Local
   7 public interface ContactsLocal {
   8     void add(String firstName, String lastName, String address, String phoneNumber);
   9     List<ContactEntity> getAll();
  10     ContactEntity get(long id);
  11     void remove(long id);
  12     void edit(long id, String firstName, String lastName, String address, String phoneNumber  );
  13 }

src/main/java/org/allowed/bitarus/ContactsWS.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Stateless;
   4 import javax.jws.WebMethod;
   5 import javax.jws.WebService;
   6 import org.allowed.bitarus.ContactsRemote;
   7 import javax.persistence.PersistenceContext;
   8 import javax.persistence.EntityManager;
   9 import java.util.List;
  10 import javax.persistence.NamedQuery;
  11 
  12 @Stateless
  13 @WebService
  14 public class ContactsWS implements ContactsRemote, ContactsLocal {
  15     @PersistenceContext(unitName="puContactsDefaultDS")
  16     EntityManager em;
  17 
  18     @WebMethod
  19     public void add(String firstName, String lastName, String address, String phoneNumber ) {
  20         ContactEntity c = new ContactEntity();
  21         c.setFirstName(firstName);
  22         c.setLastName(lastName);
  23         c.setAddress(address);
  24         c.setPhoneNumber(phoneNumber);
  25         c.setCreationDate( new java.util.Date() );
  26         em.persist(c);
  27     }
  28     
  29     @WebMethod
  30     public List<ContactEntity> getAll(){
  31       return em.createQuery("select a from ContactEntity a ").getResultList();
  32     }
  33 
  34     @WebMethod
  35     public ContactEntity get(long id){
  36       //return (ContactEntity) em.createQuery("select a from ContactEntity a where a.id = :id").setParameter("id", id).getSingleResult();
  37       return (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
  38     }
  39 
  40     @WebMethod
  41     public void remove(long id){
  42       ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
  43       em.remove(ent);
  44     }
  45 
  46     @WebMethod
  47     public void edit(long id, String firstName, String lastName, String address, String phoneNumber  ){
  48       ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
  49       ent.setFirstName(firstName);
  50       ent.setLastName(lastName);
  51       ent.setAddress(address);
  52       ent.setPhoneNumber(phoneNumber);
  53       em.persist(ent);
  54     }
  55 
  56 }

src/main/java/org/allowed/bitarus/ContactsRemote.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Remote;
   4 import java.util.List;
   5 
   6 @Remote
   7 public interface ContactsRemote {
   8     void add(String firstName, String lastName, String address, String phoneNumber);
   9     List<ContactEntity> getAll();
  10     ContactEntity get(long id);
  11     void remove(long id);
  12     void edit(long id, String firstName, String lastName, String address, String phoneNumber  );
  13 
  14 }

src/main/java/org/allowed/bitarus/ContactEntity.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.Date;
   4 import javax.persistence.Id;
   5 import javax.persistence.Entity;
   6 import javax.persistence.Column;
   7 import javax.persistence.GeneratedValue;
   8 import javax.persistence.GenerationType;
   9 import javax.persistence.TemporalType;
  10 import javax.persistence.Temporal;
  11 import javax.persistence.NamedQueries;
  12 import javax.persistence.NamedQuery;
  13 
  14 @Entity
  15 @NamedQueries( { @NamedQuery(name="Contacts.byId",query="select a from ContactEntity a where a.id = :id")   } )
  16 public class ContactEntity{
  17     @Id
  18     @GeneratedValue(strategy = GenerationType.IDENTITY) 
  19     private long id;
  20 
  21     @Column
  22     @Temporal(TemporalType.TIMESTAMP) 
  23     private Date creationDate;
  24 
  25     @Column
  26     private String firstName;
  27 
  28     @Column
  29     private String lastName;
  30 
  31     @Column
  32     private String address;
  33 
  34     @Column
  35     private String phoneNumber;
  36 
  37     public void setId(long id){this.id=id;}
  38     public long getId(){return this.id;}
  39 
  40     public void setCreationDate(Date creationDate){this.creationDate=creationDate;}
  41     public Date getCreationDate(){return this.creationDate;}
  42 
  43     public void setFirstName(String firstName){this.firstName=firstName;}
  44     public String getFirstName(){return this.firstName;}
  45 
  46     public void setLastName(String lastName){this.lastName=lastName;}
  47     public String  getLastName(){return this.lastName;}
  48 
  49     public void setAddress(String address){this.address=address;}
  50     public String getAddress(){return this.address;}
  51 
  52     public void setPhoneNumber(String phoneNumber){this.phoneNumber=phoneNumber;}
  53     public String getPhoneNumber(){return this.phoneNumber;}
  54 
  55 }

src/main/resources/META-INF/ejb-jar.xml

   1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
   4         version="3.0">
   5         <enterprise-beans>
   6                 <session>
   7                         <ejb-name>ContactsWS</ejb-name>
   8                         <business-local>org.allowed.bitarus.ContactsLocal</business-local>
   9                         <business-remote>org.allowed.bitarus.ContactsRemote</business-remote>
  10                         <ejb-class>org.allowed.bitarus.ContactsWS</ejb-class>
  11                         <session-type>Stateless</session-type>
  12                         <transaction-type>Container</transaction-type>
  13                 </session>
  14         </enterprise-beans>
  15 </ejb-jar>

src/main/resources/META-INF/persistence.xml

   1 <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   2     <persistence-unit name="puContactsDefaultDS" transaction-type="JTA">
   3         <jta-data-source>java:/DefaultDS</jta-data-source>
   4         <properties>
   5           <property name="hibernate.show_sql" value="true" />
   6           <property name="hibernate.format_sql" value="true" />
   7           <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
   8           <property name="hibernate.hbm2ddl.auto" value="update"/>
   9         </properties>
  10     </persistence-unit>
  11 </persistence>

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>ejbcontacts</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>ejb</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>javaee</groupId>
  12                         <artifactId>javaee-api</artifactId>
  13                         <version>5</version>
  14                         <scope>provided</scope>
  15                 </dependency>
  16         </dependencies>
  17         <build>
  18                 <sourceDirectory>src</sourceDirectory>
  19                 <plugins>
  20                         <plugin>
  21                                 <artifactId>maven-compiler-plugin</artifactId>
  22                                 <configuration>
  23                                         <source>1.5</source>
  24                                         <target>1.5</target>
  25                                 </configuration>
  26                         </plugin>
  27                 </plugins>
  28         </build>
  29 </project>

EJB Jar with persistence

src/main/java/org/allowed/bitarus/AddEntity.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.Date;
   4 import javax.persistence.Id;
   5 import javax.persistence.Entity;
   6 import javax.persistence.Column;
   7 import javax.persistence.GeneratedValue;
   8 import javax.persistence.GenerationType;
   9 import javax.persistence.TemporalType;
  10 import javax.persistence.Temporal;
  11 
  12 @Entity
  13 public class AddEntity{
  14     @Id
  15     @GeneratedValue(strategy = GenerationType.IDENTITY) 
  16     private long id;
  17     @Column
  18     @Temporal(TemporalType.TIMESTAMP) 
  19     private Date creationDate;
  20     @Column
  21     private int op1;
  22     @Column
  23     private int op2;
  24     @Column
  25     private int res;
  26     
  27     public long getId(){return this.id;}
  28     public void setId(long id){ this.id=id;}
  29 
  30     public Date getCreationDate(){return this.creationDate;}
  31     public void setCreationDate(Date creationDate){ this.creationDate=creationDate;}
  32     
  33     public int getOp1(){return this.op1;}
  34     public void setOp1(int op1){ this.op1=op1;}        
  35 
  36 
  37     public int getOp2(){return this.op2;}
  38     public void setOp2(int op2){ this.op2=op2;}        
  39 
  40     public int getRes(){return this.res;}
  41     public void setRes(int res){ this.res=res;}        
  42 }

src/main/java/org/allowed/bitarus/AddLocal.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Local;
   4 import java.util.List;
   5 
   6 @Local
   7 public interface AddLocal {
   8         int add(int val1, int val2) ;
   9         List<AddEntity> getAll();
  10 }

src/main/java/org/allowed/bitarus/AddWS.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Stateless;
   4 import javax.jws.WebMethod;
   5 import javax.jws.WebService;
   6 import org.allowed.bitarus.AddRemote;
   7 import javax.persistence.PersistenceContext;
   8 import javax.persistence.EntityManager;
   9 import java.util.List;
  10 
  11 @Stateless
  12 @WebService
  13 public class AddWS implements AddRemote {
  14     @PersistenceContext(unitName="puDefaultDS")
  15     EntityManager em;
  16 
  17     @WebMethod
  18     public int add(int val1, int val2) {
  19         int res = val1 + val2;
  20         AddEntity me = new AddEntity();
  21         me.setRes(res);
  22         me.setOp1(val1);
  23         me.setOp2(val2);
  24         me.setCreationDate( new java.util.Date() );
  25         em.persist(me);
  26         return res;
  27     }
  28 
  29     @WebMethod
  30     public List<AddEntity> getAll(){
  31       return em.createQuery("select a from AddEntity a ").getResultList();
  32     }
  33 
  34 }

src/main/java/org/allowed/bitarus/AddRemote.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Remote;
   4 import java.util.List;
   5 
   6 @Remote
   7 public interface AddRemote {
   8     int add(int val1, int val2);
   9     List<AddEntity> getAll();
  10 }

src/main/resources/META-INF/ejb-jar.xml

   1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
   4         version="3.0">
   5         <enterprise-beans>
   6                 <session>
   7                         <ejb-name>AddWS</ejb-name>
   8                         <business-local>org.allowed.bitarus.AddLocal</business-local>
   9                         <business-remote>org.allowed.bitarus.AddRemote</business-remote>
  10                         <ejb-class>org.allowed.bitarus.AddWS</ejb-class>
  11                         <session-type>Stateless</session-type>
  12                         <transaction-type>Container</transaction-type>
  13                 </session>
  14         </enterprise-beans>
  15 </ejb-jar>

src/main/resources/META-INF/persistence.xml

   1 <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   2     <persistence-unit name="puDefaultDS" transaction-type="JTA">
   3         <jta-data-source>java:/DefaultDS</jta-data-source>
   4         <properties>
   5           <property name="hibernate.show_sql" value="true" />
   6           <property name="hibernate.format_sql" value="true" />
   7           <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
   8           <property name="hibernate.hbm2ddl.auto" value="update"/>
   9         </properties>
  10     </persistence-unit>
  11 </persistence>

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>ejbjee5</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>ejb</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>javaee</groupId>
  12                         <artifactId>javaee-api</artifactId>
  13                         <version>5</version>
  14                         <scope>provided</scope>
  15                 </dependency>
  16         </dependencies>
  17         <build>
  18                 <sourceDirectory>src</sourceDirectory>
  19                 <plugins>
  20                         <plugin>
  21                                 <artifactId>maven-compiler-plugin</artifactId>
  22                                 <configuration>
  23                                         <source>1.5</source>
  24                                         <target>1.5</target>
  25                                 </configuration>
  26                         </plugin>
  27                 </plugins>
  28         </build>
  29 </project>

Java/JBoss5/EJB (last edited 2023-05-29 08:56:42 by 127)