Running JBossMQ in simple unit tests using the Microcontaine
adrian.brock Feb 2, 2006 7:22 AMFYI: You can do this using the followng Microcontainer config against JBossMQ in head.
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="urn:jboss:bean-deployer:2.0"> <bean name="PersistenceManager" class="org.jboss.mq.pm.none.PersistenceManager"/> <bean name="StateManager" class="org.jboss.mq.sm.none.NullStateManager"/> <bean name="MessageCache" class="org.jboss.mq.kernel.MessageCache"> <property name="persistenceManager"><inject bean="PersistenceManager"/></property> </bean> <bean name="DestinationManager" class="org.jboss.mq.kernel.DestinationManager"> <property name="persistenceManagerInstance"><inject bean="PersistenceManager"/></property> <property name="stateManagerInstance"><inject bean="StateManager"/></property> <property name="messageCacheInstance"><inject bean="MessageCache"/></property> </bean> <bean name="JMSServer"> <constructor factoryMethod="getInterceptor"> <factory bean="DestinationManager"/> </constructor> </bean> <bean name="Invoker" class="org.jboss.mq.server.JMSServerInvoker"> <property name="next"><inject bean="JMSServer"/></property> </bean> <bean name="ServerIL" class="org.jboss.mq.il.jvm.JVMServerIL"> <constructor> <parameter><inject bean="Invoker"/></parameter> </constructor> </bean> <bean name="GCF" class="org.jboss.mq.GenericConnectionFactory"> <constructor> <parameter><inject bean="ServerIL"/></parameter> <parameter> ClientILService=org.jboss.mq.il.jvm.JVMClientILService </parameter> </constructor> </bean> <bean name="Queue" class="org.jboss.mq.SpyQueue"> <constructor> <parameter>testQueue</parameter> </constructor> </bean> <bean name="RealQueue" class="org.jboss.mq.server.JMSQueue"> <constructor> <parameter><inject bean="Queue"/></parameter> <parameter><null/></parameter> <parameter><inject bean="JMSServer"/></parameter> <parameter><inject bean="JMSServer" property="parameters"/></parameter> </constructor> <install bean="JMSServer" method="addDestination"> <parameter><this/></parameter> </install> <uninstall bean="JMSServer" method="closeDestination"> <parameter><inject bean="Queue"/></parameter> </uninstall> </bean> <bean name="ConnectionFactory" class="org.jboss.mq.SpyConnectionFactory"> <constructor> <parameter><inject bean="GCF"/></parameter> </constructor> </bean> </deployment>
NOTE: This config does NOT have security, persistence or support durable subscription
operations.
A simple unit test would look like this
(assuming the above config is in org/jboss/test/JBossMQUnitTestCase.xml)
/* * JBoss, Home of Professional Open Source * Copyright 2006, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.test; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import junit.framework.Test; import org.jboss.test.kernel.junit.MicrocontainerTest; /** * JMS Unit Tests * * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a> * @version $Revision: 1.3 $ */ public class JBossMQUnitTestCase extends MicrocontainerTest { public void testNoTx() throws Throwable { ConnectionFactory cf = (ConnectionFactory) getBean("ConnectionFactory"); Queue queue = (Queue) getBean("Queue"); Connection c = cf.createConnection(); try { c.start(); Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); Message m = s.createTextMessage("hello"); MessageProducer p = s.createProducer(queue); p.send(m); MessageConsumer r = s.createConsumer(queue); m = r.receiveNoWait(); getLog().info(m); } finally { c.close(); } } public static Test suite() throws Exception { return suite(JBossMQUnitTestCase.class); } public JBossMQUnitTestCase (String name) { super(name); } }
With the following output:
0 DEBUG [JBossMQUnitTestCase] ==== setUp org.jboss.test.JBossMQUnitTestCase ==== 86 DEBUG [KernelFactory] Starting JBoss Kernel construction... 415 DEBUG [KernelFactory] Completed JBoss Kernel construction. Duration: 327 milliseconds 507 DEBUG [JBossMQUnitTestCase] Deploying file:/home/ejort/jboss-head/workspace/rars/src/resources/tests/org/jboss/test/JBossMQUnitTestCase. xml 3527 DEBUG [PersistenceManager] Creating PersistenceManager 3529 DEBUG [PersistenceManager] Created PersistenceManager 3534 DEBUG [PersistenceManager] Starting PersistenceManager 3538 DEBUG [PersistenceManager] Started PersistenceManager 3597 DEBUG [NullStateManager] Creating NullStateManager 3598 DEBUG [NullStateManager] Created NullStateManager 3599 DEBUG [NullStateManager] Starting NullStateManager 3600 DEBUG [NullStateManager] Started NullStateManager 4124 DEBUG [JBossMQUnitTestCase] ==== Starting testNoTx ==== 4167 DEBUG [Connection] Setting the thread pool 4171 DEBUG [Connection] org.jboss.mq.connectionThreadPoolFactory=org.jboss.mq.threadpool.ThreadPoolFactory 4173 DEBUG [Connection] Client(2) 4175 DEBUG [Connection] Setting the clockDaemon's thread factory 4420 INFO [JBossMQUnitTestCase] SpyTextMessage { Header { jmsDestination : QUEUE.testQueue jmsDeliveryMode : 2 jmsExpiration : 0 jmsPriority : 4 jmsMessageID : ID:1-11388827625501 jmsTimeStamp : 1138882762550 jmsCorrelationID: null jmsReplyTo : null jmsType : null jmsRedelivered : false jmsProperties : {JMSXDeliveryCount=1} jmsPropReadWrite: false msgReadOnly : true producerClientId: ID:1 } Body { text :hello } } 4430 DEBUG [JBossMQUnitTestCase] testNoTx took 305ms 4431 DEBUG [JBossMQUnitTestCase] ==== Stopping testNoTx ==== 4432 DEBUG [JBossMQUnitTestCase] ==== tornDown org.jboss.test.JBossMQUnitTestCase ==== 4441 DEBUG [JMSDestinationManager] Closing destination QUEUE.testQueue 4459 DEBUG [MessageCache] Thread exiting. 4462 DEBUG [NullStateManager] Stopping NullStateManager 4464 DEBUG [NullStateManager] Stopped NullStateManager 4465 DEBUG [NullStateManager] Destroying NullStateManager 4466 DEBUG [NullStateManager] Destroyed NullStateManager 4467 DEBUG [PersistenceManager] Stopping PersistenceManager 4468 DEBUG [PersistenceManager] Stopped PersistenceManager 4469 DEBUG [PersistenceManager] Destroying PersistenceManager 4470 DEBUG [PersistenceManager] Destroyed PersistenceManager