PageRenderTime 38ms CodeModel.GetById 24ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

/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
 23package org.jboss.as.test.smoke.messaging.client.jms;
 24
 25import java.io.IOException;
 26import java.util.ArrayList;
 27import java.util.List;
 28import java.util.concurrent.CountDownLatch;
 29import java.util.concurrent.TimeUnit;
 30
 31import javax.jms.JMSException;
 32import javax.jms.Message;
 33import javax.jms.MessageListener;
 34import javax.jms.Queue;
 35import javax.jms.QueueConnection;
 36import javax.jms.QueueConnectionFactory;
 37import javax.jms.QueueReceiver;
 38import javax.jms.QueueSender;
 39import javax.jms.QueueSession;
 40import javax.jms.TextMessage;
 41import javax.naming.Context;
 42
 43import junit.framework.Assert;
 44import org.jboss.arquillian.container.test.api.RunAsClient;
 45import org.jboss.arquillian.junit.Arquillian;
 46import org.jboss.as.arquillian.api.ContainerResource;
 47import org.jboss.as.arquillian.container.ManagementClient;
 48import org.jboss.as.controller.client.ModelControllerClient;
 49import org.jboss.as.controller.client.OperationBuilder;
 50import org.jboss.dmr.ModelNode;
 51import org.junit.Test;
 52import org.junit.runner.RunWith;
 53
 54/**
 55 * Demo using the AS management API to create and destroy a JMS queue.
 56 *
 57 * @author Emanuel Muckenhuber
 58 */
 59@RunWith(Arquillian.class)
 60@RunAsClient
 61public class JmsClientTestCase {
 62
 63    private static final String QUEUE_NAME = "createdTestQueue";
 64    private static final String EXPORTED_QUEUE_NAME = "java:jboss/exported/createdTestQueue";
 65
 66    @ContainerResource
 67    private Context remoteContext;
 68
 69    @ContainerResource
 70    private ManagementClient managementClient;
 71
 72    @Test
 73    public void testMessagingClient() throws Exception {
 74        QueueConnection conn = null;
 75        QueueSession session = null;
 76        ModelControllerClient client = managementClient.getControllerClient();
 77
 78        boolean actionsApplied = false;
 79        try {
 80
 81            // Create the queue using the management API
 82            ModelNode op = new ModelNode();
 83            op.get("operation").set("add");
 84            op.get("address").add("subsystem", "messaging");
 85            op.get("address").add("hornetq-server", "default");
 86            op.get("address").add("jms-queue", QUEUE_NAME);
 87            op.get("entries").add(EXPORTED_QUEUE_NAME);
 88            applyUpdate(op, client);
 89            actionsApplied = true;
 90
 91            QueueConnectionFactory qcf = (QueueConnectionFactory) remoteContext.lookup("jms/RemoteConnectionFactory");
 92            Queue queue = (Queue) remoteContext.lookup(QUEUE_NAME);
 93
 94            conn = qcf.createQueueConnection("guest", "guest");
 95            conn.start();
 96            session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
 97
 98            final CountDownLatch latch = new CountDownLatch(10);
 99            final List<String> result = new ArrayList<String>();
100
101            // Set the async listener
102            QueueReceiver recv = session.createReceiver(queue);
103            recv.setMessageListener(new MessageListener() {
104
105                @Override
106                public void onMessage(Message message) {
107                    TextMessage msg = (TextMessage)message;
108                    try {
109                        result.add(msg.getText());
110                        latch.countDown();
111                    } catch (JMSException e) {
112                        e.printStackTrace();
113                    }
114                }
115            });
116
117            QueueSender sender = session.createSender(queue);
118            for (int i = 0 ; i < 10 ; i++) {
119                String s = "Test" + i;
120                TextMessage msg = session.createTextMessage(s);
121                sender.send(msg);
122            }
123
124            Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
125            Assert.assertEquals(10, result.size());
126            for (int i = 0 ; i < result.size() ; i++) {
127                Assert.assertEquals("Test" + i, result.get(i));
128            }
129
130        } finally {
131            try {
132                conn.stop();
133            } catch (Exception ignore) {
134            }
135            try {
136                session.close();
137            } catch (Exception ignore) {
138            }
139            try {
140                conn.close();
141            } catch (Exception ignore) {
142            }
143
144            if(actionsApplied) {
145                // Remove the queue using the management API
146                ModelNode op = new ModelNode();
147                op.get("operation").set("remove");
148                op.get("address").add("subsystem", "messaging");
149                op.get("address").add("hornetq-server", "default");
150                op.get("address").add("jms-queue", QUEUE_NAME);
151                applyUpdate(op, client);
152            }
153        }
154    }
155
156    static void applyUpdate(ModelNode update, final ModelControllerClient client) throws IOException {
157        ModelNode result = client.execute(new OperationBuilder(update).build());
158        if (result.hasDefined("outcome") && "success".equals(result.get("outcome").asString())) {
159            if (result.hasDefined("result")) {
160                System.out.println(result.get("result"));
161            }
162        }
163        else if (result.hasDefined("failure-description")){
164            throw new RuntimeException(result.get("failure-description").toString());
165        }
166        else {
167            throw new RuntimeException("Operation not successful; outcome = " + result.get("outcome"));
168        }
169    }
170
171}