Java/EJB3/Stateful Session Bean
Содержание
- 1 EJB Tutorial from JBoss: stateful session bean
- 2 Ejb With Generic Method
- 3 Remove Annotation
- 4 Remove a Stateful Session Bean
- 5 Stateful Session Bean And Entity Manager
- 6 Stateful Session Bean And Shopping Cart
- 7 Stateful Session Bean Lifecycle: PostActivate
- 8 Stateful Session Bean Lifecycle: PostConstruct
- 9 Stateful Session Bean Lifecycle: PreDestroy
- 10 Stateful Session Bean Lifecycle: PrePassivate
- 11 Use Lifecycle Method To Manage Collection In Stateful Session Bean
EJB Tutorial from JBoss: stateful session bean
File: ShoppingCart.java
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.tutorial.stateful.bean;
import java.util.HashMap;
import javax.ejb.Remove;
public interface ShoppingCart
{
void buy(String product, int quantity);
HashMap<String, Integer> getCartContents();
@Remove void checkout();
}
File: ShoppingCartBean.java
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.tutorial.stateful.bean;
import java.io.Serializable;
import java.util.HashMap;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Remote;
@Stateful
@Remote(ShoppingCart.class)
public class ShoppingCartBean implements ShoppingCart, Serializable
{
private HashMap<String, Integer> cart = new HashMap<String, Integer>();
public void buy(String product, int quantity)
{
if (cart.containsKey(product))
{
int currq = cart.get(product);
currq += quantity;
cart.put(product, currq);
}
else
{
cart.put(product, quantity);
}
}
public HashMap<String, Integer> getCartContents()
{
return cart;
}
@Remove
public void checkout()
{
System.out.println("To be implemented");
}
}
File: Client.java
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file 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
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.tutorial.stateful.client;
import java.util.HashMap;
import javax.naming.InitialContext;
import org.jboss.tutorial.stateful.bean.ShoppingCart;
/**
* Comment
*
* @author
== EJB Tutorial from JBoss: stateful session bean deployment descriptor ==
<!-- start source code -->
<source lang="java">
File: jboss.xml
<?xml version="1.0"?>
<jboss
xmlns="http://java.sun.ru/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.ru/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss_5_0.xsd"
version="3.0">
<enterprise-beans>
<session>
<ejb-name>ShoppingCart</ejb-name>
<jndi-name>org.jboss.tutorial.stateful_deployment_descriptor.bean.ShoppingCart</jndi-name>
</session>
</enterprise-beans>
</jboss>
File: ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
xmlns="http://java.sun.ru/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.ru/xml/ns/javaee
http://java.sun.ru/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<description>JBoss Stateful Session Bean Tutorial</description>
<display-name>JBoss Stateful Session Bean Tutorial</display-name>
<enterprise-beans>
<session>
<ejb-name>ShoppingCart</ejb-name>
<remote>org.jboss.tutorial.stateful_deployment_descriptor.bean.ShoppingCart</remote>
<ejb-class>org.jboss.tutorial.stateful_deployment_descriptor.bean.ShoppingCartBean</ejb-class>
<session-type>Stateful</session-type>
<remove-method>
<bean-method>
<method-name>checkout</method-name>
</bean-method>
<retain-if-exception>false</retain-if-exception>
</remove-method>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
Ejb With Generic Method
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.transaction.UserTransaction;
@Stateful
@TransactionManagement(TransactionManagementType.BEAN)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
@PersistenceContext(unitName = "EmployeeService", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Resource
private UserTransaction ut;
public EmployeeBean() {
}
public void doAction() {
Employee emp = new Employee();
emp.setFirstName("first");
em.persist(emp);
}
public <T> T mergeEntity(T entity) {
return em.merge(entity);
}
public <T> T persistEntity(T entity) {
em.persist(entity);
return entity;
}
public <T> T refreshEntity(T entity) {
em.refresh(entity);
return entity;
}
public void removeEntity(Object entity) {
em.remove(em.merge(entity));
}
public void beginTrans()
throws Exception {
ut.begin();
}
public void commitTrans()
throws Exception {
ut.rumit();
}
public void rollbackTrans()
throws Exception {
ut.rollback();
}
}
File: EmployeeServiceLocal.java
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
<T> T mergeEntity(T entity);
<T> T persistEntity(T entity);
<T> T refreshEntity(T entity);
void removeEntity(Object entity);
void beginTrans() throws Exception;
void commitTrans() throws Exception;
void rollbackTrans() throws Exception;
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
<T> T mergeEntity(T entity);
<T> T persistEntity(T entity);
<T> T refreshEntity(T entity);
void removeEntity(Object entity);
void beginTrans() throws Exception;
void commitTrans() throws Exception;
void rollbackTrans() throws Exception;
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new
// InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.beginTrans();
service.doAction();
service.rumitTrans();
}
}
Remove Annotation
File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
@Id
@Column(name = "EMP_ID")
private int id;
@Column(name = "COMM")
private String name;
@Column(name = "SAL")
private long salary;
public Employee() {
}
public Employee(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public String toString() {
return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
}
}
File: EmployeeService.java
import java.util.Collection;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
@Stateful
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
@PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED)
EntityManager em;
public EmployeeService() {
}
public Employee createEmployee(int id, String name, long salary, byte[] pic) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
emp = findEmployee(id);
System.out.println(emp);
em.flush();
return emp;
}
public void removeEmployee(int id) {
Employee emp = findEmployee(id);
if (emp != null) {
em.remove(emp);
}
}
public Employee raiseEmployeeSalary(int id, long raise) {
Employee emp = em.find(Employee.class, id);
if (emp != null) {
emp.setSalary(emp.getSalary() + raise);
}
return emp;
}
public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}
public Collection<Employee> findAllEmployees() {
Query query = em.createQuery("SELECT e FROM Employee e");
return (Collection<Employee>) query.getResultList();
}
public void doAction(){
}
@Remove
public void remove()
{
System.out.println("removed");
}
}
File: EmployeeServiceLocal.java
import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmployee(int id);
public Employee raiseEmployeeSalary(int id, long raise) ;
public Employee findEmployee(int id);
public Collection<Employee> findAllEmployees() ;
}
File: EmployeeServiceRemote.java
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
public void doAction();
public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmployee(int id);
public Employee raiseEmployeeSalary(int id, long raise) ;
public Employee findEmployee(int id);
public Collection<Employee> findAllEmployees() ;
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import java.util.Collection;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");
service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
Employee emp = service.findEmployee(158);
System.out.println(emp);
Collection<Employee> list = service.findAllEmployees();
System.out.println(list);
}
}
Remove a Stateful Session Bean
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: EmployeeBean.java
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
private HashMap<String,Integer> items = new HashMap<String,Integer>();
public void addItem(String item, int quantity) {
Integer orderQuantity = items.get(item);
if (orderQuantity == null) {
orderQuantity = 0;
}
orderQuantity += quantity;
items.put(item, orderQuantity);
}
public void removeItem(String item, int quantity) {
Integer orderQuantity = items.get(item);
if (orderQuantity == null) {
return;
}
orderQuantity -= quantity;
if (orderQuantity > 0) {
items.put(item, orderQuantity);
} else {
items.remove(item);
}
}
public Map<String, Integer> getItems() {
return items;
}
@Remove
public void checkout(int paymentId) {
System.out.println("checkout");
}
@Remove
public void cancel() {
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void addItem(String id, int quantity);
public void removeItem(String id, int quantity);
public Map<String, Integer> getItems();
public void checkout(int paymentId);
public void cancel();
}
File: EmployeeServiceRemote.java
import java.util.Map;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void addItem(String id, int quantity);
public void removeItem(String id, int quantity);
public Map<String, Integer> getItems();
public void checkout(int paymentId);
public void cancel();
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.addItem("item", 10);
service.removeItem("item", 1);
service.checkout(10);
//service.cancel();
}
}
Stateful Session Bean And Entity Manager
File: Employee.java
import static javax.persistence.FetchType.LAZY;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
@Id
@Column(name = "EMP_ID")
private int id;
@Column(name = "COMM")
private String name;
@Column(name = "SAL")
private long salary;
public Employee() {
}
public Employee(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public String toString() {
return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
}
}
File: EmployeeService.java
import java.util.Collection;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
@Stateful
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
@PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.TRANSACTION)
EntityManager em;
public EmployeeService() {
}
public Employee createEmployee(int id, String name, long salary, byte[] pic) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
emp = findEmployee(id);
System.out.println(emp);
em.flush();
return emp;
}
public void removeEmployee(int id) {
Employee emp = findEmployee(id);
if (emp != null) {
em.remove(emp);
}
}
public Employee raiseEmployeeSalary(int id, long raise) {
Employee emp = em.find(Employee.class, id);
if (emp != null) {
emp.setSalary(emp.getSalary() + raise);
}
return emp;
}
public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}
public Collection<Employee> findAllEmployees() {
Query query = em.createQuery("SELECT e FROM Employee e");
return (Collection<Employee>) query.getResultList();
}
public void doAction(){
}
}
File: EmployeeServiceLocal.java
import java.util.Collection;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmployee(int id);
public Employee raiseEmployeeSalary(int id, long raise) ;
public Employee findEmployee(int id);
public Collection<Employee> findAllEmployees() ;
}
File: EmployeeServiceRemote.java
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote{
public void doAction();
public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmployee(int id);
public Employee raiseEmployeeSalary(int id, long raise) ;
public Employee findEmployee(int id);
public Collection<Employee> findAllEmployees() ;
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import java.util.Collection;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");
service.createEmployee(158, "AAA", 45000, "asdf".getBytes());
service.createEmployee(159, "AAA", 45000, "asdf".getBytes());
Employee emp = service.findEmployee(158);
System.out.println(emp);
Collection<Employee> list = service.findAllEmployees();
System.out.println(list);
}
}
Stateful Session Bean And Shopping Cart
File: EmployeeBean.java
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
private HashMap<String,Integer> items = new HashMap<String,Integer>();
public void addItem(String item, int quantity) {
Integer orderQuantity = items.get(item);
if (orderQuantity == null) {
orderQuantity = 0;
}
orderQuantity += quantity;
items.put(item, orderQuantity);
}
public void removeItem(String item, int quantity) {
Integer orderQuantity = items.get(item);
if (orderQuantity == null) {
return;
}
orderQuantity -= quantity;
if (orderQuantity > 0) {
items.put(item, orderQuantity);
} else {
items.remove(item);
}
}
public Map<String, Integer> getItems() {
return items;
}
@Remove
public void checkout(int paymentId) {
System.out.println("checkout");
}
@Remove
public void cancel() {
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void addItem(String id, int quantity);
public void removeItem(String id, int quantity);
public Map<String, Integer> getItems();
public void checkout(int paymentId);
public void cancel();
}
File: EmployeeServiceRemote.java
import java.util.Map;
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void addItem(String id, int quantity);
public void removeItem(String id, int quantity);
public Map<String, Integer> getItems();
public void checkout(int paymentId);
public void cancel();
}
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.addItem("item", 10);
service.removeItem("item", 1);
service.checkout(10);
//service.cancel();
}
}
Stateful Session Bean Lifecycle: PostActivate
File: EmployeeBean.java
import javax.ejb.PostActivate;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
public void doAction(){
System.out.println("doAction");
}
@PostActivate
public void PostActivate() {
System.out.println("PostActivate");
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
}
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.doAction();
}
}
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
Stateful Session Bean Lifecycle: PostConstruct
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: EmployeeBean.java
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
public void doAction(){
System.out.println("doAction");
}
@PostConstruct
public void PostConstruct() {
System.out.println("PostConstruct");
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.doAction();
}
}
Stateful Session Bean Lifecycle: PreDestroy
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: EmployeeBean.java
import javax.annotation.PreDestroy;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
public void doAction(){
System.out.println("doAction");
}
@PreDestroy
public void shutdown() {
System.out.println("PreDestroy");
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.doAction();
}
}
Stateful Session Bean Lifecycle: PrePassivate
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: EmployeeBean.java
import javax.ejb.PrePassivate;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
public void doAction(){
System.out.println("doAction");
}
@PrePassivate
public void PrePassivate() {
System.out.println("PrePassivate");
}
}
File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;
@Local
public interface EmployeeServiceLocal {
public void doAction();
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.doAction();
}
}
Use Lifecycle Method To Manage Collection In Stateful Session Bean
File: EmployeeBean.java
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
public EmployeeBean() {
}
public ArrayList cartItems;
public void addWineItem(String wine) {
cartItems.add(wine);
}
public void removeWineItem(String wine) {
cartItems.remove(wine);
}
public void setCartItems(ArrayList cartItems) {
this.cartItems = cartItems;
}
public ArrayList getCartItems() {
return cartItems;
}
@PostConstruct
public void initialize() {
cartItems = new ArrayList();
}
@PreDestroy
public void exit() {
System.out.println("Saved items list into database");
}
@Remove
public void stopSession() {
// The method body can be empty.
System.out.println("From stopSession method with @Remove annotation");
}
public void doAction() {
System.out.println("Processing...");
}
}
File: EmployeeServiceLocal.java
import javax.ejb.Local;
import javax.ejb.Remote;
@Local
public interface EmployeeServiceLocal{
public void doAction();
}
File: EmployeeServiceRemote.java
import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
public void doAction();
}
File: Employee.java
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
@Id
@GeneratedValue
public int getId() {
return id;
}
@PostRemove
public void postRemove()
{
System.out.println("@PostRemove");
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first) {
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last) {
this.lastName = last;
}
}
File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
public static void main(String[] a) throws Exception {
EmployeeServiceRemote service = null;
// Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
// service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
service.doAction();
}
}