[jboss-svn-commits] JBoss Common SVN: r3806 - in arquillian/trunk/demo: src/main/java/com/acme and 5 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Nov 27 10:08:40 EST 2009


Author: aslak
Date: 2009-11-27 10:08:39 -0500 (Fri, 27 Nov 2009)
New Revision: 3806

Added:
   arquillian/trunk/demo/src/main/java/com/acme/ejb/MessageEcho.java
   arquillian/trunk/demo/src/main/java/com/acme/util/
   arquillian/trunk/demo/src/main/java/com/acme/util/jms/
   arquillian/trunk/demo/src/main/java/com/acme/util/jms/QueueRequestor.java
   arquillian/trunk/demo/src/test/java/com/acme/jms/
   arquillian/trunk/demo/src/test/java/com/acme/jms/InjectionTestCase.java
Modified:
   arquillian/trunk/demo/pom.xml
Log:
ARQ-17 Added JMS Example

Modified: arquillian/trunk/demo/pom.xml
===================================================================
--- arquillian/trunk/demo/pom.xml	2009-11-27 12:06:03 UTC (rev 3805)
+++ arquillian/trunk/demo/pom.xml	2009-11-27 15:08:39 UTC (rev 3806)
@@ -74,6 +74,12 @@
          <version>1.4.1</version>
          <scope>provided</scope>       
       </dependency>
+      <dependency>
+         <groupId>javax.jms</groupId> 
+         <artifactId>jms</artifactId>
+         <version>1.1</version>
+         <scope>provided</scope>       
+      </dependency>
 
       <dependency>
          <groupId>org.jboss.arquillian</groupId>

Added: arquillian/trunk/demo/src/main/java/com/acme/ejb/MessageEcho.java
===================================================================
--- arquillian/trunk/demo/src/main/java/com/acme/ejb/MessageEcho.java	                        (rev 0)
+++ arquillian/trunk/demo/src/main/java/com/acme/ejb/MessageEcho.java	2009-11-27 15:08:39 UTC (rev 3806)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.
+ */
+package com.acme.ejb;
+
+import javax.annotation.Resource;
+import javax.ejb.ActivationConfigProperty;
+import javax.ejb.MessageDriven;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+
+/**
+ * MessageEcho
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at MessageDriven(activationConfig = {
+      @ActivationConfigProperty( propertyName = "destination", propertyValue = "queue/DLQ"),
+      @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue")
+})
+public class MessageEcho implements MessageListener
+{
+
+   @Resource(mappedName = "java:/ConnectionFactory")
+   private ConnectionFactory factory;
+   
+   @Override
+   public void onMessage(Message msg)
+   {
+      try 
+      {
+         System.out.println("received " + msg.getJMSMessageID());
+         
+         Connection connection = factory.createConnection();
+         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageProducer producer = session.createProducer(msg.getJMSReplyTo());
+         producer.send(msg);
+         producer.close();
+         session.close();
+         connection.close();
+      }
+      catch (Exception e) 
+      {
+         throw new RuntimeException("Could not reply to message", e);
+      }
+   }
+}

Added: arquillian/trunk/demo/src/main/java/com/acme/util/jms/QueueRequestor.java
===================================================================
--- arquillian/trunk/demo/src/main/java/com/acme/util/jms/QueueRequestor.java	                        (rev 0)
+++ arquillian/trunk/demo/src/main/java/com/acme/util/jms/QueueRequestor.java	2009-11-27 15:08:39 UTC (rev 3806)
@@ -0,0 +1,101 @@
+package com.acme.util.jms;
+
+import javax.jms.InvalidDestinationException;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.Queue;
+import javax.jms.QueueReceiver;
+import javax.jms.QueueSender;
+import javax.jms.QueueSession;
+import javax.jms.TemporaryQueue;
+
+public class QueueRequestor {
+
+   QueueSession session; // The queue session the queue belongs to.
+   Queue queue; // The queue to perform the request/reply on.
+   TemporaryQueue tempQueue;
+   QueueSender sender;
+   QueueReceiver receiver;
+
+   /**
+    * Constructor for the <CODE>QueueRequestor</CODE> class.
+    * 
+    * <P>
+    * This implementation assumes the session parameter to be non-transacted,
+    * with a delivery mode of either <CODE>AUTO_ACKNOWLEDGE</CODE> or
+    * <CODE>DUPS_OK_ACKNOWLEDGE</CODE>.
+    * 
+    * @param session
+    *           the <CODE>QueueSession</CODE> the queue belongs to
+    * @param queue
+    *           the queue to perform the request/reply call on
+    * 
+    * @exception JMSException
+    *               if the JMS provider fails to create the
+    *               <CODE>QueueRequestor</CODE> due to some internal error.
+    * @exception InvalidDestinationException
+    *               if an invalid queue is specified.
+    */
+
+   public QueueRequestor(QueueSession session, Queue queue) throws JMSException
+   {
+      this.session = session;
+      this.queue = queue;
+      tempQueue = session.createTemporaryQueue();
+      sender = session.createSender(queue);
+      receiver = session.createReceiver(tempQueue);
+   }
+
+   /**
+    * Sends a request and waits for a reply. The temporary queue is used for the
+    * <CODE>JMSReplyTo</CODE> destination, and only one reply per request is
+    * expected.
+    * 
+    * @param message
+    *           the message to send
+    * 
+    * @return the reply message
+    * 
+    * @exception JMSException
+    *               if the JMS provider fails to complete the request due to
+    *               some internal error.
+    */
+
+   public Message request(Message message) throws JMSException
+   {
+      return request(message, 0);
+   }
+
+   public Message request(Message message, int wait) throws JMSException
+   {
+      message.setJMSReplyTo(tempQueue);
+      sender.send(message);
+      return (receiver.receive(wait));
+   }
+
+   /**
+    * Closes the <CODE>QueueRequestor</CODE> and its session.
+    * 
+    * <P>
+    * Since a provider may allocate some resources on behalf of a
+    * <CODE>QueueRequestor</CODE> outside the Java virtual machine, clients
+    * should close them when they are not needed. Relying on garbage collection
+    * to eventually reclaim these resources may not be timely enough.
+    * 
+    * <P>
+    * Note that this method closes the <CODE>QueueSession</CODE> object passed
+    * to the <CODE>QueueRequestor</CODE> constructor.
+    * 
+    * @exception JMSException
+    *               if the JMS provider fails to close the
+    *               <CODE>QueueRequestor</CODE> due to some internal error.
+    */
+
+   public void close() throws JMSException
+   {
+
+      // publisher and consumer created by constructor are implicitly closed.
+      session.close();
+      tempQueue.delete();
+   }
+ }
\ No newline at end of file

Added: arquillian/trunk/demo/src/test/java/com/acme/jms/InjectionTestCase.java
===================================================================
--- arquillian/trunk/demo/src/test/java/com/acme/jms/InjectionTestCase.java	                        (rev 0)
+++ arquillian/trunk/demo/src/test/java/com/acme/jms/InjectionTestCase.java	2009-11-27 15:08:39 UTC (rev 3806)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed 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.
+ */
+package com.acme.jms;
+
+import javax.annotation.Resource;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Message;
+import javax.jms.Queue;
+import javax.jms.QueueSession;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.acme.ejb.MessageEcho;
+import com.acme.util.jms.QueueRequestor;
+
+/**
+ * JmsTest
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+ at RunWith(Arquillian.class)
+public class InjectionTestCase
+{
+   @Deployment
+   public static JavaArchive createDeployment() {
+      return Archives.create("test.jar", JavaArchive.class)
+            .addClasses(
+                  MessageEcho.class, 
+                  QueueRequestor.class);
+   }
+   
+   @Resource(mappedName = "/queue/DLQ") 
+   private Queue dlq;
+   
+   @Resource(mappedName = "/ConnectionFactory") 
+   private ConnectionFactory factory;
+   
+   @Test
+   public void shouldBeAbleToGreet() throws Exception {
+      
+      String messageBody = "ping";
+      
+      Connection connection = factory.createConnection();
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      QueueRequestor requestor = new QueueRequestor((QueueSession)session, dlq);
+
+      connection.start();
+      
+      Message request = session.createTextMessage(messageBody);
+      Message response = requestor.request(request, 5000);
+      
+      Assert.assertEquals(
+            "Should have responded with same message",
+            messageBody,
+            ((TextMessage)response).getText());
+   }
+}



More information about the jboss-svn-commits mailing list