diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful')
6 files changed, 227 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/Count.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/Count.java new file mode 100644 index 0000000..21be867 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/Count.java @@ -0,0 +1,24 @@ +package examples.session.stateful;
+
+/**
+ * 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/CountBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountBean.java new file mode 100644 index 0000000..6afd4a3 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountBean.java @@ -0,0 +1,53 @@ +package examples.session.stateful;
+
+import javax.ejb.*;
+
+/**
+ * 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:
+ * <ul>
+ * <li>this is a Stateful Session Bean
+ * <li>the bean's remote business interface is <code>Count</code>
+ * <li>any lifecycle callbacks go to the class <code>CountCallbacks</code>
+ * </ul>
+ */
+
+@Stateful
+@Remote(Count.class)
+@Interceptors(CountCallbacks.class)
+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()");
+ }
+
+ /**
+ * The remove method is annotated so that the container knows
+ * it can remove the bean after this method returns.
+ */
+ @Remove
+ 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/CountCallbacks.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountCallbacks.java new file mode 100644 index 0000000..d6f6aa1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountCallbacks.java @@ -0,0 +1,53 @@ +package examples.session.stateful;
+
+import javax.ejb.InvocationContext;
+import javax.ejb.PostActivate;
+import javax.ejb.PrePassivate;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+
+/**
+ * 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
+ */
+ @PostConstruct
+ public void construct(InvocationContext ctx) throws Exception {
+ System.out.println("cb:construct() ");
+ ctx.proceed();
+ }
+
+ /**
+ * Called by the container after activation
+ */
+ @PostActivate
+ public void activate(InvocationContext ctx) throws Exception {
+ System.out.println("cb:activate()");
+ ctx.proceed();
+ }
+
+ /**
+ * Called by the container before passivation
+ */
+ @PrePassivate
+ public void passivate(InvocationContext ctx) throws Exception {
+ System.out.println("cb:passivate()");
+ ctx.proceed();
+ }
+
+ /**
+ * Called by the container before destruction
+ */
+ @PreDestroy
+ public void destroy(InvocationContext ctx) throws Exception {
+ System.out.println("cb:destroy()");
+ ctx.proceed();
+ }
+
+}
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountClient.java new file mode 100644 index 0000000..3536705 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/CountClient.java @@ -0,0 +1,63 @@ +package examples.session.stateful;
+
+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(System.getProperties());
+
+ /* 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(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/META-INF/ejb-jar.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/META-INF/ejb-jar.xml new file mode 100644 index 0000000..9503e74 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/META-INF/ejb-jar.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8" ?>
+ <ejb-jar>
+ <enterprise-beans>
+ </enterprise-beans>
+</ejb-jar>
\ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/build.xml new file mode 100644 index 0000000..d958085 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/session/stateful/build.xml @@ -0,0 +1,29 @@ +<?xml version="1.0"?> +<!DOCTYPE project [ <!ENTITY include SYSTEM "../../../../etc/common.xml"> ]> + +<project name="ejb3-examples-session-stateful" default="deploy" + basedir="../../../.."> + + <!-- basic settings --> + <property name="src.dir" value="${basedir}/src"/> + <property name="build.dir" value="${basedir}/build"/> + <property name="build.classes.dir" value="${build.dir}/classes"/> + <property name="appname" value="StatefulSession"/> + <property name="client.class" value="examples.session.stateful.CountClient"/> + <property name="app.pkg" value="examples/session/stateful"/> + <property name="package" value="${app.pkg}"/> + <property name="pack.dir" value="${src.dir}/${app.pkg}"/> + <property name="jar.pkg" value="examples/session/stateful"/> + + <!-- Include common.xml --> + &include; + + <target name="jar" depends="compile_common, create_ejbjar_common"/> + + <target name="deploy" depends="jar"> + <copy file="${assemble.ejbjar}/${ejbjar}" + todir="${deploy.dir}"/> + </target> + +</project> + |
