You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2008/05/21 04:05:15 UTC

svn commit: r658530 - in /openejb/trunk/openejb3/examples: ./ simple-mdb/ simple-mdb/src/ simple-mdb/src/main/ simple-mdb/src/main/java/ simple-mdb/src/main/java/org/ simple-mdb/src/main/java/org/superbiz/ simple-mdb/src/main/java/org/superbiz/mdb/ sim...

Author: dblevins
Date: Tue May 20 19:05:14 2008
New Revision: 658530

URL: http://svn.apache.org/viewvc?rev=658530&view=rev
Log:
OPENEJB-348: Example: Minimal MessageDriven Bean via @MessageDriven

Added:
    openejb/trunk/openejb3/examples/simple-mdb/
    openejb/trunk/openejb3/examples/simple-mdb/build.xml
    openejb/trunk/openejb3/examples/simple-mdb/pom.xml
    openejb/trunk/openejb3/examples/simple-mdb/src/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/ChatBean.java
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientBean.java
    openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientLocal.java
    openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/
    openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/ejb-jar.xml
    openejb/trunk/openejb3/examples/simple-mdb/src/test/
    openejb/trunk/openejb3/examples/simple-mdb/src/test/java/
    openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/
    openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/
    openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/
    openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/ChatBeanTest.java
Modified:
    openejb/trunk/openejb3/examples/pom.xml

Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=658530&r1=658529&r2=658530&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Tue May 20 19:05:14 2008
@@ -32,6 +32,7 @@
   <modules>
     <module>simple-stateful</module>
     <module>simple-stateless</module>
+    <module>simple-mdb</module>
     <module>simple-webservice</module>
     <module>component-interfaces</module>
     <module>injection-of-ejbs</module>

Added: openejb/trunk/openejb3/examples/simple-mdb/build.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/build.xml?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/build.xml (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/build.xml Tue May 20 19:05:14 2008
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- $Rev: 635408 $ $Date: 2008-03-09 17:48:40 -0700 (Sun, 09 Mar 2008) $ -->
+
+<project name="MyProject" default="dist" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+  <!-- ===============================================================
+
+  HOW TO RUN
+
+    Download http://www.apache.org/dist/maven/binaries/maven-ant-tasks-2.0.7.jar
+    Then execute ant as follows:
+
+    ant -lib maven-ant-tasks-2.0.7.jar
+
+  NOTE
+
+    You do NOT need maven-ant-tasks-2.0.7.jar to use OpenEJB for embedded EJB
+    testing with Ant.  It is simply used in this example to make the build.xml
+    a bit simpler.  As long as OpenEJB and it's required libraries are in the
+    <junit> classpath, the tests will run with OpenEJB embedded.
+
+  ================================================================= -->
+
+  <artifact:remoteRepository id="apache.snapshot.repository" url="http://people.apache.org/repo/m2-snapshot-repository/" />
+  <artifact:remoteRepository id="m2.repository" url="http://repo1.maven.org/maven2/" />
+
+  <!-- Build Classpath -->
+  <artifact:dependencies pathId="classpath.main">
+    <dependency groupId="org.apache.openejb" artifactId="javaee-api" version="5.0-1"/>
+  </artifact:dependencies>
+
+  <!-- Test Build Classpath -->
+  <artifact:dependencies pathId="classpath.test.build">
+    <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+  </artifact:dependencies>
+
+  <!-- Test Run Classpath -->
+  <artifact:dependencies pathId="classpath.test.run">
+    <remoteRepository refid="apache.snapshot.repository" />
+    <remoteRepository refid="m2.repository" />
+
+    <dependency groupId="org.apache.openejb" artifactId="openejb-core" version="3.0-SNAPSHOT"/>
+    <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+  </artifact:dependencies>
+
+  <!-- Properties -->
+
+  <property name="src.main.java" location="src/main/java"/>
+  <property name="src.main.resources" location="src/main/resources"/>
+  <property name="src.test.java" location="src/test/java"/>
+  <property name="build.main" location="target/classes"/>
+  <property name="build.test" location="target/test-classes"/>
+  <property name="test.reports" location="target/test-reports"/>
+  <property name="dist" location="target"/>
+
+
+  <target name="init">
+    <mkdir dir="${build.main}"/>
+    <mkdir dir="${build.test}"/>
+    <mkdir dir="${test.reports}"/>
+  </target>
+
+  <target name="compile" depends="init">
+
+    <javac srcdir="${src.main.java}" destdir="${build.main}">
+      <classpath refid="classpath.main" />
+    </javac>
+    <copy todir="${build.main}">
+      <fileset dir="${src.main.resources}"/>
+    </copy>
+
+    <javac srcdir="${src.test.java}" destdir="${build.test}">
+      <classpath location="${build.main}"/>
+      <classpath refid="classpath.main"/>
+      <classpath refid="classpath.test.build"/>
+    </javac>
+  </target>
+
+  <target name="test" depends="compile">
+    <junit fork="yes" printsummary="yes">
+      <classpath location="${build.main}"/>
+      <classpath location="${build.test}"/>
+      <classpath refid="classpath.main"/>
+      <classpath refid="classpath.test.build"/>
+      <classpath refid="classpath.test.run"/>
+
+      <formatter type="plain"/>
+
+      <batchtest fork="yes" todir="${test.reports}">
+        <fileset dir="${src.test.java}">
+          <include name="**/*Test.java"/>
+        </fileset>
+      </batchtest>
+    </junit>
+  </target>
+
+  <target name="dist" depends="test">
+    <jar jarfile="${dist}/myproject-1.0.jar" basedir="${build.main}"/>
+  </target>
+
+</project>

Added: openejb/trunk/openejb3/examples/simple-mdb/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/pom.xml?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/pom.xml (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/pom.xml Tue May 20 19:05:14 2008
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- $Rev: 638272 $ $Date: 2008-03-18 01:59:59 -0700 (Tue, 18 Mar 2008) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.superbiz</groupId>
+  <artifactId>simple-mdb</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Simple MDB Example</name>
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>http://people.apache.org/repo/m2-snapshot-repository/</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>5.0-1</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.1</version>
+      <scope>test</scope>
+    </dependency>
+
+    <!--
+    The <scope>test</scope> guarantees that non of your runtime
+    code is dependent on any OpenEJB classes.
+    -->
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>3.1-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
+</project>
\ No newline at end of file

Added: openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/ChatBean.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/ChatBean.java?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/ChatBean.java (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/ChatBean.java Tue May 20 19:05:14 2008
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+//START SNIPPET: code
+package org.superbiz.mdb;
+
+import javax.jms.MessageListener;
+import javax.jms.Message;
+import javax.jms.ConnectionFactory;
+import javax.jms.Connection;
+import javax.jms.Session;
+import javax.jms.MessageProducer;
+import javax.jms.DeliveryMode;
+import javax.jms.TextMessage;
+import javax.jms.Queue;
+import javax.jms.JMSException;
+import javax.annotation.Resource;
+import javax.ejb.MessageDriven;
+
+@MessageDriven
+public class ChatBean implements MessageListener {
+
+    @Resource
+    private ConnectionFactory connectionFactory;
+
+    @Resource(name="AnswerQueue")
+    private Queue answerQueue;
+
+    public void onMessage(Message message) {
+        try {
+            TextMessage textMessage = (TextMessage) message;
+            String question = textMessage.getText();
+
+            if (question.equals("Hello World!")){
+                respond("Hello, Test Case!");
+            } else if (question.equals("How are you?")){
+                respond("I'm doing well.");
+            } else if (question.equals("Still spinning?")){
+                respond("Once every day, as usual.");
+            }
+        } catch (JMSException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    private void respond(String text) throws JMSException {
+
+        Connection connection = null;
+        Session session = null;
+
+        try {
+            connection = connectionFactory.createConnection();
+            connection.start();
+
+            // Create a Session
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+            // Create a MessageProducer from the Session to the Topic or Queue
+            MessageProducer producer = session.createProducer(answerQueue);
+            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+            // Create a message
+            TextMessage message = session.createTextMessage(text);
+
+            // Tell the producer to send the message
+            producer.send(message);
+        } finally {
+            // Clean up
+            if (session != null) session.close();
+            if (connection != null) connection.close();
+        }
+    }
+}
+//END SNIPPET: code

Added: openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientBean.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientBean.java?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientBean.java (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientBean.java Tue May 20 19:05:14 2008
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+//START SNIPPET: code
+package org.superbiz.mdb;
+
+import javax.annotation.Resource;
+import javax.ejb.Stateless;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.DeliveryMode;
+import javax.jms.JMSException;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+@Stateless
+public class MessagingClientBean implements MessagingClientLocal {
+
+    @Resource
+    private ConnectionFactory connectionFactory;
+
+    @Resource(name = "ChatBean")
+    private Queue questionQueue;
+
+    @Resource(name="AnswerQueue")
+    private Queue answerQueue;
+
+
+    public void sendMessage(String text) throws JMSException {
+
+        Connection connection = null;
+        Session session = null;
+
+        try {
+            connection = connectionFactory.createConnection();
+            connection.start();
+
+            // Create a Session
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+            // Create a MessageProducer from the Session to the Topic or Queue
+            MessageProducer producer = session.createProducer(questionQueue);
+            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+            // Create a message
+            TextMessage message = session.createTextMessage(text);
+
+            // Tell the producer to send the message
+            producer.send(message);
+        } finally {
+            // Clean up
+            if (session != null) session.close();
+            if (connection != null) connection.close();
+        }
+    }
+
+    public String receiveMessage() throws JMSException {
+
+        Connection connection = null;
+        Session session = null;
+        MessageConsumer consumer = null;
+        try {
+            connection = connectionFactory.createConnection();
+            connection.start();
+
+            // Create a Session
+            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+            // Create a MessageConsumer from the Session to the Topic or Queue
+            consumer = session.createConsumer(answerQueue);
+
+            // Wait for a message
+            TextMessage message = (TextMessage) consumer.receive(1000);
+
+            return message.getText();
+        } finally {
+            if (consumer != null) consumer.close();
+            if (session != null) session.close();
+            if (connection != null) connection.close();
+        }
+
+    }
+}
+//END SNIPPET: code

Added: openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientLocal.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientLocal.java?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientLocal.java (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/src/main/java/org/superbiz/mdb/MessagingClientLocal.java Tue May 20 19:05:14 2008
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+//START SNIPPET: code
+package org.superbiz.mdb;
+
+import javax.jms.JMSException;
+
+public interface MessagingClientLocal {
+
+    public void sendMessage(String text) throws JMSException;
+
+    public String receiveMessage() throws JMSException;
+}
+//END SNIPPET: code

Added: openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/ejb-jar.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/ejb-jar.xml?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/ejb-jar.xml (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/src/main/resources/META-INF/ejb-jar.xml Tue May 20 19:05:14 2008
@@ -0,0 +1 @@
+<ejb-jar/>

Added: openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/ChatBeanTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/ChatBeanTest.java?rev=658530&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/ChatBeanTest.java (added)
+++ openejb/trunk/openejb3/examples/simple-mdb/src/test/java/org/superbiz/mdb/ChatBeanTest.java Tue May 20 19:05:14 2008
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+//START SNIPPET: code
+package org.superbiz.mdb;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+public class ChatBeanTest extends TestCase {
+
+    public void test() throws Exception {
+        Properties p = new Properties();
+        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
+        InitialContext context = new InitialContext(p);
+
+        MessagingClientLocal bean = (MessagingClientLocal) context.lookup("MessagingClientBeanLocal");
+
+        bean.sendMessage("Hello World!");
+
+        assertEquals(bean.receiveMessage(), "Hello, Test Case!");
+
+        bean.sendMessage("How are you?");
+
+        assertEquals(bean.receiveMessage(), "I'm doing well.");
+
+        bean.sendMessage("Still spinning?");
+
+        assertEquals(bean.receiveMessage(), "Once every day, as usual.");
+    }
+}
+//END SNIPPET: code