This essay developed out of conversations I've had with several other programmers about why Java smelled suspicious. It's not a critique of Java!
Sycorax - complete tutorials
Programming Tutorials
" Java provides the industry - software companies and customer alike , an opportunity to create a true open computing environment where software is portable,
and customers benefit from increase competition. "
Java and the Future
December 1, 2008-LEJB 3.1: EJB New and Improved!
The EJB 3.0 specification was a huge improvement from what you were used to in the early versions of EJB. Available as an early draft, EJB 3.1 has many more features and is even easier to use.
December 1, 2008-Should Java Assert that Network I/O Can't Occur on the UI Thread?
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it's still done.
Register now to recieve special alert and latest technology news!
EJB3.0 Stateful Session Beans
Life Cycle of a Stateful Session Bean
The life cycle of a stateful session bean has three states: Does Not Exist, Method-Ready, and Passivated.
Does Not Exist State
A stateful bean instance in the Does Not Exist state has not been instantiated yet. It doesn't exist in the system's memory.
Method-Ready State
The Method-Ready state is the state in which the bean instance can service requests from its clients. This section explores the instance's transition into and out of the Method-Ready state.Passivated State
During the lifetime of a stateful session bean, there may be periods of inactivity when the bean instance is not servicing methods from the client. To conserve resources, the container can passivate the bean instance by preserving its conversational state and evicting the bean instance from memory.
Remote Interface: Cart.java
package sycorax.tutorial.stateful;
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface Cart {
public void addItem(String item);
public void removeItem(String item);
public Collection getItems();
}
Stateful Session Bean: CartBean.java
package sycorax.tutorial.stateful.bean;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements Cart {
private ArrayList items;
@PostConstruct
public void initialize() {
items = new ArrayList();
}
public void addItem(String item) {
items.add(item);
}
public void removeItem(String item) {
items.remove(item);
}
public Collection getItems() {
return items;
}
}
EJB Client: CartClient.java
package sycorax.tutorial.stateful;
import java.util.Collection;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class CartClient {
public static void main(String [] args) throws NamingException {
try {
final Context context = getInitialContext();
Cart cart = (Cart)context.lookup("CartBean");
System.out.println("Adding items to cart");
cart.addItem("Pizza");
cart.addItem("Pasta");
cart.addItem("Noodles");
cart.addItem("Bread");
cart.addItem("Butter");
System.out.println("Listing cart contents");
Collection items = cart.getItems();
for (Iterator i = items.iterator(); i.hasNext();) {
String item = (String) i.next();
System.out.println(" " + item);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Context getInitialContext() throws NamingException {
return new InitialContext();
}
}
Compile all the programs, start the server and execute CartClient. You will see following Output:
Adding items to cart
Listing cart contents
Pasta
Noodles
Bread