Stateful Session Bean And Shopping Cart : Stateful Session Bean « EJB3 « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » EJB3 » Stateful Session BeanScreenshots 
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[] athrows Exception {

    EmployeeServiceRemote service = null;

    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemotenew InitialContext().lookup("EmployeeBean/remote");

    service.addItem("item"10);
    service.removeItem("item"1);
    service.checkout(10);
    //service.cancel();

  }

}




           
       
EJB-StatefulSessionBeanAndShoppingCart.zip( 4,489 k)
Related examples in the same category
1.Stateful Session Bean Lifecycle: PrePassivate
2.Stateful Session Bean Lifecycle: PreDestroy
3.Stateful Session Bean Lifecycle: PostConstruct
4.Stateful Session Bean Lifecycle: PostActivate
5.Stateful Session Bean And Entity Manager
6.Use Lifecycle Method To Manage Collection In Stateful Session Bean
7.EJB Tutorial from JBoss: stateful session bean
8.EJB Tutorial from JBoss: stateful session bean deployment descriptor
9.Remove a Stateful Session Bean
10.Remove Annotation
11.Ejb With Generic Method
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.