== Example EJB for jboss5 in docker container ==
{{attachment:ejbjarFiles.png}}
{{{#!highlight sh
docker exec -it jboss5_container bash
cd /app/ejbjee5
mvn clean compile package
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 ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Remote;
@Remote
public interface ScheduleRemote {
int addSchedule(int val1, int val2);
}
}}}
=== ./src/main/java/org/allowed/bitarus/ScheduleLocal.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Local;
@Local
public interface ScheduleLocal {
int addSchedule(int val1, int val2) ;
}
}}}
=== ./src/main/java/org/allowed/bitarus/ScheduleWS.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.allowed.bitarus.ScheduleRemote;
@Stateless
@WebService
public class ScheduleWS implements ScheduleRemote {
@WebMethod
public int addSchedule(int val1, int val2) {
return val1 + val2;
}
}
}}}
=== ./src/main/resources/META-INF/ejb-jar.xml ===
{{{#!highlight java
ScheduleWS
org.allowed.bitarus.ScheduleLocal
org.allowed.bitarus.ScheduleRemote
org.allowed.bitarus.ScheduleWS
Stateless
Container
}}}
=== ./pom.xml ===
{{{#!highlight xml
4.0.0
org.allowed.bitarus
ejbjee5
0.0.1
ejb
javaee
javaee-api
5
provided
src/main/java
maven-compiler-plugin
1.5
}}}
* ./SoapUI-5.5.0
* new soap project
* initial wsdl: http://127.0.0.1:8080/ejbjee5-0.0.1/ScheduleWS?wsdl
* http://127.0.0.1:8080/ejbjee5-0.0.1/ScheduleWS
{{{#!highlight xml
1
2
}}}
{{{#!highlight xml
3
}}}
== EJB 3.0 Contacts, JPA ==
=== src/main/java/org/allowed/bitarus/ContactsLocal.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Local;
import java.util.List;
@Local
public interface ContactsLocal {
void add(String firstName, String lastName, String address, String phoneNumber);
List getAll();
ContactEntity get(long id);
void remove(long id);
void edit(long id, String firstName, String lastName, String address, String phoneNumber );
}
}}}
=== src/main/java/org/allowed/bitarus/ContactsWS.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.allowed.bitarus.ContactsRemote;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import java.util.List;
import javax.persistence.NamedQuery;
@Stateless
@WebService
public class ContactsWS implements ContactsRemote, ContactsLocal {
@PersistenceContext(unitName="puContactsDefaultDS")
EntityManager em;
@WebMethod
public void add(String firstName, String lastName, String address, String phoneNumber ) {
ContactEntity c = new ContactEntity();
c.setFirstName(firstName);
c.setLastName(lastName);
c.setAddress(address);
c.setPhoneNumber(phoneNumber);
c.setCreationDate( new java.util.Date() );
em.persist(c);
}
@WebMethod
public List getAll(){
return em.createQuery("select a from ContactEntity a ").getResultList();
}
@WebMethod
public ContactEntity get(long id){
//return (ContactEntity) em.createQuery("select a from ContactEntity a where a.id = :id").setParameter("id", id).getSingleResult();
return (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
}
@WebMethod
public void remove(long id){
ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
em.remove(ent);
}
@WebMethod
public void edit(long id, String firstName, String lastName, String address, String phoneNumber ){
ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
ent.setFirstName(firstName);
ent.setLastName(lastName);
ent.setAddress(address);
ent.setPhoneNumber(phoneNumber);
em.persist(ent);
}
}
}}}
=== src/main/java/org/allowed/bitarus/ContactsRemote.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface ContactsRemote {
void add(String firstName, String lastName, String address, String phoneNumber);
List getAll();
ContactEntity get(long id);
void remove(long id);
void edit(long id, String firstName, String lastName, String address, String phoneNumber );
}
}}}
=== src/main/java/org/allowed/bitarus/ContactEntity.java ===
{{{#!highlight java
package org.allowed.bitarus;
import java.util.Date;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.TemporalType;
import javax.persistence.Temporal;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries( { @NamedQuery(name="Contacts.byId",query="select a from ContactEntity a where a.id = :id") } )
public class ContactEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String address;
@Column
private String phoneNumber;
public void setId(long id){this.id=id;}
public long getId(){return this.id;}
public void setCreationDate(Date creationDate){this.creationDate=creationDate;}
public Date getCreationDate(){return this.creationDate;}
public void setFirstName(String firstName){this.firstName=firstName;}
public String getFirstName(){return this.firstName;}
public void setLastName(String lastName){this.lastName=lastName;}
public String getLastName(){return this.lastName;}
public void setAddress(String address){this.address=address;}
public String getAddress(){return this.address;}
public void setPhoneNumber(String phoneNumber){this.phoneNumber=phoneNumber;}
public String getPhoneNumber(){return this.phoneNumber;}
}
}}}
=== src/main/resources/META-INF/ejb-jar.xml ===
{{{#!highlight xml
ContactsWS
org.allowed.bitarus.ContactsLocal
org.allowed.bitarus.ContactsRemote
org.allowed.bitarus.ContactsWS
Stateless
Container
}}}
=== src/main/resources/META-INF/persistence.xml ===
{{{#!highlight xml
java:/DefaultDS
}}}
=== pom.xml ===
{{{#!highlight xml
4.0.0
org.allowed.bitarus
ejbcontacts
0.0.1
ejb
javaee
javaee-api
5
provided
src
maven-compiler-plugin
1.5
}}}
== EJB Jar with persistence ==
* http://127.0.0.1:8080/jbossws/services
* http://127.0.0.1:8080/ejbjee5-0.0.1/AddWS?wsdl
=== src/main/java/org/allowed/bitarus/AddEntity.java ===
{{{#!highlight java
package org.allowed.bitarus;
import java.util.Date;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.TemporalType;
import javax.persistence.Temporal;
@Entity
public class AddEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Column
private int op1;
@Column
private int op2;
@Column
private int res;
public long getId(){return this.id;}
public void setId(long id){ this.id=id;}
public Date getCreationDate(){return this.creationDate;}
public void setCreationDate(Date creationDate){ this.creationDate=creationDate;}
public int getOp1(){return this.op1;}
public void setOp1(int op1){ this.op1=op1;}
public int getOp2(){return this.op2;}
public void setOp2(int op2){ this.op2=op2;}
public int getRes(){return this.res;}
public void setRes(int res){ this.res=res;}
}
}}}
=== src/main/java/org/allowed/bitarus/AddLocal.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Local;
import java.util.List;
@Local
public interface AddLocal {
int add(int val1, int val2) ;
List getAll();
}
}}}
=== src/main/java/org/allowed/bitarus/AddWS.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.allowed.bitarus.AddRemote;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import java.util.List;
@Stateless
@WebService
public class AddWS implements AddRemote {
@PersistenceContext(unitName="puDefaultDS")
EntityManager em;
@WebMethod
public int add(int val1, int val2) {
int res = val1 + val2;
AddEntity me = new AddEntity();
me.setRes(res);
me.setOp1(val1);
me.setOp2(val2);
me.setCreationDate( new java.util.Date() );
em.persist(me);
return res;
}
@WebMethod
public List getAll(){
return em.createQuery("select a from AddEntity a ").getResultList();
}
}
}}}
=== src/main/java/org/allowed/bitarus/AddRemote.java ===
{{{#!highlight java
package org.allowed.bitarus;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface AddRemote {
int add(int val1, int val2);
List getAll();
}
}}}
=== src/main/resources/META-INF/ejb-jar.xml ===
{{{#!highlight xml
AddWS
org.allowed.bitarus.AddLocal
org.allowed.bitarus.AddRemote
org.allowed.bitarus.AddWS
Stateless
Container
}}}
=== src/main/resources/META-INF/persistence.xml ===
{{{#!highlight xml
java:/DefaultDS
}}}
=== pom.xml ===
{{{#!highlight xml
4.0.0
org.allowed.bitarus
ejbjee5
0.0.1
ejb
javaee
javaee-api
5
provided
src
maven-compiler-plugin
1.5
}}}