From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../src/examples/session/stateful_dd/Count.java | 24 +++++++++ .../examples/session/stateful_dd/CountBean.java | 47 ++++++++++++++++ .../session/stateful_dd/CountCallbacks.java | 43 +++++++++++++++ .../examples/session/stateful_dd/CountClient.java | 63 ++++++++++++++++++++++ .../session/stateful_dd/META-INF/ejb-jar.xml | 35 ++++++++++++ .../src/examples/session/stateful_dd/build.xml | 32 +++++++++++ 6 files changed, 244 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/Count.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountCallbacks.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/META-INF/ejb-jar.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/build.xml (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd') diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/Count.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/Count.java new file mode 100644 index 0000000..f20ae5e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/Count.java @@ -0,0 +1,24 @@ +package examples.session.stateful_dd; + +/** + * The business interface - a plain Java interface with only business methods. + */ +public interface Count { + + /** + * Increments the counter by 1 + */ + public int count(); + + /** + * Sets the counter to val + * + * @param val + */ + public void set(int val); + + /** + * removes the counter + */ + public void remove(); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountBean.java new file mode 100644 index 0000000..0a2ab2c --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountBean.java @@ -0,0 +1,47 @@ +package examples.session.stateful_dd; + +import java.io.Serializable; + +/** + * A Stateful Session Bean Class that shows the basics of + * how to write a stateful session bean. + * + * This Bean is initialized to some integer value. It has a + * business method which increments the value. + * + * The annotations below declare that: + * + */ + +public class CountBean implements Count { + + /** The current counter is our conversational state. */ + private int val; + + /** + * The count() business method. + */ + public int count() { + System.out.println("count()"); + return ++val; + } + + /** + * The set() business method. + */ + public void set(int val) { + this.val = val; + System.out.println("set()"); + } + + /** + */ + public void remove() { + System.out.println("remove()"); + } + +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountCallbacks.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountCallbacks.java new file mode 100644 index 0000000..f2ed8b0 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountCallbacks.java @@ -0,0 +1,43 @@ +package examples.session.stateful_dd; + +import javax.ejb.InvocationContext; + +/** + * This class is a lifecycle callback interceptor for the Count bean. + * The callback methods simply print a message when invoked by + * the container. + */ +public class CountCallbacks { + + public CountCallbacks() {} + + /** + * Called by the container after construction + */ + public void construct(InvocationContext ctx) { + System.out.println("cb:construct()"); + } + + /** + * Called by the container after activation + */ + public void activate(InvocationContext ctx) { + System.out.println("cb:activate()"); + } + + /** + * Called by the container before passivation + */ + public void passivate(InvocationContext ctx) { + System.out.println("cb:passivate()"); + } + + /** + * Called by the container before destruction + */ + + public void destroy(InvocationContext ctx) { + System.out.println("cb:destroy()"); + } + +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountClient.java new file mode 100644 index 0000000..46fb0cc --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/CountClient.java @@ -0,0 +1,63 @@ +package examples.session.stateful_dd; + +import javax.naming.*; + +/** + * This class is a simple client for a stateful session bean. + * + * To illustrate how passivation works, configure your EJB server + * to allow only 2 stateful session beans in memory. (Consult your + * vendor documentation for details on how to do this.) We create + * 3 beans in this example to see how and when beans are passivated. + */ +public class CountClient { + + public static final int noOfClients = 3; + + public static void main(String[] args) { + try { + /* Get a reference to the bean */ + Context ctx = new InitialContext(); + + /* An array to hold the Count beans */ + Count count[] = new Count[noOfClients]; + int countVal = 0; + + /* Create and count() on each member of array */ + System.out.println("Instantiating beans..."); + for (int i = 0; i < noOfClients; i++) { + //count[i] = (Count) ctx.lookup("StatefulCount" ); + count[i] = (Count) ctx.lookup(Count.class.getName()); + /* initialize each bean to the current count value */ + count[i].set(countVal); + + /* Add 1 and print */ + countVal = count[i].count(); + System.out.println(countVal); + + /* Sleep for 1/2 second */ + Thread.sleep(100); + } + + /* + * Let's call count() on each bean to make sure the + * beans were passivated and activated properly. + */ + System.out.println("Calling count() on beans..."); + for (int i = 0; i < noOfClients; i++) { + + /* Add 1 and print */ + countVal = count[i].count(); + System.out.println(countVal); + + /* call remove to let the container dispose of the bean */ + count[i].remove(); + + /* Sleep for 1/2 second */ + Thread.sleep(50); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/META-INF/ejb-jar.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/META-INF/ejb-jar.xml new file mode 100644 index 0000000..a483584 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/META-INF/ejb-jar.xml @@ -0,0 +1,35 @@ + + + Stateful Session Bean Example + Stateful Session Bean Example + + + CountBean + examples.session.stateful_dd.Count + examples.session.stateful_dd.CountBean + Stateful + Container + + + + + + examples.session.stateful_dd.CountCallbacks + + construct + activate + passivate + + + + + + + CountBean + examples.session.stateful_dd.CountCallbacks + + + + \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/build.xml new file mode 100644 index 0000000..8c9181a --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful_dd/build.xml @@ -0,0 +1,32 @@ + + ]> + + + + + + + + + + + + + + + + &include; + + + + + + + + + + + + -- cgit v1.2.3