PageRenderTime 81ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/smoke/src/test/java/org/jboss/as/test/smoke/messaging/client/jms/JmsClientTestCase.java

#
Java | 171 lines | 120 code | 22 blank | 29 comment | 9 complexity | bb74555b51a3713700099cefe15e9540 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.smoke.messaging.client.jms;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.concurrent.CountDownLatch;
  27. import java.util.concurrent.TimeUnit;
  28. import javax.jms.JMSException;
  29. import javax.jms.Message;
  30. import javax.jms.MessageListener;
  31. import javax.jms.Queue;
  32. import javax.jms.QueueConnection;
  33. import javax.jms.QueueConnectionFactory;
  34. import javax.jms.QueueReceiver;
  35. import javax.jms.QueueSender;
  36. import javax.jms.QueueSession;
  37. import javax.jms.TextMessage;
  38. import javax.naming.Context;
  39. import junit.framework.Assert;
  40. import org.jboss.arquillian.container.test.api.RunAsClient;
  41. import org.jboss.arquillian.junit.Arquillian;
  42. import org.jboss.as.arquillian.api.ContainerResource;
  43. import org.jboss.as.arquillian.container.ManagementClient;
  44. import org.jboss.as.controller.client.ModelControllerClient;
  45. import org.jboss.as.controller.client.OperationBuilder;
  46. import org.jboss.dmr.ModelNode;
  47. import org.junit.Test;
  48. import org.junit.runner.RunWith;
  49. /**
  50. * Demo using the AS management API to create and destroy a JMS queue.
  51. *
  52. * @author Emanuel Muckenhuber
  53. */
  54. @RunWith(Arquillian.class)
  55. @RunAsClient
  56. public class JmsClientTestCase {
  57. private static final String QUEUE_NAME = "createdTestQueue";
  58. private static final String EXPORTED_QUEUE_NAME = "java:jboss/exported/createdTestQueue";
  59. @ContainerResource
  60. private Context remoteContext;
  61. @ContainerResource
  62. private ManagementClient managementClient;
  63. @Test
  64. public void testMessagingClient() throws Exception {
  65. QueueConnection conn = null;
  66. QueueSession session = null;
  67. ModelControllerClient client = managementClient.getControllerClient();
  68. boolean actionsApplied = false;
  69. try {
  70. // Create the queue using the management API
  71. ModelNode op = new ModelNode();
  72. op.get("operation").set("add");
  73. op.get("address").add("subsystem", "messaging");
  74. op.get("address").add("hornetq-server", "default");
  75. op.get("address").add("jms-queue", QUEUE_NAME);
  76. op.get("entries").add(EXPORTED_QUEUE_NAME);
  77. applyUpdate(op, client);
  78. actionsApplied = true;
  79. QueueConnectionFactory qcf = (QueueConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
  80. Queue queue = (Queue) remoteContext.lookup(QUEUE_NAME);
  81. conn = qcf.createQueueConnection("guest", "guest");
  82. conn.start();
  83. session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
  84. final CountDownLatch latch = new CountDownLatch(10);
  85. final List<String> result = new ArrayList<String>();
  86. // Set the async listener
  87. QueueReceiver recv = session.createReceiver(queue);
  88. recv.setMessageListener(new MessageListener() {
  89. @Override
  90. public void onMessage(Message message) {
  91. TextMessage msg = (TextMessage)message;
  92. try {
  93. result.add(msg.getText());
  94. latch.countDown();
  95. } catch (JMSException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. });
  100. QueueSender sender = session.createSender(queue);
  101. for (int i = 0 ; i < 10 ; i++) {
  102. String s = "Test" + i;
  103. TextMessage msg = session.createTextMessage(s);
  104. sender.send(msg);
  105. }
  106. Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
  107. Assert.assertEquals(10, result.size());
  108. for (int i = 0 ; i < result.size() ; i++) {
  109. Assert.assertEquals("Test" + i, result.get(i));
  110. }
  111. } finally {
  112. try {
  113. conn.stop();
  114. } catch (Exception ignore) {
  115. }
  116. try {
  117. session.close();
  118. } catch (Exception ignore) {
  119. }
  120. try {
  121. conn.close();
  122. } catch (Exception ignore) {
  123. }
  124. if(actionsApplied) {
  125. // Remove the queue using the management API
  126. ModelNode op = new ModelNode();
  127. op.get("operation").set("remove");
  128. op.get("address").add("subsystem", "messaging");
  129. op.get("address").add("hornetq-server", "default");
  130. op.get("address").add("jms-queue", QUEUE_NAME);
  131. applyUpdate(op, client);
  132. }
  133. }
  134. }
  135. static void applyUpdate(ModelNode update, final ModelControllerClient client) throws IOException {
  136. ModelNode result = client.execute(new OperationBuilder(update).build());
  137. if (result.hasDefined("outcome") && "success".equals(result.get("outcome").asString())) {
  138. if (result.hasDefined("result")) {
  139. System.out.println(result.get("result"));
  140. }
  141. }
  142. else if (result.hasDefined("failure-description")){
  143. throw new RuntimeException(result.get("failure-description").toString());
  144. }
  145. else {
  146. throw new RuntimeException("Operation not successful; outcome = " + result.get("outcome"));
  147. }
  148. }
  149. }