[jboss-cvs] JBoss Messaging SVN: r4424 - in trunk: src/main/org/jboss/messaging/core/remoting/impl and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Jun 10 11:23:37 EDT 2008
Author: ataylor
Date: 2008-06-10 11:23:36 -0400 (Tue, 10 Jun 2008)
New Revision: 4424
Added:
trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryFactory.java
trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryLocator.java
trunk/tests/src/org/jboss/messaging/tests/unit/core/remoting/impl/RemotingConnectionTest.java
Removed:
trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistrySingleton.java
Modified:
trunk/src/main/org/jboss/messaging/core/remoting/impl/ConnectorRegistryImpl.java
trunk/src/main/org/jboss/messaging/core/remoting/impl/RemotingConnectionImpl.java
trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/ClientKeepAliveFactory.java
trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/RemotingServiceImpl.java
trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java
trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/MessageConsumerTest.java
Log:
added some tests and added a ConnectorRegistryFactory used for creating the ConnectorRegistryLocator used for INVM transports etc.
Copied: trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryFactory.java (from rev 4416, trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistrySingleton.java)
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryFactory.java (rev 0)
+++ trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryFactory.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.messaging.core.remoting;
+
+import org.jboss.messaging.core.remoting.impl.ConnectorRegistryImpl;
+
+/**
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ * @author <a href="mailto:ataylor at redhat.com">Andy Taylor</a>
+ * @version <tt>$Revision$</tt>
+ */
+public class ConnectorRegistryFactory implements ConnectorRegistryLocator
+{
+ // Constants -----------------------------------------------------
+ // Attributes ----------------------------------------------------
+
+ // Static --------------------------------------------------------
+
+ /**
+ * This singleton is used to get NIOConnectors.
+ * It also check if the RemotingConfiguration has been locally registered so that the client
+ * can use an in-vm connection to the server directly and skip the network.
+ */
+ private ConnectorRegistry registry = new ConnectorRegistryImpl();
+
+ private static ConnectorRegistryLocator REGISTRY_LOCATOR = null;
+
+ // Constructors --------------------------------------------------
+
+ // Public --------------------------------------------------------
+
+
+ public static ConnectorRegistry getRegistry()
+ {
+ if (REGISTRY_LOCATOR == null)
+ {
+ //todo create the locator from another source using sys props, for now just use the default
+ REGISTRY_LOCATOR = new ConnectorRegistryFactory();
+ }
+ return REGISTRY_LOCATOR.locate();
+ }
+
+ public static void setRegisteryLocator(ConnectorRegistryLocator RegisteryLocator)
+ {
+ REGISTRY_LOCATOR = RegisteryLocator;
+ }
+
+ // Z implementation ----------------------------------------------
+
+ public ConnectorRegistry locate()
+ {
+ return registry;
+ }
+// Y overrides ---------------------------------------------------
+
+ // Package protected ---------------------------------------------
+
+ // Protected -----------------------------------------------------
+
+ // Private -------------------------------------------------------
+
+ // Inner classes -------------------------------------------------
+}
Added: trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryLocator.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryLocator.java (rev 0)
+++ trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistryLocator.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -0,0 +1,11 @@
+package org.jboss.messaging.core.remoting;
+
+/**
+ * used for locating the ConnectorRegistry, can be overridden by the user.
+ *
+ * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ */
+public interface ConnectorRegistryLocator
+{
+ ConnectorRegistry locate();
+}
Deleted: trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistrySingleton.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistrySingleton.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/remoting/ConnectorRegistrySingleton.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -1,47 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- *
- * Distributable under LGPL license.
- * See terms of license at gnu.org.
- */
-package org.jboss.messaging.core.remoting;
-
-import org.jboss.messaging.core.remoting.impl.ConnectorRegistryImpl;
-
-/**
- * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- *
- * @version <tt>$Revision$</tt>
- *
- */
-public class ConnectorRegistrySingleton
-{
- // Constants -----------------------------------------------------
-
- // Attributes ----------------------------------------------------
-
- // Static --------------------------------------------------------
-
- /**
- * This singleton is used to get NIOConnectors.
- * It also check if the RemotingConfiguration has been locally registered so that the client
- * can use an in-vm connection to the server directly and skip the network.
- */
- public static final ConnectorRegistry REGISTRY = new ConnectorRegistryImpl();
-
- // Constructors --------------------------------------------------
-
- // Public --------------------------------------------------------
-
- // Z implementation ----------------------------------------------
-
- // Y overrides ---------------------------------------------------
-
- // Package protected ---------------------------------------------
-
- // Protected -----------------------------------------------------
-
- // Private -------------------------------------------------------
-
- // Inner classes -------------------------------------------------
-}
Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/ConnectorRegistryImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/ConnectorRegistryImpl.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/ConnectorRegistryImpl.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -6,29 +6,26 @@
*/
package org.jboss.messaging.core.remoting.impl;
-import static org.jboss.messaging.core.remoting.TransportType.INVM;
-import static org.jboss.messaging.core.remoting.TransportType.TCP;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
+import org.jboss.messaging.core.client.ConnectionParams;
+import org.jboss.messaging.core.client.Location;
import org.jboss.messaging.core.logging.Logger;
import org.jboss.messaging.core.remoting.ConnectorRegistry;
import org.jboss.messaging.core.remoting.NIOConnector;
import org.jboss.messaging.core.remoting.PacketDispatcher;
import org.jboss.messaging.core.remoting.TransportType;
+import static org.jboss.messaging.core.remoting.TransportType.INVM;
+import static org.jboss.messaging.core.remoting.TransportType.TCP;
import org.jboss.messaging.core.remoting.impl.invm.INVMConnector;
import org.jboss.messaging.core.remoting.impl.mina.MinaConnector;
-import org.jboss.messaging.core.client.Location;
-import org.jboss.messaging.core.client.ConnectionParams;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
/**
* @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- *
* @version <tt>$Revision$</tt>
- *
*/
public class ConnectorRegistryImpl implements ConnectorRegistry
{
@@ -40,11 +37,11 @@
// the String key corresponds to Configuration.getLocation()
private Map<String, PacketDispatcher> localDispatchers = new HashMap<String, PacketDispatcher>();
-
- public Map<String, NIOConnectorHolder> connectors = new HashMap<String, NIOConnectorHolder>();
-
+
+ private Map<String, NIOConnectorHolder> connectors = new HashMap<String, NIOConnectorHolder>();
+
private final AtomicLong idCounter = new AtomicLong(0);
-
+
// Static --------------------------------------------------------
/**
@@ -64,46 +61,46 @@
{
log.debug("registered " + key + " for " + serverDispatcher);
}
-
+
return (previousDispatcher == null);
}
/**
* @return <code>true</code> if this Configuration was registered,
* <code>false</code> else
- */
+ */
public boolean unregister(Location location)
- {
+ {
PacketDispatcher dispatcher = localDispatchers.remove(location.getLocation());
- if(log.isDebugEnabled())
- {
- log.debug("unregistered " + dispatcher);
- }
+ if (log.isDebugEnabled())
+ {
+ log.debug("unregistered " + dispatcher);
+ }
- return (dispatcher != null);
+ return (dispatcher != null);
}
public synchronized NIOConnector getConnector(Location location, ConnectionParams connectionParams)
{
assert location != null;
String key = location.getLocation();
-
+
if (connectors.containsKey(key))
- {
+ {
NIOConnectorHolder holder = connectors.get(key);
holder.increment();
NIOConnector connector = holder.getConnector();
if (log.isDebugEnabled())
log.debug("Reuse " + connector + " to connect to "
- + key + " [count=" + holder.getCount() + "]");
+ + key + " [count=" + holder.getCount() + "]");
return connector;
}
//TODO INVM optimisation is disabled for now
-
+
// check if the server is in the same vm than the client
// if (localDispatchers.containsKey(key))
// {
@@ -136,13 +133,13 @@
if (connector == null)
{
throw new IllegalArgumentException(
- "no connector defined for transport " + transport);
+ "no connector defined for transport " + transport);
}
if (log.isDebugEnabled())
log.debug("Created " + connector + " to connect to "
- + location);
-
+ + location);
+
NIOConnectorHolder holder = new NIOConnectorHolder(connector);
connectors.put(key, holder);
return connector;
@@ -151,16 +148,14 @@
/**
* Decrement the number of references on the NIOConnector corresponding to
* the Configuration.
- *
+ * <p/>
* If there is only one reference, remove it from the connectors Map and
* returns it. Otherwise return null.
- *
- * @param location
- * a Location
+ *
+ * @param location a Location
* @return the NIOConnector if there is no longer any references to it or
* <code>null</code>
- * @throws IllegalStateException
- * if no NIOConnector were created for the given Configuration
+ * @throws IllegalStateException if no NIOConnector were created for the given Configuration
*/
public synchronized NIOConnector removeConnector(Location location)
{
@@ -171,7 +166,7 @@
if (holder == null)
{
throw new IllegalStateException("No Connector were created for "
- + key);
+ + key);
}
if (holder.getCount() == 1)
@@ -180,12 +175,13 @@
log.debug("Removed connector for " + key);
connectors.remove(key);
return holder.getConnector();
- } else
+ }
+ else
{
holder.decrement();
if (log.isDebugEnabled())
log.debug(holder.getCount() + " remaining references to "
- + key);
+ + key);
return null;
}
}
Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/RemotingConnectionImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/RemotingConnectionImpl.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/RemotingConnectionImpl.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -21,43 +21,27 @@
*/
package org.jboss.messaging.core.remoting.impl;
-import static org.jboss.messaging.core.remoting.ConnectorRegistrySingleton.REGISTRY;
-
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-
import org.jboss.messaging.core.client.ConnectionParams;
import org.jboss.messaging.core.client.Location;
import org.jboss.messaging.core.client.RemotingSessionListener;
import org.jboss.messaging.core.exception.MessagingException;
import org.jboss.messaging.core.logging.Logger;
-import org.jboss.messaging.core.remoting.NIOConnector;
-import org.jboss.messaging.core.remoting.NIOSession;
-import org.jboss.messaging.core.remoting.Packet;
-import org.jboss.messaging.core.remoting.PacketDispatcher;
-import org.jboss.messaging.core.remoting.PacketHandler;
-import org.jboss.messaging.core.remoting.PacketReturner;
-import org.jboss.messaging.core.remoting.RemotingConnection;
+import org.jboss.messaging.core.remoting.*;
import org.jboss.messaging.core.remoting.impl.wireformat.MessagingExceptionMessage;
-import org.jboss.messaging.core.remoting.impl.wireformat.Ping;
/**
- *
* @author <a href="tim.fox at jboss.com">Tim Fox</a>
* @author <a href="ovidiu at feodorov.com">Ovidiu Feodorov</a>
* @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
* @version <tt>$Revision$</tt>
- * $Id$
+ * $Id$
*/
public class RemotingConnectionImpl implements RemotingConnection
{
// Constants ------------------------------------------------------------------------------------
private static final Logger log = Logger.getLogger(RemotingConnectionImpl.class);
-
+
// Static ---------------------------------------------------------------------------------------
// Attributes -----------------------------------------------------------------------------------
@@ -67,9 +51,9 @@
private final ConnectionParams connectionParams;
private NIOConnector connector;
-
+
private NIOSession session;
-
+
private RemotingSessionListener listener;
// Constructors ---------------------------------------------------------------------------------
@@ -78,34 +62,25 @@
{
assert location != null;
assert connectionParams != null;
-
+
this.location = location;
this.connectionParams = connectionParams;
-
+
log.trace(this + " created with configuration " + location);
}
- public RemotingConnectionImpl(final Location location, ConnectionParams connectionParams, NIOConnector nioConnector) throws Exception
- {
- assert location != null;
- assert connectionParams != null;
-
- this.location = location;
- this.connectionParams = connectionParams;
- connector = nioConnector;
- session = connector.connect();
- log.trace(this + " created with connector " + nioConnector);
- }
-
// Public ---------------------------------------------------------------------------------------
// RemotingConnection implementation ------------------------------------------------------------
-
+
public void start() throws Throwable
{
- if (log.isTraceEnabled()) { log.trace(this + " started remoting connection"); }
+ if (log.isTraceEnabled())
+ {
+ log.trace(this + " started remoting connection");
+ }
- connector = REGISTRY.getConnector(location, connectionParams);
+ connector = ConnectorRegistryFactory.getRegistry().getConnector(location, connectionParams);
session = connector.connect();
if (log.isDebugEnabled())
@@ -122,21 +97,20 @@
try
{
if (connector != null)
- {
+ {
if (listener != null)
connector.removeSessionListener(listener);
- NIOConnector connectorFromRegistry = REGISTRY.removeConnector(location);
+ NIOConnector connectorFromRegistry = ConnectorRegistryFactory.getRegistry().removeConnector(location);
if (connectorFromRegistry != null)
connectorFromRegistry.disconnect();
}
}
catch (Throwable ignore)
- {
+ {
log.trace(this + " failed to disconnect the new client", ignore);
}
-
+
connector = null;
-
log.trace(this + " closed");
}
@@ -148,26 +122,26 @@
}
return session.getID();
}
-
+
/**
* send the packet and block until a response is received (<code>oneWay</code> is set to <code>false</code>)
*/
public Packet sendBlocking(final long targetID, final long executorID, final Packet packet) throws MessagingException
{
checkConnected();
-
+
long handlerID = connector.getDispatcher().generateID();
-
+
ResponseHandler handler = new ResponseHandler(handlerID);
-
+
connector.getDispatcher().register(handler);
-
+
try
- {
+ {
packet.setTargetID(targetID);
packet.setExecutorID(executorID);
packet.setResponseTargetID(handlerID);
-
+
try
{
session.write(packet);
@@ -175,41 +149,41 @@
catch (Exception e)
{
log.error("Caught unexpected exception", e);
-
+
throw new MessagingException(MessagingException.INTERNAL_ERROR);
}
-
+
Packet response = handler.waitForResponse(connectionParams.getTimeout());
-
+
if (response == null)
{
throw new IllegalStateException("No response received for " + packet);
}
-
+
if (response instanceof MessagingExceptionMessage)
{
MessagingExceptionMessage message = (MessagingExceptionMessage) response;
-
+
throw message.getException();
}
else
{
return response;
- }
+ }
}
finally
{
connector.getDispatcher().unregister(handlerID);
- }
+ }
}
-
+
public void sendOneWay(final long targetID, final long executorID, final Packet packet) throws MessagingException
{
assert packet != null;
packet.setTargetID(targetID);
packet.setExecutorID(executorID);
-
+
try
{
session.write(packet);
@@ -217,11 +191,11 @@
catch (Exception e)
{
log.error("Caught unexpected exception", e);
-
+
throw new MessagingException(MessagingException.INTERNAL_ERROR);
}
}
-
+
public synchronized void setRemotingSessionListener(final RemotingSessionListener newListener)
{
if (listener != null && newListener != null)
@@ -233,21 +207,21 @@
{
connector.addSessionListener(newListener);
}
- else
+ else
{
connector.removeSessionListener(listener);
}
this.listener = newListener;
}
-
+
public PacketDispatcher getPacketDispatcher()
{
return connector.getDispatcher();
}
-
+
public Location getLocation()
{
- return location;
+ return location;
}
// Package protected ----------------------------------------------------------------------------
@@ -255,13 +229,13 @@
// Protected ------------------------------------------------------------------------------------
// Private --------------------------------------------------------------------------------------
-
+
private static class ResponseHandler implements PacketHandler
{
private long id;
-
+
private Packet response;
-
+
ResponseHandler(final long id)
{
this.id = id;
@@ -275,10 +249,10 @@
public synchronized void handle(final Packet packet, final PacketReturner sender)
{
this.response = packet;
-
+
notify();
}
-
+
public synchronized Packet waitForResponse(final long timeout)
{
long toWait = timeout;
@@ -293,17 +267,17 @@
catch (InterruptedException e)
{
}
-
+
long now = System.currentTimeMillis();
-
+
toWait -= now - start;
-
+
start = now;
}
-
- return response;
+
+ return response;
}
-
+
}
private void checkConnected() throws MessagingException
@@ -311,7 +285,7 @@
if (session == null)
{
throw new IllegalStateException("Client " + this
- + " is not connected.");
+ + " is not connected.");
}
if (!session.isConnected())
{
Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/ClientKeepAliveFactory.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/ClientKeepAliveFactory.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/ClientKeepAliveFactory.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -27,11 +27,11 @@
// Public --------------------------------------------------------
// KeepAliveFactory implementation -------------------------------
- boolean isAlive = true;
+ private boolean isAlive = true;
public Pong pong(long sessionID, Ping ping)
{
- Pong pong = new Pong(sessionID, !isAlive);
+ Pong pong = new Pong(sessionID, !isAlive());
pong.setTargetID(ping.getResponseTargetID());
return pong;
}
Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/RemotingServiceImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/RemotingServiceImpl.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/mina/RemotingServiceImpl.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -6,30 +6,7 @@
*/
package org.jboss.messaging.core.remoting.impl.mina;
-import static org.jboss.messaging.core.remoting.ConnectorRegistrySingleton.REGISTRY;
-import static org.jboss.messaging.core.remoting.TransportType.INVM;
-import static org.jboss.messaging.core.remoting.impl.RemotingConfigurationValidator.validate;
-import static org.jboss.messaging.core.remoting.impl.mina.FilterChainSupport.addCodecFilter;
-import static org.jboss.messaging.core.remoting.impl.mina.FilterChainSupport.addSSLFilter;
-
-import java.net.InetSocketAddress;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.mina.common.DefaultIoFilterChainBuilder;
-import org.apache.mina.common.IdleStatus;
-import org.apache.mina.common.IoService;
-import org.apache.mina.common.IoServiceListener;
-import org.apache.mina.common.IoSession;
+import org.apache.mina.common.*;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.jboss.beans.metadata.api.annotations.Install;
import org.jboss.beans.metadata.api.annotations.Uninstall;
@@ -39,11 +16,22 @@
import org.jboss.messaging.core.logging.Logger;
import org.jboss.messaging.core.ping.Pinger;
import org.jboss.messaging.core.ping.impl.PingerImpl;
+import org.jboss.messaging.core.remoting.ConnectorRegistryFactory;
import org.jboss.messaging.core.remoting.Interceptor;
import org.jboss.messaging.core.remoting.PacketDispatcher;
import org.jboss.messaging.core.remoting.RemotingService;
+import static org.jboss.messaging.core.remoting.TransportType.INVM;
import org.jboss.messaging.core.remoting.impl.PacketDispatcherImpl;
+import static org.jboss.messaging.core.remoting.impl.RemotingConfigurationValidator.validate;
+import static org.jboss.messaging.core.remoting.impl.mina.FilterChainSupport.addCodecFilter;
+import static org.jboss.messaging.core.remoting.impl.mina.FilterChainSupport.addSSLFilter;
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.*;
+
/**
* @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
* @version <tt>$Revision$</tt>
@@ -190,7 +178,7 @@
// if (!disableInvm)
log.info("Registering:" + config.getLocation());
- REGISTRY.register(config.getLocation(), dispatcher);
+ ConnectorRegistryFactory.getRegistry().register(config.getLocation(), dispatcher);
started = true;
}
@@ -208,7 +196,7 @@
threadPool.shutdown();
}
- REGISTRY.unregister(config.getLocation());
+ ConnectorRegistryFactory.getRegistry().unregister(config.getLocation());
started = false;
}
Modified: trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/src/main/org/jboss/messaging/core/server/impl/MessagingServerImpl.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -21,13 +21,6 @@
*/
package org.jboss.messaging.core.server.impl;
-import java.util.HashSet;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadFactory;
-
import org.jboss.messaging.core.config.Configuration;
import org.jboss.messaging.core.config.impl.ConfigurationImpl;
import org.jboss.messaging.core.deployers.Deployer;
@@ -43,11 +36,10 @@
import org.jboss.messaging.core.persistence.impl.nullpm.NullStorageManager;
import org.jboss.messaging.core.postoffice.PostOffice;
import org.jboss.messaging.core.postoffice.impl.PostOfficeImpl;
-import org.jboss.messaging.core.remoting.ConnectorRegistrySingleton;
+import org.jboss.messaging.core.remoting.ConnectorRegistryFactory;
import org.jboss.messaging.core.remoting.Interceptor;
import org.jboss.messaging.core.remoting.PacketReturner;
import org.jboss.messaging.core.remoting.RemotingService;
-import org.jboss.messaging.core.remoting.impl.mina.CleanUpNotifier;
import org.jboss.messaging.core.remoting.impl.mina.RemotingServiceImpl;
import org.jboss.messaging.core.remoting.impl.wireformat.CreateConnectionResponse;
import org.jboss.messaging.core.security.JBMSecurityManager;
@@ -68,7 +60,10 @@
import org.jboss.messaging.util.OrderedExecutorFactory;
import org.jboss.messaging.util.VersionLoader;
+import java.util.HashSet;
+import java.util.concurrent.*;
+
/**
* A Messaging Server
*
@@ -146,12 +141,11 @@
this();
this.configuration = configuration;
createTransport = true;
- remotingService = new RemotingServiceImpl(configuration);
+ remotingService = new RemotingServiceImpl(configuration);
}
// lifecycle methods ----------------------------------------------------------------
-
-
+
public synchronized void start() throws Exception
{
log.debug("starting MessagingServer");
@@ -176,7 +170,7 @@
connectionManager = new ConnectionManagerImpl();
memoryManager = new SimpleMemoryManager();
postOffice = new PostOfficeImpl(storageManager, queueFactory, configuration.isRequireDestinations());
- queueSettingsDeployer = new QueueSettingsDeployer(queueSettingsRepository);
+ queueSettingsDeployer = new QueueSettingsDeployer(queueSettingsRepository);
threadPool = Executors.newFixedThreadPool(configuration.getThreadPoolMaxSize(), new JBMThreadFactory("JBM-session-threads"));
orderedExecutorFactory = new OrderedExecutorFactory(threadPool);
@@ -242,7 +236,7 @@
{
remotingService.stop();
}
- ConnectorRegistrySingleton.REGISTRY.clear();
+ ConnectorRegistryFactory.getRegistry().clear();
}
// MessagingServer implementation -----------------------------------------------------------
@@ -365,7 +359,7 @@
return new CreateConnectionResponse(connection.getID(), version);
}
-
+
public OrderedExecutorFactory getOrderedExecutorFactory()
{
return orderedExecutorFactory;
@@ -378,22 +372,22 @@
// Protected ------------------------------------------------------------------------------------
// Private --------------------------------------------------------------------------------------
-
+
// Inner classes --------------------------------------------------------------------------------
-
+
private static class JBMThreadFactory implements ThreadFactory
{
private ThreadGroup group;
-
+
JBMThreadFactory(final String groupName)
{
- this.group = new ThreadGroup(groupName);
+ this.group = new ThreadGroup(groupName);
}
-
+
public Thread newThread(Runnable command)
- {
+ {
return new Thread(group, command);
- }
+ }
}
}
Modified: trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/MessageConsumerTest.java
===================================================================
--- trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/MessageConsumerTest.java 2008-06-10 14:36:20 UTC (rev 4423)
+++ trunk/tests/jms-tests/src/org/jboss/test/messaging/jms/MessageConsumerTest.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -21,51 +21,32 @@
*/
package org.jboss.test.messaging.jms;
-import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import org.jboss.messaging.jms.JBossQueue;
+import org.jboss.messaging.jms.JBossTopic;
+import javax.jms.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
-import javax.jms.BytesMessage;
-import javax.jms.Connection;
-import javax.jms.DeliveryMode;
-import javax.jms.InvalidDestinationException;
-import javax.jms.JMSException;
-import javax.jms.MapMessage;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
-import javax.jms.Queue;
-import javax.jms.QueueReceiver;
-import javax.jms.Session;
-import javax.jms.StreamMessage;
-import javax.jms.TextMessage;
-import javax.jms.Topic;
-import javax.jms.TopicSubscriber;
-
-import org.jboss.messaging.jms.JBossQueue;
-import org.jboss.messaging.jms.JBossTopic;
-
/**
* @author <a href="mailto:ovidiu at feodorov.com">Ovidiu Feodorov</a>
* @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
* @author <a href="mailto:clebert.suconic at jboss.com">Clebert Suconic</a>
* @version <tt>$Revision$</tt>
- *
- * $Id$
+ * <p/>
+ * $Id$
*/
public class MessageConsumerTest extends JMSTestCase
{
// Constants -----------------------------------------------------
// Static --------------------------------------------------------
-
+
// Attributes ----------------------------------------------------
// Constructors --------------------------------------------------
@@ -74,48 +55,48 @@
{
super(name);
}
-
+
// Public --------------------------------------------------------
public void testReceiveWithClientAckThenCloseSession() throws Exception
{
Connection conn = null;
-
+
try
{
conn = cf.createConnection();
-
+
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
+
MessageProducer prod = sess.createProducer(queue1);
-
+
final int NUM_MESSAGES = 5;
-
+
for (int i = 0; i < NUM_MESSAGES; i++)
{
TextMessage tm = sess.createTextMessage("message" + i);
-
+
prod.send(tm);
}
-
+
Session sess2 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
-
+
MessageConsumer cons = sess2.createConsumer(queue1);
-
+
conn.start();
-
+
for (int i = 0; i < NUM_MESSAGES; i++)
{
- TextMessage tm = (TextMessage)cons.receive(500);
-
+ TextMessage tm = (TextMessage) cons.receive(500);
+
assertNotNull(tm);
-
+
assertEquals("message" + i, tm.getText());
}
-
+
// Now close the session
- sess2.close();
+ sess2.close();
}
finally
{
@@ -123,89 +104,89 @@
{
conn.close();
}
-
+
removeAllMessages(queue1.getQueueName(), true, 0);
- }
+ }
}
-
-
+
+
public void testRelayMessage() throws Exception
{
Connection conn = null;
-
+
try
- {
- conn = cf.createConnection();
-
- conn.start();
-
- final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer cons = sess.createConsumer(queue1);
-
- final int numMessages = 100;
-
- class MyListener implements MessageListener
- {
- boolean failed;
-
- int count;
-
- public synchronized void onMessage(Message m)
- {
- try
- {
- MessageProducer prod = sess.createProducer(queue2);
-
- prod.send(m);
-
- count++;
-
- if (count == numMessages)
- {
- this.notify();
- }
- }
- catch (JMSException e)
- {
- failed = true;
- }
- }
-
- synchronized void waitForMessages() throws Exception
- {
- while (count < numMessages)
- {
- this.wait();
- }
- }
- }
-
- MyListener listener = new MyListener();
-
- cons.setMessageListener(listener);
-
- MessageProducer prod = sess.createProducer(queue1);
-
- for (int i = 0; i < numMessages; i++)
- {
- prod.send(sess.createMessage());
- }
-
- listener.waitForMessages();
-
- conn.close();
-
- assertFalse(listener.failed);
+ {
+ conn = cf.createConnection();
+
+ conn.start();
+
+ final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer cons = sess.createConsumer(queue1);
+
+ final int numMessages = 100;
+
+ class MyListener implements MessageListener
+ {
+ boolean failed;
+
+ int count;
+
+ public synchronized void onMessage(Message m)
+ {
+ try
+ {
+ MessageProducer prod = sess.createProducer(queue2);
+
+ prod.send(m);
+
+ count++;
+
+ if (count == numMessages)
+ {
+ this.notify();
+ }
+ }
+ catch (JMSException e)
+ {
+ failed = true;
+ }
+ }
+
+ synchronized void waitForMessages() throws Exception
+ {
+ while (count < numMessages)
+ {
+ this.wait();
+ }
+ }
+ }
+
+ MyListener listener = new MyListener();
+
+ cons.setMessageListener(listener);
+
+ MessageProducer prod = sess.createProducer(queue1);
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ prod.send(sess.createMessage());
+ }
+
+ listener.waitForMessages();
+
+ conn.close();
+
+ assertFalse(listener.failed);
}
finally
{
- if (conn != null)
- {
- conn.close();
- }
-
- removeAllMessages(queue2.getQueueName(), true, 0);
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ removeAllMessages(queue2.getQueueName(), true, 0);
}
}
@@ -224,51 +205,51 @@
public void testRedeliveryToCompetingConsumerOnQueue() throws Exception
{
Connection conn = null;
-
+
try
{
conn = cf.createConnection();
-
+
Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
+
MessageProducer prod = sessSend.createProducer(queue1);
-
+
conn.start();
-
+
Session sessConsume1 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
-
+
MessageConsumer cons1 = sessConsume1.createConsumer(queue1);
-
+
TextMessage tm = sessSend.createTextMessage();
-
+
tm.setText("Your mum");
-
+
prod.send(tm);
-
- TextMessage tm2 = (TextMessage)cons1.receive();
-
+
+ TextMessage tm2 = (TextMessage) cons1.receive();
+
assertNotNull(tm2);
-
+
assertEquals("Your mum", tm2.getText());
-
+
// Don't ack
-
+
// Create another consumer
-
+
Session sessConsume2 = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
-
+
MessageConsumer cons2 = sessConsume2.createConsumer(queue1);
-
+
// this should cancel message and cause delivery to other consumer
-
+
sessConsume1.close();
-
- TextMessage tm3 = (TextMessage)cons2.receive(1000);
-
+
+ TextMessage tm3 = (TextMessage) cons2.receive(1000);
+
assertNotNull(tm3);
-
+
assertEquals("Your mum", tm3.getText());
-
+
tm3.acknowledge();
}
finally
@@ -286,95 +267,95 @@
*/
public void testReceive() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- // start consumer connection before the message is submitted
- consumerConnection.start();
-
- TextMessage tm = producerSession.createTextMessage("someText");
-
- queueProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
-
- queueProducer.send(tm);
-
- TextMessage m = (TextMessage)queueConsumer.receive();
-
- assertEquals(tm.getText(), m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ // start consumer connection before the message is submitted
+ consumerConnection.start();
+
+ TextMessage tm = producerSession.createTextMessage("someText");
+
+ queueProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+ queueProducer.send(tm);
+
+ TextMessage m = (TextMessage) queueConsumer.receive();
+
+ assertEquals(tm.getText(), m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
+
}
public void testReceivePersistent() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- // start consumer connection before the message is submitted
- consumerConnection.start();
-
- TextMessage tm = producerSession.createTextMessage("someText");
-
- assertEquals(DeliveryMode.PERSISTENT, tm.getJMSDeliveryMode());
-
- queueProducer.send(tm);
-
- TextMessage m = (TextMessage)queueConsumer.receive();
-
- assertEquals(tm.getText(), m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ // start consumer connection before the message is submitted
+ consumerConnection.start();
+
+ TextMessage tm = producerSession.createTextMessage("someText");
+
+ assertEquals(DeliveryMode.PERSISTENT, tm.getJMSDeliveryMode());
+
+ queueProducer.send(tm);
+
+ TextMessage m = (TextMessage) queueConsumer.receive();
+
+ assertEquals(tm.getText(), m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
@@ -383,46 +364,46 @@
*/
public void testReceiveTimeout() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- TextMessage tm = producerSession.createTextMessage("someText");
-
- queueProducer.send(tm);
-
- // start consumer connection after the message is submitted
- consumerConnection.start();
-
- TextMessage m = (TextMessage)queueConsumer.receive(2000);
-
- assertEquals(tm.getText(), m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ TextMessage tm = producerSession.createTextMessage("someText");
+
+ queueProducer.send(tm);
+
+ // start consumer connection after the message is submitted
+ consumerConnection.start();
+
+ TextMessage m = (TextMessage) queueConsumer.receive(2000);
+
+ assertEquals(tm.getText(), m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
/**
@@ -430,55 +411,55 @@
*/
public void testReceiveNoWait() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- TextMessage tm = producerSession.createTextMessage("someText");
-
- queueProducer.send(tm);
-
- // start consumer connection after the message is submitted
- consumerConnection.start();
-
- //NOTE! There semantics of receiveNoWait do not guarantee the message is available
- //immediately after the message is sent
- //It will be available some indeterminate time later.
- //This is fine and as per spec.
- //To implement receiveNoWait otherwise would be very costly
- //Also other messaging systems e.g. Sun, ActiveMQ implement it this way
-
- Thread.sleep(500);
-
- TextMessage m = (TextMessage)queueConsumer.receiveNoWait();
-
- assertEquals(tm.getText(), m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ TextMessage tm = producerSession.createTextMessage("someText");
+
+ queueProducer.send(tm);
+
+ // start consumer connection after the message is submitted
+ consumerConnection.start();
+
+ //NOTE! There semantics of receiveNoWait do not guarantee the message is available
+ //immediately after the message is sent
+ //It will be available some indeterminate time later.
+ //This is fine and as per spec.
+ //To implement receiveNoWait otherwise would be very costly
+ //Also other messaging systems e.g. Sun, ActiveMQ implement it this way
+
+ Thread.sleep(500);
+
+ TextMessage m = (TextMessage) queueConsumer.receiveNoWait();
+
+ assertEquals(tm.getText(), m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
/**
@@ -486,53 +467,53 @@
*/
public void testReceiveOnListener() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- TextMessage tm = producerSession.createTextMessage("someText");
-
- queueProducer.send(tm);
-
- MessageListenerImpl l = new MessageListenerImpl();
-
- queueConsumer.setMessageListener(l);
-
- // start consumer connection after the message is submitted
- consumerConnection.start();
-
- // wait for the listener to receive the message
- l.waitForMessages();
-
- TextMessage m = (TextMessage)l.getNextMessage();
-
- assertEquals(tm.getText(), m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ TextMessage tm = producerSession.createTextMessage("someText");
+
+ queueProducer.send(tm);
+
+ MessageListenerImpl l = new MessageListenerImpl();
+
+ queueConsumer.setMessageListener(l);
+
+ // start consumer connection after the message is submitted
+ consumerConnection.start();
+
+ // wait for the listener to receive the message
+ l.waitForMessages();
+
+ TextMessage m = (TextMessage) l.getNextMessage();
+
+ assertEquals(tm.getText(), m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
//
@@ -542,10 +523,10 @@
public void testCreateConsumerOnNonExistentTopic() throws Exception
{
Connection pconn = null;
-
+
try
{
- pconn = cf.createConnection();
+ pconn = cf.createConnection();
Session ps = pconn.createSession(false, Session.AUTO_ACKNOWLEDGE);
@@ -554,7 +535,7 @@
ps.createConsumer(new JBossTopic("NoSuchTopic"));
fail("should throw exception");
}
- catch(InvalidDestinationException e)
+ catch (InvalidDestinationException e)
{
// OK
}
@@ -563,15 +544,15 @@
{
if (pconn != null)
{
- pconn.close();
+ pconn.close();
}
}
}
-
+
public void testCreateConsumerOnNonExistentQueue() throws Exception
{
Connection pconn = null;
-
+
try
{
pconn = cf.createConnection();
@@ -583,7 +564,7 @@
ps.createConsumer(new JBossQueue("NoSuchQueue"));
fail("should throw exception");
}
- catch(InvalidDestinationException e)
+ catch (InvalidDestinationException e)
{
// OK
}
@@ -596,7 +577,7 @@
}
}
}
-
+
//
// closed consumer tests
//
@@ -604,6 +585,7 @@
/* Test that an ack can be sent after the consumer that received the message has been closed.
* Acks are scoped per session.
*/
+
public void testAckAfterConsumerClosed() throws Exception
{
Connection connSend = null;
@@ -635,7 +617,7 @@
MessageConsumer cons = sessReceive.createConsumer(queue1);
- TextMessage m2 = (TextMessage)cons.receive(1500);
+ TextMessage m2 = (TextMessage) cons.receive(1500);
assertNotNull(m2);
@@ -662,113 +644,113 @@
public void testClientAcknowledgmentOnClosedConsumer() throws Exception
{
- Connection producerConnection = null;
+ Connection producerConnection = null;
- Connection consumerConnection = null;
+ Connection consumerConnection = null;
- try
- {
- producerConnection = cf.createConnection();
+ try
+ {
+ producerConnection = cf.createConnection();
- consumerConnection = cf.createConnection();
+ consumerConnection = cf.createConnection();
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageProducer queueProducer = producerSession.createProducer(queue1);
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
- TextMessage tm = producerSession.createTextMessage();
+ TextMessage tm = producerSession.createTextMessage();
- tm.setText("One");
+ tm.setText("One");
- queueProducer.send(tm);
+ queueProducer.send(tm);
- consumerConnection.start();
+ consumerConnection.start();
- TextMessage m = (TextMessage)queueConsumer.receive(1500);
+ TextMessage m = (TextMessage) queueConsumer.receive(1500);
- assertEquals(m.getText(), "One");
+ assertEquals(m.getText(), "One");
- queueConsumer.close();
+ queueConsumer.close();
- m.acknowledge();
+ m.acknowledge();
- try
- {
- queueConsumer.receive(2000);
- fail("should throw exception");
- }
- catch (javax.jms.IllegalStateException e)
- {
- // OK
- }
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ try
+ {
+ queueConsumer.receive(2000);
+ fail("should throw exception");
+ }
+ catch (javax.jms.IllegalStateException e)
+ {
+ // OK
+ }
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testSendMessageAndCloseConsumer1() throws Exception
{
- Connection producerConnection = null;
+ Connection producerConnection = null;
- Connection consumerConnection = null;
+ Connection consumerConnection = null;
- try
- {
- producerConnection = cf.createConnection();
+ try
+ {
+ producerConnection = cf.createConnection();
- consumerConnection = cf.createConnection();
+ consumerConnection = cf.createConnection();
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageProducer queueProducer = producerSession.createProducer(queue1);
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
- Message m = producerSession.createMessage();
-
- queueProducer.send(m);
-
- queueConsumer.close();
-
- // since no message was received, we expect the message back in the queue
-
- queueConsumer = consumerSession.createConsumer(queue1);
-
- consumerConnection.start();
-
- Message r = queueConsumer.receive(2000);
-
- assertEquals(m.getJMSMessageID(), r.getJMSMessageID());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
-
- removeAllMessages(queue1.getQueueName(), true, 0);
+ Message m = producerSession.createMessage();
+
+ queueProducer.send(m);
+
+ queueConsumer.close();
+
+ // since no message was received, we expect the message back in the queue
+
+ queueConsumer = consumerSession.createConsumer(queue1);
+
+ consumerConnection.start();
+
+ Message r = queueConsumer.receive(2000);
+
+ assertEquals(m.getJMSMessageID(), r.getJMSMessageID());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
+
+ removeAllMessages(queue1.getQueueName(), true, 0);
}
@@ -778,530 +760,530 @@
*/
public void testSendMessageAndCloseConsumer2() throws Exception
{
- Connection producerConnection = null;
+ Connection producerConnection = null;
- Connection consumerConnection = null;
+ Connection consumerConnection = null;
- try
- {
- producerConnection = cf.createConnection();
+ try
+ {
+ producerConnection = cf.createConnection();
- consumerConnection = cf.createConnection();
+ consumerConnection = cf.createConnection();
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Session consumerSession = consumerConnection.createSession(true, 0);
+ Session consumerSession = consumerConnection.createSession(true, 0);
- MessageProducer queueProducer = producerSession.createProducer(queue1);
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- TextMessage tm = producerSession.createTextMessage();
-
- tm.setText("One");
-
- queueProducer.send(tm);
-
- tm.setText("Two");
-
- queueProducer.send(tm);
-
- consumerConnection.start();
-
- TextMessage m = (TextMessage)queueConsumer.receive(1500);
-
- assertEquals("One", m.getText());
-
- queueConsumer.close();
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
- consumerSession.commit();
+ TextMessage tm = producerSession.createTextMessage();
- // I expect that "Two" is still in the queue
-
- MessageConsumer queueConsumer2 = consumerSession.createConsumer(queue1);
-
- m = (TextMessage)queueConsumer2.receive(1500);
-
- assertNotNull(m);
-
- assertEquals(m.getText(), "Two");
-
- consumerSession.commit();
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ tm.setText("One");
+
+ queueProducer.send(tm);
+
+ tm.setText("Two");
+
+ queueProducer.send(tm);
+
+ consumerConnection.start();
+
+ TextMessage m = (TextMessage) queueConsumer.receive(1500);
+
+ assertEquals("One", m.getText());
+
+ queueConsumer.close();
+
+ consumerSession.commit();
+
+ // I expect that "Two" is still in the queue
+
+ MessageConsumer queueConsumer2 = consumerSession.createConsumer(queue1);
+
+ m = (TextMessage) queueConsumer2.receive(1500);
+
+ assertNotNull(m);
+
+ assertEquals(m.getText(), "Two");
+
+ consumerSession.commit();
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
- public void testRedel0() throws Exception
- {
- Connection conn = null;
+ public void testRedel0() throws Exception
+ {
+ Connection conn = null;
- try
- {
- conn = cf.createConnection();
- conn.start();
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("a");
- TextMessage tm2 = sess.createTextMessage("b");
- TextMessage tm3 = sess.createTextMessage("c");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
- sess.commit();
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("a");
+ TextMessage tm2 = sess.createTextMessage("b");
+ TextMessage tm3 = sess.createTextMessage("c");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+ sess.commit();
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- TextMessage rm1 = (TextMessage)cons1.receive();
- assertNotNull(rm1);
- assertEquals("a", rm1.getText());
+ TextMessage rm1 = (TextMessage) cons1.receive();
+ assertNotNull(rm1);
+ assertEquals("a", rm1.getText());
- cons1.close();
+ cons1.close();
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- sess.commit();
+ sess.commit();
- TextMessage rm2 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm2);
- assertEquals("b", rm2.getText());
+ TextMessage rm2 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("b", rm2.getText());
- TextMessage rm3 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm3);
- assertEquals("c", rm3.getText());
-
- sess.commit();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ TextMessage rm3 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("c", rm3.getText());
+ sess.commit();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
- public void testRedel1() throws Exception
- {
- Connection conn = null;
+ checkEmpty(queue1);
- try
- {
- conn = cf.createConnection();
- conn.start();
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1");
- TextMessage tm2 = sess.createTextMessage("hello2");
- TextMessage tm3 = sess.createTextMessage("hello3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
- sess.commit();
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ public void testRedel1() throws Exception
+ {
+ Connection conn = null;
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1", rm1.getText());
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- cons1.close();
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1");
+ TextMessage tm2 = sess.createTextMessage("hello2");
+ TextMessage tm3 = sess.createTextMessage("hello3");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+ sess.commit();
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- sess.commit();
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1", rm1.getText());
- TextMessage rm2 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm2);
- assertEquals("hello2", rm2.getText());
+ cons1.close();
- TextMessage rm3 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm3);
- assertEquals("hello3", rm3.getText());
-
- sess.commit();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ sess.commit();
- public void testRedel2() throws Exception
- {
- Connection conn = null;
+ TextMessage rm2 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("hello2", rm2.getText());
- try
- {
- conn = cf.createConnection();
- conn.start();
+ TextMessage rm3 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("hello3", rm3.getText());
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1-a");
- TextMessage tm2 = sess.createTextMessage("hello2-a");
- TextMessage tm3 = sess.createTextMessage("hello3-a");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
- sess.commit();
+ sess.commit();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ checkEmpty(queue1);
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1-a", rm1.getText());
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
- cons1.close();
+ public void testRedel2() throws Exception
+ {
+ Connection conn = null;
- sess.commit();
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1-a");
+ TextMessage tm2 = sess.createTextMessage("hello2-a");
+ TextMessage tm3 = sess.createTextMessage("hello3-a");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+ sess.commit();
- TextMessage rm2 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm2);
- assertEquals("hello2-a", rm2.getText());
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- TextMessage rm3 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm3);
- assertEquals("hello3-a", rm3.getText());
-
- sess.commit();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1-a", rm1.getText());
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ cons1.close();
- public void testRedel3() throws Exception
- {
- Connection conn = null;
+ sess.commit();
- try
- {
- conn = cf.createConnection();
- conn.start();
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1");
- log.trace(tm1.getJMSMessageID());
- TextMessage tm2 = sess.createTextMessage("hello2");
- TextMessage tm3 = sess.createTextMessage("hello3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
- sess.commit();
+ TextMessage rm2 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("hello2-a", rm2.getText());
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ TextMessage rm3 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("hello3-a", rm3.getText());
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1", rm1.getText());
- log.trace(rm1.getJMSMessageID());
+ sess.commit();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
- log.trace("rolling back");
- //rollback should cause redelivery of messages not acked
- sess.rollback();
- log.trace("rolled back");
+ checkEmpty(queue1);
- TextMessage rm2 = (TextMessage)cons1.receive(1500);
- assertEquals("hello1", rm2.getText());
- log.trace(rm1.getJMSMessageID());
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
- TextMessage rm3 = (TextMessage)cons1.receive(1500);
- assertEquals("hello2", rm3.getText());
+ public void testRedel3() throws Exception
+ {
+ Connection conn = null;
- TextMessage rm4 = (TextMessage)cons1.receive(1500);
- assertEquals("hello3", rm4.getText());
-
- sess.commit();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- //This last step is important - there shouldn't be any more messages to receive
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- public void testRedel4() throws Exception
- {
- Connection conn = null;
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1");
+ log.trace(tm1.getJMSMessageID());
+ TextMessage tm2 = sess.createTextMessage("hello2");
+ TextMessage tm3 = sess.createTextMessage("hello3");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+ sess.commit();
- try
- {
- conn = cf.createConnection();
- conn.start();
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1");
- TextMessage tm2 = sess.createTextMessage("hello2");
- TextMessage tm3 = sess.createTextMessage("hello3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
- sess.commit();
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1", rm1.getText());
+ log.trace(rm1.getJMSMessageID());
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ log.trace("rolling back");
+ //rollback should cause redelivery of messages not acked
+ sess.rollback();
+ log.trace("rolled back");
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1", rm1.getText());
+ TextMessage rm2 = (TextMessage) cons1.receive(1500);
+ assertEquals("hello1", rm2.getText());
+ log.trace(rm1.getJMSMessageID());
- cons1.close();
+ TextMessage rm3 = (TextMessage) cons1.receive(1500);
+ assertEquals("hello2", rm3.getText());
- //rollback should cause redelivery of messages
+ TextMessage rm4 = (TextMessage) cons1.receive(1500);
+ assertEquals("hello3", rm4.getText());
- //in this case redelivery occurs to a different receiver
+ sess.commit();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
- sess.rollback();
+ //This last step is important - there shouldn't be any more messages to receive
+ checkEmpty(queue1);
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
- TextMessage rm2 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm2);
- assertEquals("hello1", rm2.getText());
+ public void testRedel4() throws Exception
+ {
+ Connection conn = null;
- TextMessage rm3 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm3);
- assertEquals("hello2", rm3.getText());
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- TextMessage rm4 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm4);
- assertEquals("hello3", rm4.getText());
-
- sess.commit();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- //This last step is important - there shouldn't be any more messages to receive
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1");
+ TextMessage tm2 = sess.createTextMessage("hello2");
+ TextMessage tm3 = sess.createTextMessage("hello3");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+ sess.commit();
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- public void testRedel5() throws Exception
- {
- Connection conn = null;
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1", rm1.getText());
- try
- {
- conn = cf.createConnection();
- conn.start();
+ cons1.close();
- Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1");
- TextMessage tm2 = sess.createTextMessage("hello2");
- TextMessage tm3 = sess.createTextMessage("hello3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
+ //rollback should cause redelivery of messages
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ //in this case redelivery occurs to a different receiver
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1", rm1.getText());
+ sess.rollback();
- //redeliver
- sess.recover();
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- TextMessage rm2 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm2);
- assertEquals("hello1", rm2.getText());
+ TextMessage rm2 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("hello1", rm2.getText());
- TextMessage rm3 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm3);
- assertEquals("hello2", rm3.getText());
+ TextMessage rm3 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("hello2", rm3.getText());
- TextMessage rm4 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm4);
- assertEquals("hello3", rm4.getText());
-
- rm4.acknowledge();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- //This last step is important - there shouldn't be any more messages to receive
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ TextMessage rm4 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm4);
+ assertEquals("hello3", rm4.getText());
- public void testRedel6() throws Exception
- {
- Connection conn = null;
+ sess.commit();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
- try
- {
- conn = cf.createConnection();
- conn.start();
+ //This last step is important - there shouldn't be any more messages to receive
+ checkEmpty(queue1);
- Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("hello1");
- TextMessage tm2 = sess.createTextMessage("hello2");
- TextMessage tm3 = sess.createTextMessage("hello3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
- MessageConsumer cons1 = sess.createConsumer(queue1);
- TextMessage rm1 = (TextMessage)cons1.receive(1500);
- assertNotNull(rm1);
- assertEquals("hello1", rm1.getText());
+ public void testRedel5() throws Exception
+ {
+ Connection conn = null;
- cons1.close();
-
- log.debug("sess.recover()");
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- //redeliver
- sess.recover();
+ Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1");
+ TextMessage tm2 = sess.createTextMessage("hello2");
+ TextMessage tm3 = sess.createTextMessage("hello3");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- log.debug("receiving ...");
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1", rm1.getText());
- TextMessage rm2 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm2);
- assertEquals("hello1", rm2.getText());
+ //redeliver
+ sess.recover();
- TextMessage rm3 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm3);
- assertEquals("hello2", rm3.getText());
+ TextMessage rm2 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("hello1", rm2.getText());
- TextMessage rm4 = (TextMessage)cons2.receive(1500);
- assertNotNull(rm4);
- assertEquals("hello3", rm4.getText());
-
- rm4.acknowledge();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- //This last step is important - there shouldn't be any more messages to receive
- checkEmpty(queue1);
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ TextMessage rm3 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("hello2", rm3.getText());
+ TextMessage rm4 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm4);
+ assertEquals("hello3", rm4.getText());
+
+ rm4.acknowledge();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ //This last step is important - there shouldn't be any more messages to receive
+ checkEmpty(queue1);
+
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
+
+ public void testRedel6() throws Exception
+ {
+ Connection conn = null;
+
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
+
+ Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ MessageProducer prod = sess.createProducer(queue1);
+ TextMessage tm1 = sess.createTextMessage("hello1");
+ TextMessage tm2 = sess.createTextMessage("hello2");
+ TextMessage tm3 = sess.createTextMessage("hello3");
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
+
+ MessageConsumer cons1 = sess.createConsumer(queue1);
+
+ TextMessage rm1 = (TextMessage) cons1.receive(1500);
+ assertNotNull(rm1);
+ assertEquals("hello1", rm1.getText());
+
+ cons1.close();
+
+ log.debug("sess.recover()");
+
+ //redeliver
+ sess.recover();
+
+ MessageConsumer cons2 = sess.createConsumer(queue1);
+
+ log.debug("receiving ...");
+
+ TextMessage rm2 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm2);
+ assertEquals("hello1", rm2.getText());
+
+ TextMessage rm3 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm3);
+ assertEquals("hello2", rm3.getText());
+
+ TextMessage rm4 = (TextMessage) cons2.receive(1500);
+ assertNotNull(rm4);
+ assertEquals("hello3", rm4.getText());
+
+ rm4.acknowledge();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ //This last step is important - there shouldn't be any more messages to receive
+ checkEmpty(queue1);
+
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
+
/**
* http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71350
*/
- public void testRedel7() throws Exception
- {
- Connection conn = null;
+ public void testRedel7() throws Exception
+ {
+ Connection conn = null;
- try
- {
- conn = cf.createConnection();
- conn.start();
+ try
+ {
+ conn = cf.createConnection();
+ conn.start();
- Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageProducer prod = sess.createProducer(queue1);
+ MessageProducer prod = sess.createProducer(queue1);
- TextMessage tm1 = sess.createTextMessage("1");
+ TextMessage tm1 = sess.createTextMessage("1");
- TextMessage tm2 = sess.createTextMessage("2");
+ TextMessage tm2 = sess.createTextMessage("2");
- TextMessage tm3 = sess.createTextMessage("3");
+ TextMessage tm3 = sess.createTextMessage("3");
- prod.send(tm1);
- prod.send(tm2);
- prod.send(tm3);
+ prod.send(tm1);
+ prod.send(tm2);
+ prod.send(tm3);
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- TextMessage r1 = (TextMessage)cons1.receive();
+ TextMessage r1 = (TextMessage) cons1.receive();
- assertEquals(tm1.getText(), r1.getText());
+ assertEquals(tm1.getText(), r1.getText());
- cons1.close();
+ cons1.close();
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- TextMessage r2 = (TextMessage)cons2.receive();
+ TextMessage r2 = (TextMessage) cons2.receive();
- assertEquals(tm2.getText(), r2.getText());
+ assertEquals(tm2.getText(), r2.getText());
- TextMessage r3 = (TextMessage)cons2.receive();
+ TextMessage r3 = (TextMessage) cons2.receive();
- assertEquals(tm3.getText(), r3.getText());
+ assertEquals(tm3.getText(), r3.getText());
- r1.acknowledge();
- r2.acknowledge();
- r3.acknowledge();
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
- }
+ r1.acknowledge();
+ r2.acknowledge();
+ r3.acknowledge();
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
+ }
/**
* http://www.jboss.org/index.html?module=bb&op=viewtopic&t=71350
@@ -1310,44 +1292,44 @@
{
Connection conn = null;
- try
- {
- conn = cf.createConnection();
+ try
+ {
+ conn = cf.createConnection();
- Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sess.createProducer(queue1);
+ MessageProducer prod = sess.createProducer(queue1);
- //Send 3 messages
+ //Send 3 messages
- prod.send(sess.createTextMessage("1"));
- prod.send(sess.createTextMessage("2"));
- prod.send(sess.createTextMessage("3"));
-
- conn.start();
+ prod.send(sess.createTextMessage("1"));
+ prod.send(sess.createTextMessage("2"));
+ prod.send(sess.createTextMessage("3"));
- MessageConsumer cons1 = sess.createConsumer(queue1);
+ conn.start();
- cons1.close();
+ MessageConsumer cons1 = sess.createConsumer(queue1);
- MessageConsumer cons2 = sess.createConsumer(queue1);
+ cons1.close();
- Message r1 = cons2.receive();
- Message r2 = cons2.receive();
- Message r3 = cons2.receive();
+ MessageConsumer cons2 = sess.createConsumer(queue1);
- //Messages should be received?
- assertNotNull(r1);
- assertNotNull(r2);
- assertNotNull(r3);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- }
+ Message r1 = cons2.receive();
+ Message r2 = cons2.receive();
+ Message r3 = cons2.receive();
+
+ //Messages should be received?
+ assertNotNull(r1);
+ assertNotNull(r2);
+ assertNotNull(r3);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ }
}
public void testSendAndReceivePersistentDifferentConnections() throws Exception
@@ -1381,7 +1363,7 @@
MessageConsumer cons = sessReceive.createConsumer(queue1);
- TextMessage m2 = (TextMessage)cons.receive(1500);
+ TextMessage m2 = (TextMessage) cons.receive(1500);
assertNotNull(m2);
@@ -1390,7 +1372,7 @@
sessReceive.commit();
cons.close();
-
+
connReceive.close();
connReceive = cf.createConnection();
@@ -1405,542 +1387,543 @@
{
if (connSend != null) connSend.close();
if (connReceive != null) connReceive.close();
-
+
checkEmpty(queue1);
}
}
public void testMultipleConcurrentConsumers() throws Exception
- {
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- consumerConnection.start();
- Session sess1 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Session sess2 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Session sess3 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer cons1 = sess1.createConsumer(topic1);
- MessageConsumer cons2 = sess2.createConsumer(topic1);
- MessageConsumer cons3 = sess3.createConsumer(topic1);
-
- final int NUM_MESSAGES = 10;
-
- class Receiver implements Runnable
- {
- Receiver(MessageConsumer c1)
- {
- cons = c1;
- }
- MessageConsumer cons;
- boolean failed;
- public void run()
- {
- try
- {
- for (int i = 0; i < NUM_MESSAGES; i++)
- {
- TextMessage m = (TextMessage)cons.receive(5000);
- if (m == null)
- {
- log.error("Didn't receive all the messages");
- failed = true;
- break;
- }
- if (!m.getText().equals("testing"))
- {
- failed = true;
- }
- }
-
- }
- catch (Exception e)
- {
- log.error("Failed in receiving messages", e);
- failed = true;
- }
- }
- }
-
-
- Receiver rec1 = new Receiver(cons1);
- Receiver rec2 = new Receiver(cons2);
- Receiver rec3 = new Receiver(cons3);
-
- Thread t1 = new Thread(rec1);
- Thread t2 = new Thread(rec2);
- Thread t3 = new Thread(rec3);
-
- log.trace("Starting threads");
-
- t1.start();
- t2.start();
- t3.start();
-
- log.trace("Sending messages to topic");
-
- producerConnection.start();
- Session prodSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = prodSession.createProducer(topic1);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
-
- for (int i = 0; i < NUM_MESSAGES; i++)
- {
- Message m = prodSession.createTextMessage("testing");
- prod.send(m);
- log.trace("Sent message to topic");
- }
-
- t1.join(15000);
- t2.join(15000);
- t3.join(15000);
-
- sess1.close();
- sess2.close();
- sess3.close();
- prodSession.close();
-
- assertTrue(!rec1.failed);
- assertTrue(!rec2.failed);
- assertTrue(!rec3.failed);
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
- }
+ {
+ Connection producerConnection = null;
+ Connection consumerConnection = null;
+ try
+ {
+ producerConnection = cf.createConnection();
+ consumerConnection = cf.createConnection();
+
+ consumerConnection.start();
+ Session sess1 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session sess2 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session sess3 = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer cons1 = sess1.createConsumer(topic1);
+ MessageConsumer cons2 = sess2.createConsumer(topic1);
+ MessageConsumer cons3 = sess3.createConsumer(topic1);
+
+ final int NUM_MESSAGES = 10;
+
+ class Receiver implements Runnable
+ {
+ Receiver(MessageConsumer c1)
+ {
+ cons = c1;
+ }
+
+ MessageConsumer cons;
+ boolean failed;
+
+ public void run()
+ {
+ try
+ {
+ for (int i = 0; i < NUM_MESSAGES; i++)
+ {
+ TextMessage m = (TextMessage) cons.receive(5000);
+ if (m == null)
+ {
+ log.error("Didn't receive all the messages");
+ failed = true;
+ break;
+ }
+ if (!m.getText().equals("testing"))
+ {
+ failed = true;
+ }
+ }
+
+ }
+ catch (Exception e)
+ {
+ log.error("Failed in receiving messages", e);
+ failed = true;
+ }
+ }
+ }
+
+
+ Receiver rec1 = new Receiver(cons1);
+ Receiver rec2 = new Receiver(cons2);
+ Receiver rec3 = new Receiver(cons3);
+
+ Thread t1 = new Thread(rec1);
+ Thread t2 = new Thread(rec2);
+ Thread t3 = new Thread(rec3);
+
+ log.trace("Starting threads");
+
+ t1.start();
+ t2.start();
+ t3.start();
+
+ log.trace("Sending messages to topic");
+
+ producerConnection.start();
+ Session prodSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = prodSession.createProducer(topic1);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+ for (int i = 0; i < NUM_MESSAGES; i++)
+ {
+ Message m = prodSession.createTextMessage("testing");
+ prod.send(m);
+ log.trace("Sent message to topic");
+ }
+
+ t1.join(15000);
+ t2.join(15000);
+ t3.join(15000);
+
+ sess1.close();
+ sess2.close();
+ sess3.close();
+ prodSession.close();
+
+ assertTrue(!rec1.failed);
+ assertTrue(!rec2.failed);
+ assertTrue(!rec3.failed);
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
+ }
+
+
public void testGetSelector() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- String selector = "JMSType = 'something'";
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1, selector);
-
- assertEquals(selector, topicConsumer.getMessageSelector());
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ String selector = "JMSType = 'something'";
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1, selector);
+
+ assertEquals(selector, topicConsumer.getMessageSelector());
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testGetSelectorOnClosedConsumer() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- topicConsumer.close();
-
- try
- {
- topicConsumer.getMessageSelector();
- }
- catch(javax.jms.IllegalStateException e)
- {
- // OK
- }
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ topicConsumer.close();
+
+ try
+ {
+ topicConsumer.getMessageSelector();
+ }
+ catch (javax.jms.IllegalStateException e)
+ {
+ // OK
+ }
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testGetTopic() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- Topic t = ((TopicSubscriber)topicConsumer).getTopic();
-
- assertEquals(topic1, t);
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ Topic t = ((TopicSubscriber) topicConsumer).getTopic();
+
+ assertEquals(topic1, t);
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testGetTopicOnClosedConsumer() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- topicConsumer.close();
-
- try
- {
- ((TopicSubscriber)topicConsumer).getTopic();
- }
- catch(javax.jms.IllegalStateException e)
- {
- // OK
- }
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ topicConsumer.close();
+
+ try
+ {
+ ((TopicSubscriber) topicConsumer).getTopic();
+ }
+ catch (javax.jms.IllegalStateException e)
+ {
+ // OK
+ }
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testGetQueue() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- Queue q = ((QueueReceiver)queueConsumer).getQueue();
-
- assertEquals(queue1, q);
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ Queue q = ((QueueReceiver) queueConsumer).getQueue();
+
+ assertEquals(queue1, q);
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testGetQueueOnClosedConsumer() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- queueConsumer.close();
-
- try
- {
- ((QueueReceiver)queueConsumer).getQueue();
- }
- catch(javax.jms.IllegalStateException e)
- {
- // OK
- }
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ queueConsumer.close();
+
+ try
+ {
+ ((QueueReceiver) queueConsumer).getQueue();
+ }
+ catch (javax.jms.IllegalStateException e)
+ {
+ // OK
+ }
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveOnTopicTimeoutNoMessage() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- Message m = topicConsumer.receive(1000);
-
- assertNull(m);
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ Message m = topicConsumer.receive(1000);
+
+ assertNull(m);
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveOnTopicConnectionStopped() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- final Message m = producerSession.createMessage();
- new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- // this is needed to make sure the main thread has enough time to block
- Thread.sleep(1000);
- topicProducer.send(m);
- }
- catch(Exception e)
- {
- log.error(e);
- }
- }
- }, "Producer").start();
-
- assertNull(topicConsumer.receive(1500));
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ final Message m = producerSession.createMessage();
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ // this is needed to make sure the main thread has enough time to block
+ Thread.sleep(1000);
+ topicProducer.send(m);
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ }
+ }, "Producer").start();
+
+ assertNull(topicConsumer.receive(1500));
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveOnTopicTimeout() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- consumerConnection.start();
-
- final Message m1 = producerSession.createMessage();
- new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- // this is needed to make sure the main thread has enough time to block
- Thread.sleep(1000);
- topicProducer.send(m1);
- }
- catch(Exception e)
- {
- log.error(e);
- }
- }
- }, "Producer").start();
-
- Message m2 = topicConsumer.receive(1500);
- assertEquals(m1.getJMSMessageID(), m2.getJMSMessageID());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ consumerConnection.start();
+
+ final Message m1 = producerSession.createMessage();
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ // this is needed to make sure the main thread has enough time to block
+ Thread.sleep(1000);
+ topicProducer.send(m1);
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ }
+ }, "Producer").start();
+
+ Message m2 = topicConsumer.receive(1500);
+ assertEquals(m1.getJMSMessageID(), m2.getJMSMessageID());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveOnTopic() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- consumerConnection.start();
-
- final Message m1 = producerSession.createMessage();
- new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- // this is needed to make sure the main thread has enough time to block
- Thread.sleep(1000);
- topicProducer.send(m1);
- }
- catch(Exception e)
- {
- log.error(e);
- }
- }
- }, "Producer").start();
-
- Message m2 = topicConsumer.receive(3000);
+ Connection producerConnection = null;
- assertEquals(m1.getJMSMessageID(), m2.getJMSMessageID());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ consumerConnection.start();
+
+ final Message m1 = producerSession.createMessage();
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ // this is needed to make sure the main thread has enough time to block
+ Thread.sleep(1000);
+ topicProducer.send(m1);
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ }
+ }, "Producer").start();
+
+ Message m2 = topicConsumer.receive(3000);
+
+ assertEquals(m1.getJMSMessageID(), m2.getJMSMessageID());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveNoWaitOnTopic() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- consumerConnection.start();
-
- Message m = topicConsumer.receiveNoWait();
-
- assertNull(m);
-
- Message m1 = producerSession.createMessage();
- topicProducer.send(m1);
-
- // block this thread for a while to allow ServerConsumerDelegate's delivery thread to kick in
- Thread.sleep(500);
-
- m = topicConsumer.receiveNoWait();
-
- assertEquals(m1.getJMSMessageID(), m.getJMSMessageID());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ consumerConnection.start();
+
+ Message m = topicConsumer.receiveNoWait();
+
+ assertNull(m);
+
+ Message m1 = producerSession.createMessage();
+ topicProducer.send(m1);
+
+ // block this thread for a while to allow ServerConsumerDelegate's delivery thread to kick in
+ Thread.sleep(500);
+
+ m = topicConsumer.receiveNoWait();
+
+ assertEquals(m1.getJMSMessageID(), m.getJMSMessageID());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
/**
@@ -1948,66 +1931,66 @@
*/
public void testStressReceiveOnQueue() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- final int count = 100;
-
- consumerConnection.start();
-
- new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- for (int i = 0; i < count; i++)
- {
- Message m = producerSession.createMessage();
- queueProducer.send(m);
- }
- }
- catch(Exception e)
- {
- log.error(e);
- }
- }
- }, "ProducerTestThread").start();
-
- for (int i = 0; i < count; i++)
- {
- Message m = queueConsumer.receive(1500);
- assertNotNull(m);
- }
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
-
- checkEmpty(queue1);
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ final int count = 100;
+
+ consumerConnection.start();
+
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ for (int i = 0; i < count; i++)
+ {
+ Message m = producerSession.createMessage();
+ queueProducer.send(m);
+ }
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ }
+ }, "ProducerTestThread").start();
+
+ for (int i = 0; i < count; i++)
+ {
+ Message m = queueConsumer.receive(1500);
+ assertNotNull(m);
+ }
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+
+ checkEmpty(queue1);
+ }
}
@@ -2016,303 +1999,306 @@
*/
public void testStressReceiveOnTopic() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- final int count = 1000;
-
- consumerConnection.start();
-
- new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- // this is needed to make sure the main thread has enough time to block
- Thread.sleep(1000);
-
- for (int i = 0; i < count; i++)
- {
- Message m = producerSession.createMessage();
- topicProducer.send(m);
- }
- }
- catch(Exception e)
- {
- log.error(e);
- }
- }
- }, "ProducerTestThread").start();
-
- for (int i = 0; i < count; i++)
- {
- Message m = topicConsumer.receive(10000);
- assertNotNull(m);
- }
-
- checkEmpty(topic1);
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ final int count = 1000;
+
+ consumerConnection.start();
+
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ // this is needed to make sure the main thread has enough time to block
+ Thread.sleep(1000);
+
+ for (int i = 0; i < count; i++)
+ {
+ Message m = producerSession.createMessage();
+ topicProducer.send(m);
+ }
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ }
+ }, "ProducerTestThread").start();
+
+ for (int i = 0; i < count; i++)
+ {
+ Message m = topicConsumer.receive(10000);
+ assertNotNull(m);
+ }
+
+ checkEmpty(topic1);
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testReceiveOnClose() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- consumerConnection.start();
- final CountDownLatch latch = new CountDownLatch(1);
- Thread closerThread = new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- // this is needed to make sure the main thread has enough time to block
- Thread.sleep(1000);
- topicConsumer.close();
- }
- catch(Exception e)
- {
- log.error(e);
- }
- finally
- {
- latch.countDown();
- }
- }
- }, "closing thread");
- closerThread.start();
-
- assertNull(topicConsumer.receive(1500));
-
- // wait for the closing thread to finish
- boolean closed = latch.await(5000, TimeUnit.MILLISECONDS);
- assertTrue(closed);
-
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ consumerConnection.start();
+ final CountDownLatch latch = new CountDownLatch(1);
+ Thread closerThread = new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ // this is needed to make sure the main thread has enough time to block
+ Thread.sleep(1000);
+ topicConsumer.close();
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ }
+ finally
+ {
+ latch.countDown();
+ }
+ }
+ }, "closing thread");
+ closerThread.start();
+
+ assertNull(topicConsumer.receive(1500));
+
+ // wait for the closing thread to finish
+ boolean closed = latch.await(5000, TimeUnit.MILLISECONDS);
+ assertTrue(closed);
+
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
- /** to be used by testTimeoutReceiveOnClose */
- private class ThreadCloser extends Thread
- {
+ /**
+ * to be used by testTimeoutReceiveOnClose
+ */
+ private class ThreadCloser extends Thread
+ {
- Object waitMonitor;
- long timeToSleep;
- MessageConsumer topicConsumer;
+ Object waitMonitor;
+ long timeToSleep;
+ MessageConsumer topicConsumer;
- public ThreadCloser( Object waitMonitor, long timeToSleep, MessageConsumer topicConsumer)
- {
- this.waitMonitor=waitMonitor;
- this.timeToSleep=timeToSleep;
- this.topicConsumer = topicConsumer;
- }
+ public ThreadCloser(Object waitMonitor, long timeToSleep, MessageConsumer topicConsumer)
+ {
+ this.waitMonitor = waitMonitor;
+ this.timeToSleep = timeToSleep;
+ this.topicConsumer = topicConsumer;
+ }
- public void run()
- {
- try
- {
- log.info("(ThreadCloser)Waiting on monitor to close thread");
- synchronized (waitMonitor)
- {
- waitMonitor.wait();
- }
- log.info("(ThreadCloser)Notification received");
- Thread.sleep(timeToSleep);
- topicConsumer.close();
+ public void run()
+ {
+ try
+ {
+ log.info("(ThreadCloser)Waiting on monitor to close thread");
+ synchronized (waitMonitor)
+ {
+ waitMonitor.wait();
+ }
+ log.info("(ThreadCloser)Notification received");
+ Thread.sleep(timeToSleep);
+ topicConsumer.close();
- }
- catch (Exception e)
- {
- log.error(e);
- e.printStackTrace();
- }
- }
- }
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ e.printStackTrace();
+ }
+ }
+ }
- /** to be used by testTimeoutReceiveOnClose */
- private class ThreadReceiver extends Thread
- {
+ /**
+ * to be used by testTimeoutReceiveOnClose
+ */
+ private class ThreadReceiver extends Thread
+ {
- long timeToWait;
- Object waitMonitor;
- long t1;
- long t2;
- Object receivedObject;
- MessageConsumer topicConsumer;
+ long timeToWait;
+ Object waitMonitor;
+ long t1;
+ long t2;
+ Object receivedObject;
+ MessageConsumer topicConsumer;
- public ThreadReceiver(Object waitMonitor, long timeToWait, MessageConsumer topicConsumer)
- {
- this.waitMonitor=waitMonitor;
- this.timeToWait=timeToWait;
- this.topicConsumer = topicConsumer;
- }
+ public ThreadReceiver(Object waitMonitor, long timeToWait, MessageConsumer topicConsumer)
+ {
+ this.waitMonitor = waitMonitor;
+ this.timeToWait = timeToWait;
+ this.topicConsumer = topicConsumer;
+ }
- public void run()
- {
- try
- {
- log.info("(ThreadReceiver)Waiting on monitor to close thread");
- synchronized(waitMonitor)
- {
- waitMonitor.wait();
- }
- log.info("(ThreadReceiver)Notification received");
- t1=System.currentTimeMillis();
- receivedObject=topicConsumer.receive(timeToWait);
- t2=System.currentTimeMillis();
+ public void run()
+ {
+ try
+ {
+ log.info("(ThreadReceiver)Waiting on monitor to close thread");
+ synchronized (waitMonitor)
+ {
+ waitMonitor.wait();
+ }
+ log.info("(ThreadReceiver)Notification received");
+ t1 = System.currentTimeMillis();
+ receivedObject = topicConsumer.receive(timeToWait);
+ t2 = System.currentTimeMillis();
- }
- catch (Exception e)
- {
- log.error(e);
- e.printStackTrace();
- }
- }
- }
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ e.printStackTrace();
+ }
+ }
+ }
- public void testTimeoutReceiveOnClose() throws Exception
- {
- Connection consumerConnection = null;
+ public void testTimeoutReceiveOnClose() throws Exception
+ {
+ Connection consumerConnection = null;
- try
- {
- consumerConnection = cf.createConnection();
+ try
+ {
+ consumerConnection = cf.createConnection();
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- //This is a really weird test - the received object is always going to be null since no message is sent!!
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
- forceGC(); /// If A GC need to be executed, it' s better to be executed now
- if (log.isTraceEnabled()) log.trace("testTimeoutReceiveOnClose");
+ //This is a really weird test - the received object is always going to be null since no message is sent!!
- Object monitor = new Object();
- ThreadCloser closer = null;
- ThreadReceiver receiver = null;
+ forceGC(); /// If A GC need to be executed, it' s better to be executed now
+ if (log.isTraceEnabled()) log.trace("testTimeoutReceiveOnClose");
- closer = new ThreadCloser(monitor,1000, topicConsumer);
- receiver= new ThreadReceiver(monitor,2000, topicConsumer);
- closer.start();
- receiver.start();
- Thread.sleep(2000);
- synchronized (monitor)
- {
- monitor.notifyAll();
- }
- closer.join();
- receiver.join();
+ Object monitor = new Object();
+ ThreadCloser closer = null;
+ ThreadReceiver receiver = null;
- assertNull(receiver.receivedObject);
+ closer = new ThreadCloser(monitor, 1000, topicConsumer);
+ receiver = new ThreadReceiver(monitor, 2000, topicConsumer);
+ closer.start();
+ receiver.start();
+ Thread.sleep(2000);
+ synchronized (monitor)
+ {
+ monitor.notifyAll();
+ }
+ closer.join();
+ receiver.join();
- log.info("Elapsed time was " + (receiver.t2-receiver.t1));
+ assertNull(receiver.receivedObject);
- // We need to make sure the
- assertTrue("Receive was supposed to receive a notification before 2 seconds",receiver.t2-receiver.t1<=1500);
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
- }
+ log.info("Elapsed time was " + (receiver.t2 - receiver.t1));
+ // We need to make sure the
+ assertTrue("Receive was supposed to receive a notification before 2 seconds", receiver.t2 - receiver.t1 <= 1500);
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
+ }
+
//
// MessageListener tests
//
public void testMessageListenerOnTopic() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+ Connection producerConnection = null;
- MessageListenerImpl l = new MessageListenerImpl();
- topicConsumer.setMessageListener(l);
-
- consumerConnection.start();
-
- Message m1 = producerSession.createMessage();
- topicProducer.send(m1);
-
- // block the current thread until the listener gets something; this is to avoid closing
- // the connection too early
- l.waitForMessages();
-
- assertEquals(m1.getJMSMessageID(), l.getNextMessage().getJMSMessageID());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
- }
+ Connection consumerConnection = null;
+ try
+ {
+ producerConnection = cf.createConnection();
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ MessageListenerImpl l = new MessageListenerImpl();
+ topicConsumer.setMessageListener(l);
+
+ consumerConnection.start();
+
+ Message m1 = producerSession.createMessage();
+ topicProducer.send(m1);
+
+ // block the current thread until the listener gets something; this is to avoid closing
+ // the connection too early
+ l.waitForMessages();
+
+ assertEquals(m1.getJMSMessageID(), l.getNextMessage().getJMSMessageID());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
+ }
+
//// TODO: enable this
//// public void testMessageListenerOnTopicMultipleMessages() throws Exception
//// {
@@ -2384,107 +2370,108 @@
public void testSetMessageListenerTwice() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer topicProducer = producerSession.createProducer(topic1);
-
- MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- MessageListenerImpl listener1 = new MessageListenerImpl();
-
- topicConsumer.setMessageListener(listener1);
+ Connection producerConnection = null;
- MessageListenerImpl listener2 = new MessageListenerImpl();
-
- topicConsumer.setMessageListener(listener2);
+ Connection consumerConnection = null;
- consumerConnection.start();
-
- Message m1 = producerSession.createMessage();
- topicProducer.send(m1);
-
- // block the current thread until the listener gets something; this is to avoid closing
- // connection too early
+ try
+ {
+ producerConnection = cf.createConnection();
- listener2.waitForMessages();
-
- assertEquals(m1.getJMSMessageID(), listener2.getNextMessage().getJMSMessageID());
- assertEquals(0, listener1.size());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer topicProducer = producerSession.createProducer(topic1);
+
+ MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ MessageListenerImpl listener1 = new MessageListenerImpl();
+
+ topicConsumer.setMessageListener(listener1);
+
+ MessageListenerImpl listener2 = new MessageListenerImpl();
+
+ topicConsumer.setMessageListener(listener2);
+
+ consumerConnection.start();
+
+ Message m1 = producerSession.createMessage();
+ topicProducer.send(m1);
+
+ // block the current thread until the listener gets something; this is to avoid closing
+ // connection too early
+
+ listener2.waitForMessages();
+
+ assertEquals(m1.getJMSMessageID(), listener2.getNextMessage().getJMSMessageID());
+ assertEquals(0, listener1.size());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testSetMessageListenerWhileReceiving() throws Exception
{
- Connection consumerConnection = null;
-
- try
- {
- consumerConnection = cf.createConnection();
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- final MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
-
- consumerConnection.start();
- Thread worker1= new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- topicConsumer.receive(3000);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }}, "Receiver");
-
- worker1.start();
-
- Thread.sleep(1000);
-
- try
- {
- topicConsumer.setMessageListener(new MessageListenerImpl());
- fail("should have thrown JMSException");
- }
- catch(JMSException e)
- {
- // ok
- log.trace(e.getMessage());
- }
- }
- finally
- {
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ consumerConnection = cf.createConnection();
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ final MessageConsumer topicConsumer = consumerSession.createConsumer(topic1);
+
+ consumerConnection.start();
+ Thread worker1 = new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ topicConsumer.receive(3000);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }, "Receiver");
+
+ worker1.start();
+
+ Thread.sleep(1000);
+
+ try
+ {
+ topicConsumer.setMessageListener(new MessageListenerImpl());
+ fail("should have thrown JMSException");
+ }
+ catch (JMSException e)
+ {
+ // ok
+ log.trace(e.getMessage());
+ }
+ }
+ finally
+ {
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
// This is commented out until http://jira.jboss.com/jira/browse/JBMESSAGING-983 is complete
@@ -2549,69 +2536,71 @@
// Thread.sleep(15000);
// assertEquals("Should have received all messages after restarting", MESSAGE_COUNT, messagesReceived.get());
// }
-
+
// Test that stop doesn't in any way break subsequent close
+
public void testCloseAfterStop() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
-
- MessageListener myListener = new MessageListener() {
- public void onMessage(Message message)
- {
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException e)
- {
- // Ignore
- }
- }
- };
-
- queueConsumer.setMessageListener(myListener);
-
- consumerConnection.start();
-
- for (int i = 0; i < 100; i++)
- {
- queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
- }
-
- consumerConnection.stop();
-
- consumerConnection.close();
-
- consumerConnection = null;
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
+ Connection producerConnection = null;
+
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+
+ MessageListener myListener = new MessageListener()
+ {
+ public void onMessage(Message message)
+ {
+ try
+ {
+ Thread.sleep(100);
+ }
+ catch (InterruptedException e)
+ {
+ // Ignore
+ }
+ }
+ };
+
+ queueConsumer.setMessageListener(myListener);
+
+ consumerConnection.start();
+
+ for (int i = 0; i < 100; i++)
+ {
+ queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
+ }
+
+ consumerConnection.stop();
+
+ consumerConnection.close();
+
+ consumerConnection = null;
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
}
//
@@ -2620,112 +2609,112 @@
public void testTwoConsumersNonTransacted() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
-
- TextMessage tm = producerSession.createTextMessage();
- tm.setText("One");
- queueProducer.send(tm);
- tm.setText("Two");
- queueProducer.send(tm);
+ Connection producerConnection = null;
- // recreate the connection and receive the first message
- consumerConnection = cf.createConnection();
- Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
- consumerConnection.start();
-
- TextMessage m = (TextMessage)queueConsumer.receive(1500);
- assertEquals("One", m.getText());
-
- consumerConnection.close();
- consumerConnection = null;
-
- // recreate the connection and receive the second message
- consumerConnection = cf.createConnection();
- consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- queueConsumer = consumerSession.createConsumer(queue1);
- consumerConnection.start();
-
- m = (TextMessage)queueConsumer.receive(1500);
- assertEquals("Two", m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ TextMessage tm = producerSession.createTextMessage();
+ tm.setText("One");
+ queueProducer.send(tm);
+ tm.setText("Two");
+ queueProducer.send(tm);
+
+ // recreate the connection and receive the first message
+ consumerConnection = cf.createConnection();
+ Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+ consumerConnection.start();
+
+ TextMessage m = (TextMessage) queueConsumer.receive(1500);
+ assertEquals("One", m.getText());
+
+ consumerConnection.close();
+ consumerConnection = null;
+
+ // recreate the connection and receive the second message
+ consumerConnection = cf.createConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ queueConsumer = consumerSession.createConsumer(queue1);
+ consumerConnection.start();
+
+ m = (TextMessage) queueConsumer.receive(1500);
+ assertEquals("Two", m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ }
}
public void testTwoConsumersTransacted() throws Exception
{
- Connection producerConnection = null;
-
- Connection consumerConnection = null;
-
- try
- {
- producerConnection = cf.createConnection();
-
- Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer queueProducer = producerSession.createProducer(queue1);
+ Connection producerConnection = null;
- TextMessage tm = producerSession.createTextMessage();
- tm.setText("One");
- queueProducer.send(tm);
- tm.setText("Two");
- queueProducer.send(tm);
-
- // recreate the connection and receive the first message
- consumerConnection = cf.createConnection();
- Session consumerSession = consumerConnection.createSession(true, -1);
- MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
- consumerConnection.start();
-
- TextMessage m = (TextMessage)queueConsumer.receive(1500);
- assertEquals("One", m.getText());
-
- consumerSession.commit();
- consumerConnection.close();
- consumerConnection = null;
-
- // recreate the connection and receive the second message
- consumerConnection = cf.createConnection();
- consumerSession = consumerConnection.createSession(true, -1);
- queueConsumer = consumerSession.createConsumer(queue1);
- consumerConnection.start();
-
- m = (TextMessage)queueConsumer.receive(1500);
- assertEquals("Two", m.getText());
- }
- finally
- {
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
+ Connection consumerConnection = null;
+
+ try
+ {
+ producerConnection = cf.createConnection();
+
+ Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer queueProducer = producerSession.createProducer(queue1);
+
+ TextMessage tm = producerSession.createTextMessage();
+ tm.setText("One");
+ queueProducer.send(tm);
+ tm.setText("Two");
+ queueProducer.send(tm);
+
+ // recreate the connection and receive the first message
+ consumerConnection = cf.createConnection();
+ Session consumerSession = consumerConnection.createSession(true, -1);
+ MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
+ consumerConnection.start();
+
+ TextMessage m = (TextMessage) queueConsumer.receive(1500);
+ assertEquals("One", m.getText());
+
+ consumerSession.commit();
+ consumerConnection.close();
+ consumerConnection = null;
+
+ // recreate the connection and receive the second message
+ consumerConnection = cf.createConnection();
+ consumerSession = consumerConnection.createSession(true, -1);
+ queueConsumer = consumerSession.createConsumer(queue1);
+ consumerConnection.start();
+
+ m = (TextMessage) queueConsumer.receive(1500);
+ assertEquals("Two", m.getText());
+ }
+ finally
+ {
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
}
//
@@ -2770,6 +2759,7 @@
boolean exceptionThrown;
public Message m;
MessageConsumer consumer;
+
TestRunnable(MessageConsumer consumer)
{
this.consumer = consumer;
@@ -2827,43 +2817,43 @@
}
}
}
-
+
public void testNoLocalMemoryExhaustion() throws Exception
{
- Connection conn = null;
-
- try
- {
- conn = cf.createConnection();
-
- Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- MessageProducer prod = sess.createProducer(topic1);
-
- MessageConsumer cons = sess.createConsumer(topic1, null, true);
-
- final int numMessages = 100;
-
- for (int i = 0; i < numMessages; i++)
- {
- prod.send(sess.createMessage());
- }
-
- conn.start();
-
- Message msg = cons.receive(3000);
-
- assertNull(msg);
-
- checkEmpty(topic1);
- }
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- }
+ Connection conn = null;
+
+ try
+ {
+ conn = cf.createConnection();
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer prod = sess.createProducer(topic1);
+
+ MessageConsumer cons = sess.createConsumer(topic1, null, true);
+
+ final int numMessages = 100;
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ prod.send(sess.createMessage());
+ }
+
+ conn.start();
+
+ Message msg = cons.receive(3000);
+
+ assertNull(msg);
+
+ checkEmpty(topic1);
+ }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ }
}
/*
@@ -2897,8 +2887,8 @@
TextMessage tm = sess3.createTextMessage("nurse!");
prod.send(tm);
- TextMessage tm1 = (TextMessage)cons1.receive(1500);
- TextMessage tm2 = (TextMessage)cons2.receive(1500);
+ TextMessage tm1 = (TextMessage) cons1.receive(1500);
+ TextMessage tm2 = (TextMessage) cons2.receive(1500);
assertNotNull(tm1);
assertNotNull(tm2);
@@ -2913,12 +2903,12 @@
sess2.recover();
- tm2 = (TextMessage)cons2.receive(1500);
+ tm2 = (TextMessage) cons2.receive(1500);
assertNotNull(tm2);
assertEquals("nurse!", tm2.getText());
//but tm1 should not be redelivered
- tm1 = (TextMessage)cons1.receive(1500);
+ tm1 = (TextMessage) cons1.receive(1500);
assertNull(tm1);
}
finally
@@ -2933,11 +2923,10 @@
/**
- Topics shouldn't persist messages for non durable subscribers and redeliver them on reconnection
- even if delivery mode of persistent is specified
- See JMS spec. sec. 6.12
-
- */
+ * Topics shouldn't persist messages for non durable subscribers and redeliver them on reconnection
+ * even if delivery mode of persistent is specified
+ * See JMS spec. sec. 6.12
+ */
public void testNoRedeliveryOnNonDurableSubscriber() throws Exception
{
Connection conn1 = null;
@@ -2968,7 +2957,7 @@
int count = 0;
while (true)
{
- TextMessage tm = (TextMessage)cons.receive(1000);
+ TextMessage tm = (TextMessage) cons.receive(1000);
if (tm == null) break;
assertEquals(tm.getText(), "helloxyz");
count++;
@@ -2976,10 +2965,10 @@
assertEquals(NUM_MESSAGES, count);
conn1.close();
-
+
conn1 = null;
-
- checkEmpty(topic1);
+
+ checkEmpty(topic1);
}
finally
{
@@ -3052,19 +3041,19 @@
assertEquals("aardvark", m2.getStringProperty("p1"));
- BytesMessage bm2 = (BytesMessage)theConsumer.receive(1500);
+ BytesMessage bm2 = (BytesMessage) theConsumer.receive(1500);
assertEquals("aardvark", bm2.readUTF());
- MapMessage mm2 = (MapMessage)theConsumer.receive(1500);
+ MapMessage mm2 = (MapMessage) theConsumer.receive(1500);
assertEquals("aardvark", mm2.getString("s1"));
- ObjectMessage om2 = (ObjectMessage)theConsumer.receive(1500);
- assertEquals("aardvark", (String)om2.getObject());
+ ObjectMessage om2 = (ObjectMessage) theConsumer.receive(1500);
+ assertEquals("aardvark", (String) om2.getObject());
- StreamMessage sm2 = (StreamMessage)theConsumer.receive(1500);
+ StreamMessage sm2 = (StreamMessage) theConsumer.receive(1500);
assertEquals("aardvark", sm2.readString());
-
- TextMessage tm2 = (TextMessage)theConsumer.receive(1500);
+
+ TextMessage tm2 = (TextMessage) theConsumer.receive(1500);
assertEquals("aardvark", tm2.getText());
}
finally
@@ -3111,7 +3100,7 @@
int count = 0;
while (true)
{
- TextMessage tm = (TextMessage)durable.receive(1500);
+ TextMessage tm = (TextMessage) durable.receive(1500);
if (tm == null)
{
break;
@@ -3185,7 +3174,7 @@
int count = 0;
while (true)
{
- TextMessage tm = (TextMessage)durable3.receive(1000);
+ TextMessage tm = (TextMessage) durable3.receive(1000);
if (tm == null)
{
break;
@@ -3202,13 +3191,13 @@
Message m = durable4.receive(1000);
assertNull(m);
-
+
durable3.close();
-
+
sess3.unsubscribe("mySubscription2");
-
+
durable4.close();
-
+
sess3.unsubscribe("mySubscription1");
}
finally
@@ -3276,7 +3265,7 @@
int count = 0;
while (true)
{
- TextMessage tm = (TextMessage)durable.receive(1000);
+ TextMessage tm = (TextMessage) durable.receive(1000);
if (tm == null)
{
break;
@@ -3294,7 +3283,7 @@
sess4 = conn4.createSession(false, Session.AUTO_ACKNOWLEDGE);
durable = sess4.createDurableSubscriber(topic1, "mySubscription");
- TextMessage tm = (TextMessage)durable.receive(1000);
+ TextMessage tm = (TextMessage) durable.receive(1000);
assertNull(tm);
conn4.close();
@@ -3327,7 +3316,7 @@
sess6 = conn6.createSession(false, Session.AUTO_ACKNOWLEDGE);
durable = sess6.createDurableSubscriber(topic1, "mySubscription");
- TextMessage tm3 = (TextMessage)durable.receive(1000);
+ TextMessage tm3 = (TextMessage) durable.receive(1000);
assertNull(tm3);
durable.close();
@@ -3397,7 +3386,7 @@
for (int i = 0; i < NUM_TO_RECEIVE; i++)
{
- TextMessage tm = (TextMessage)durable.receive(3000);
+ TextMessage tm = (TextMessage) durable.receive(3000);
assertNotNull(tm);
}
@@ -3420,7 +3409,7 @@
int count = 0;
while (true)
{
- TextMessage tm = (TextMessage)durable2.receive(1500);
+ TextMessage tm = (TextMessage) durable2.receive(1500);
if (tm == null)
{
break;
@@ -3482,7 +3471,7 @@
for (int i = 0; i < NUM_TO_RECEIVE1; i++)
{
- TextMessage tm = (TextMessage)durable.receive(1500);
+ TextMessage tm = (TextMessage) durable.receive(1500);
if (tm == null)
{
fail();
@@ -3504,21 +3493,21 @@
conn2.start();
- TextMessage tm = (TextMessage)durable2.receive(1500);
+ TextMessage tm = (TextMessage) durable2.receive(1500);
assertNull(tm);
-
+
durable2.close();
sess2.unsubscribe("mySubscription");
-
+
//Now need to remove the original subscription
-
+
conn1 = cf.createConnection();
conn1.setClientID(CLIENT_ID1);
sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
+
sess1.unsubscribe("mySubscription");
}
finally
@@ -3589,7 +3578,8 @@
fail();
}
catch (JMSException e)
- {}
+ {
+ }
}
finally
@@ -3604,146 +3594,146 @@
public void testRedeliveredDifferentSessions() throws Exception
{
Connection producerConnection = null;
-
+
Connection consumerConnection = null;
-
+
try
{
- producerConnection = cf.createConnection();
-
- consumerConnection = cf.createConnection();
-
- Session sessProducer = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- MessageProducer prod = sessProducer.createProducer(queue1);
- TextMessage tm = sessProducer.createTextMessage("testRedeliveredDifferentSessions");
- prod.send(tm);
-
- consumerConnection.start();
-
- Session sess1 = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageConsumer cons1 = sess1.createConsumer(queue1);
- TextMessage tm2 = (TextMessage)cons1.receive(3000);
-
- assertNotNull(tm2);
- assertEquals("testRedeliveredDifferentSessions", tm2.getText());
-
- //don't acknowledge it
- sess1.close();
-
- Session sess2 = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
- MessageConsumer cons2 = sess2.createConsumer(queue1);
- TextMessage tm3 = (TextMessage)cons2.receive(3000);
-
- assertNotNull(tm3);
- assertEquals("testRedeliveredDifferentSessions", tm3.getText());
-
- assertTrue(tm3.getJMSRedelivered());
+ producerConnection = cf.createConnection();
+
+ consumerConnection = cf.createConnection();
+
+ Session sessProducer = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer prod = sessProducer.createProducer(queue1);
+ TextMessage tm = sessProducer.createTextMessage("testRedeliveredDifferentSessions");
+ prod.send(tm);
+
+ consumerConnection.start();
+
+ Session sess1 = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ MessageConsumer cons1 = sess1.createConsumer(queue1);
+ TextMessage tm2 = (TextMessage) cons1.receive(3000);
+
+ assertNotNull(tm2);
+ assertEquals("testRedeliveredDifferentSessions", tm2.getText());
+
+ //don't acknowledge it
+ sess1.close();
+
+ Session sess2 = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+ MessageConsumer cons2 = sess2.createConsumer(queue1);
+ TextMessage tm3 = (TextMessage) cons2.receive(3000);
+
+ assertNotNull(tm3);
+ assertEquals("testRedeliveredDifferentSessions", tm3.getText());
+
+ assertTrue(tm3.getJMSRedelivered());
}
finally
{
- if (producerConnection != null)
- {
- producerConnection.close();
- }
- if (consumerConnection != null)
- {
- consumerConnection.close();
- }
- removeAllMessages(queue1.getQueueName(), true, 0);
+ if (producerConnection != null)
+ {
+ producerConnection.close();
+ }
+ if (consumerConnection != null)
+ {
+ consumerConnection.close();
+ }
+ removeAllMessages(queue1.getQueueName(), true, 0);
}
}
public void testRedelMessageListener1() throws Exception
{
Connection conn = null;
-
+
try
- {
- conn = cf.createConnection();
-
- conn.start();
-
- Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
-
- MessageConsumer cons = sess.createConsumer(queue1);
-
- RedelMessageListenerImpl listener = new RedelMessageListenerImpl(false);
- listener.sess = sess;
-
- cons.setMessageListener(listener);
-
- MessageProducer prod = sess.createProducer(queue1);
- prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- TextMessage m1 = sess.createTextMessage("a");
- TextMessage m2 = sess.createTextMessage("b");
- TextMessage m3 = sess.createTextMessage("c");
-
- prod.send(m1);
- prod.send(m2);
- prod.send(m3);
-
- listener.waitForMessages();
-
- conn.close();
- conn = null;
-
- assertFalse(listener.failed);
+ {
+ conn = cf.createConnection();
+
+ conn.start();
+
+ Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ MessageConsumer cons = sess.createConsumer(queue1);
+
+ RedelMessageListenerImpl listener = new RedelMessageListenerImpl(false);
+ listener.sess = sess;
+
+ cons.setMessageListener(listener);
+
+ MessageProducer prod = sess.createProducer(queue1);
+ prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ TextMessage m1 = sess.createTextMessage("a");
+ TextMessage m2 = sess.createTextMessage("b");
+ TextMessage m3 = sess.createTextMessage("c");
+
+ prod.send(m1);
+ prod.send(m2);
+ prod.send(m3);
+
+ listener.waitForMessages();
+
+ conn.close();
+ conn = null;
+
+ assertFalse(listener.failed);
}
finally
{
- if (conn != null)
- {
- conn.close();
- }
+ if (conn != null)
+ {
+ conn.close();
+ }
}
}
public void testRedelMessageListener2() throws Exception
{
- Connection conn = null;
-
+ Connection conn = null;
+
try
- {
- conn = cf.createConnection();
+ {
+ conn = cf.createConnection();
- conn.start();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
-
- MessageConsumer cons = sess.createConsumer(queue1);
-
- RedelMessageListenerImpl listener = new RedelMessageListenerImpl(true);
- listener.sess = sess;
-
- cons.setMessageListener(listener);
-
- MessageProducer prod = sessSend.createProducer(queue1);
- TextMessage m1 = sess.createTextMessage("a");
- TextMessage m2 = sess.createTextMessage("b");
- TextMessage m3 = sess.createTextMessage("c");
-
- prod.send(m1);
- prod.send(m2);
- prod.send(m3);
-
- listener.waitForMessages();
-
- log.debug(listener.messageOrder);
- assertFalse(listener.messageOrder, listener.failed);
-
- conn.close();
-
- conn = null;
+ conn.start();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+
+ MessageConsumer cons = sess.createConsumer(queue1);
+
+ RedelMessageListenerImpl listener = new RedelMessageListenerImpl(true);
+ listener.sess = sess;
+
+ cons.setMessageListener(listener);
+
+ MessageProducer prod = sessSend.createProducer(queue1);
+ TextMessage m1 = sess.createTextMessage("a");
+ TextMessage m2 = sess.createTextMessage("b");
+ TextMessage m3 = sess.createTextMessage("c");
+
+ prod.send(m1);
+ prod.send(m2);
+ prod.send(m3);
+
+ listener.waitForMessages();
+
+ log.debug(listener.messageOrder);
+ assertFalse(listener.messageOrder, listener.failed);
+
+ conn.close();
+
+ conn = null;
}
finally
{
- if (conn != null)
- {
- conn.close();
- }
+ if (conn != null)
+ {
+ conn.close();
+ }
}
}
@@ -3843,102 +3833,101 @@
public void testExceptionMessageListener3() throws Exception
{
- Connection conn = null;
-
+ Connection conn = null;
+
try
- {
- conn = cf.createConnection();
+ {
+ conn = cf.createConnection();
- conn.start();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
-
- MessageConsumer cons = sess.createConsumer(queue1);
-
- ExceptionRedelMessageListenerImpl listener = new ExceptionRedelMessageListenerImpl(sess);
-
- cons.setMessageListener(listener);
-
- MessageProducer prod = sessSend.createProducer(queue1);
- TextMessage m1 = sess.createTextMessage("a");
- TextMessage m2 = sess.createTextMessage("b");
- TextMessage m3 = sess.createTextMessage("c");
-
- prod.send(m1);
- prod.send(m2);
- prod.send(m3);
-
- listener.waitForMessages();
-
- assertFalse(listener.failed);
-
- conn.close();
-
- conn = null;
-
+ conn.start();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+
+ MessageConsumer cons = sess.createConsumer(queue1);
+
+ ExceptionRedelMessageListenerImpl listener = new ExceptionRedelMessageListenerImpl(sess);
+
+ cons.setMessageListener(listener);
+
+ MessageProducer prod = sessSend.createProducer(queue1);
+ TextMessage m1 = sess.createTextMessage("a");
+ TextMessage m2 = sess.createTextMessage("b");
+ TextMessage m3 = sess.createTextMessage("c");
+
+ prod.send(m1);
+ prod.send(m2);
+ prod.send(m3);
+
+ listener.waitForMessages();
+
+ assertFalse(listener.failed);
+
+ conn.close();
+
+ conn = null;
+
}
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
}
public void testExceptionMessageListener4() throws Exception
{
- Connection conn = null;
-
+ Connection conn = null;
+
try
- {
- conn = cf.createConnection();
+ {
+ conn = cf.createConnection();
- conn.start();
-
- Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
-
- MessageConsumer cons = sess.createConsumer(queue1);
-
- ExceptionRedelMessageListenerImpl listener = new ExceptionRedelMessageListenerImpl(sess);
-
- cons.setMessageListener(listener);
-
- MessageProducer prod = sessSend.createProducer(queue1);
- TextMessage m1 = sess.createTextMessage("a");
- TextMessage m2 = sess.createTextMessage("b");
- TextMessage m3 = sess.createTextMessage("c");
-
- prod.send(m1);
- prod.send(m2);
- prod.send(m3);
-
- listener.waitForMessages();
-
- assertFalse(listener.failed);
-
- conn.close();
-
- conn = null;
-
+ conn.start();
+
+ Session sessSend = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ MessageConsumer cons = sess.createConsumer(queue1);
+
+ ExceptionRedelMessageListenerImpl listener = new ExceptionRedelMessageListenerImpl(sess);
+
+ cons.setMessageListener(listener);
+
+ MessageProducer prod = sessSend.createProducer(queue1);
+ TextMessage m1 = sess.createTextMessage("a");
+ TextMessage m2 = sess.createTextMessage("b");
+ TextMessage m3 = sess.createTextMessage("c");
+
+ prod.send(m1);
+ prod.send(m2);
+ prod.send(m3);
+
+ listener.waitForMessages();
+
+ assertFalse(listener.failed);
+
+ conn.close();
+
+ conn = null;
+
}
- finally
- {
- if (conn != null)
- {
- conn.close();
- }
-
- removeAllMessages(queue1.getQueueName(), true, 0);
- }
+ finally
+ {
+ if (conn != null)
+ {
+ conn.close();
+ }
+
+ removeAllMessages(queue1.getQueueName(), true, 0);
+ }
}
-
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
@@ -3956,14 +3945,14 @@
private Session sess;
private boolean failed;
-
+
String message = "ok";
-
+
private void failed(String msg)
{
- log.warn(msg);
- failed = true;
- this.message = msg;
+ log.warn(msg);
+ failed = true;
+ this.message = msg;
}
public void waitForMessages() throws InterruptedException
@@ -3978,11 +3967,12 @@
public void onMessage(Message m)
{
- TextMessage tm = (TextMessage)m;
+ TextMessage tm = (TextMessage) m;
count++;
try
- {;
+ {
+ ;
if (count == 1)
{
if (!("a".equals(tm.getText())))
@@ -3999,12 +3989,12 @@
//Message should be immediately redelivered
if (!("a".equals(tm.getText())))
{
- failed("Should be a but was " + tm.getText());
+ failed("Should be a but was " + tm.getText());
latch.countDown();
}
if (!tm.getJMSRedelivered())
{
- failed("Message was supposed to be a redelivery");
+ failed("Message was supposed to be a redelivery");
latch.countDown();
}
}
@@ -4053,15 +4043,15 @@
else
{
//Shouldn't get a 4th messge
- failed("Shouldn't get a 4th message");
+ failed("Shouldn't get a 4th message");
latch.countDown();
}
}
}
catch (JMSException e)
{
- log.error(e.getMessage(), e);
- failed("Got a JMSException " + e.toString());
+ log.error(e.getMessage(), e);
+ failed("Got a JMSException " + e.toString());
latch.countDown();
}
}
@@ -4085,7 +4075,9 @@
this.transacted = transacted;
}
- /** Blocks the calling thread until at least a message is received */
+ /**
+ * Blocks the calling thread until at least a message is received
+ */
public void waitForMessages() throws InterruptedException
{
latch.await();
@@ -4095,7 +4087,7 @@
{
try
{
- TextMessage tm = (TextMessage)m;
+ TextMessage tm = (TextMessage) m;
log.trace("Got message:" + tm.getText() + " count is " + count);
@@ -4181,7 +4173,9 @@
private List messages = Collections.synchronizedList(new ArrayList());
private CountDownLatch latch = new CountDownLatch(1);
- /** Blocks the calling thread until at least a message is received */
+ /**
+ * Blocks the calling thread until at least a message is received
+ */
public void waitForMessages() throws InterruptedException
{
latch.await();
@@ -4203,8 +4197,10 @@
log.trace("Added message " + m + " to my list");
latch.countDown();
- };
+ }
+ ;
+
public Message getNextMessage()
{
Iterator i = messages.iterator();
@@ -4212,7 +4208,7 @@
{
return null;
}
- Message m = (Message)i.next();
+ Message m = (Message) i.next();
i.remove();
return m;
}
Added: trunk/tests/src/org/jboss/messaging/tests/unit/core/remoting/impl/RemotingConnectionTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/remoting/impl/RemotingConnectionTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/remoting/impl/RemotingConnectionTest.java 2008-06-10 15:23:36 UTC (rev 4424)
@@ -0,0 +1,588 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.messaging.tests.unit.core.remoting.impl;
+
+import junit.framework.TestCase;
+import org.easymock.EasyMock;
+import org.jboss.messaging.core.client.ConnectionParams;
+import org.jboss.messaging.core.client.Location;
+import org.jboss.messaging.core.client.RemotingSessionListener;
+import org.jboss.messaging.core.client.impl.ConnectionParamsImpl;
+import org.jboss.messaging.core.exception.MessagingException;
+import org.jboss.messaging.core.remoting.*;
+import org.jboss.messaging.core.remoting.impl.RemotingConnectionImpl;
+
+/**
+ * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ */
+public class RemotingConnectionTest extends TestCase
+{
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+ ConnectorRegistryFactory.setRegisteryLocator(null);
+ }
+
+ public void testConnectionStarted() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ }
+
+ public void testConnectionStartedAndStopped() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.expect(connectorRegistry.removeConnector(location)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.expect(connector.disconnect()).andReturn(true);
+ EasyMock.replay(connector);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.stop();
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ assertEquals(-1, remotingConnection.getSessionID());
+
+ }
+
+ public void testConnectionGetSessionId() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+ EasyMock.expect(nioSession.isConnected()).andReturn(true);
+ EasyMock.expect(nioSession.getID()).andReturn((123l));
+ EasyMock.replay(nioSession);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ assertEquals(123l, remotingConnection.getSessionID());
+
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(nioSession);
+ }
+
+ public void testConnectionGetSessionIdDisconnected() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+ EasyMock.expect(nioSession.isConnected()).andReturn(false);
+ //EasyMock.expect(nioSession.getID()).andReturn((123l));
+ EasyMock.replay(nioSession);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ assertEquals(-1, remotingConnection.getSessionID());
+
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(nioSession);
+ }
+
+ public void testConnectionGetSessionIdStopped() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+ EasyMock.expect(nioSession.isConnected()).andReturn(true);
+ EasyMock.expect(nioSession.getID()).andReturn((123l));
+ EasyMock.replay(nioSession);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.stop();
+ assertEquals(123l, remotingConnection.getSessionID());
+
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(nioSession);
+ }
+
+ public void testConnectionSendBlocking() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = new ConnectionParamsImpl();
+ connectionParams.setTimeout(1000);
+ DummyDispatcher dispatcher = new DummyDispatcher();
+ DummySession nioSession = new DummySession(dispatcher, 0, null, false);
+ PacketHandler handler = null;
+
+ Packet packet = EasyMock.createStrictMock(Packet.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.replay(connector);
+ packet.setTargetID(1);
+ packet.setExecutorID(2);
+ packet.setResponseTargetID(0);
+ EasyMock.replay(packet);
+
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.sendBlocking(1, 2, packet);
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(packet);
+ assertNotNull(nioSession.getPacketDispatched());
+ }
+
+ public void testConnectionSendBlockingWithTimeout() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = new ConnectionParamsImpl();
+ connectionParams.setTimeout(1000);
+ DummyDispatcher dispatcher = new DummyDispatcher();
+ DummySession nioSession = new DummySession(dispatcher, 2000, null, false);
+ PacketHandler handler = null;
+
+ Packet packet = EasyMock.createStrictMock(Packet.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.replay(connector);
+ packet.setTargetID(1);
+ packet.setExecutorID(2);
+ packet.setResponseTargetID(0);
+ EasyMock.replay(packet);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ try
+ {
+ remotingConnection.sendBlocking(1, 2, packet);
+ fail("should have timed out");
+ }
+ catch (IllegalStateException e)
+ {
+ //pass
+ }
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(packet);
+ assertNull(nioSession.getPacketDispatched());
+ }
+
+ public void testConnectionSendBlockingErrorOnWrite() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = new ConnectionParamsImpl();
+ connectionParams.setTimeout(1000);
+ DummyDispatcher dispatcher = new DummyDispatcher();
+ DummySession nioSession = new DummySession(dispatcher, 0, new Exception(), false);
+ PacketHandler handler = null;
+
+ Packet packet = EasyMock.createStrictMock(Packet.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.expect(connector.getDispatcher()).andReturn(dispatcher);
+ EasyMock.replay(connector);
+ packet.setTargetID(1);
+ packet.setExecutorID(2);
+ packet.setResponseTargetID(0);
+ EasyMock.replay(packet);
+
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ try
+ {
+ remotingConnection.sendBlocking(1, 2, packet);
+ fail("should throw exception");
+ }
+ catch (MessagingException e)
+ {
+ //pass
+ }
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(packet);
+ assertNull(nioSession.getPacketDispatched());
+ }
+
+ public void testConnectionSendOneWay() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = new ConnectionParamsImpl();
+ connectionParams.setTimeout(1000);
+ DummyDispatcher dispatcher = new DummyDispatcher();
+ DummySession nioSession = new DummySession(dispatcher, 0, null, true);
+ PacketHandler handler = null;
+
+ Packet packet = EasyMock.createStrictMock(Packet.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+ packet.setTargetID(1);
+ packet.setExecutorID(2);
+ EasyMock.replay(packet);
+
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.sendOneWay(1, 2, packet);
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(packet);
+ assertNull(nioSession.getPacketDispatched());
+ }
+
+ public void testConnectionSendOneWayErrorOnWrite() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = new ConnectionParamsImpl();
+ connectionParams.setTimeout(1000);
+ DummyDispatcher dispatcher = new DummyDispatcher();
+ DummySession nioSession = new DummySession(dispatcher, 0, new Exception(), true);
+ PacketHandler handler = null;
+
+ Packet packet = EasyMock.createStrictMock(Packet.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ EasyMock.replay(connector);
+ packet.setTargetID(1);
+ packet.setExecutorID(2);
+ EasyMock.replay(packet);
+
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ try
+ {
+ remotingConnection.sendOneWay(1, 2, packet);
+ fail("should throw exception");
+ }
+ catch (MessagingException e)
+ {
+ //pass
+ }
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ EasyMock.verify(packet);
+ assertNull(nioSession.getPacketDispatched());
+ }
+
+ public void testConnectionSetListener() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+ RemotingSessionListener listener = EasyMock.createNiceMock(RemotingSessionListener.class);
+
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ connector.addSessionListener(listener);
+ EasyMock.replay(connector);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.setRemotingSessionListener(listener);
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ }
+
+ public void testConnectionReSetListener() throws Throwable
+ {
+ final ConnectorRegistry connectorRegistry = EasyMock.createStrictMock(ConnectorRegistry.class);
+ NIOConnector connector = EasyMock.createStrictMock(NIOConnector.class);
+ ConnectorRegistryFactory.setRegisteryLocator(new ConnectorRegistryLocator()
+ {
+ public ConnectorRegistry locate()
+ {
+ return connectorRegistry;
+ }
+ });
+ Location location = EasyMock.createNiceMock(Location.class);
+ ConnectionParams connectionParams = EasyMock.createNiceMock(ConnectionParams.class);
+ NIOSession nioSession = EasyMock.createStrictMock(NIOSession.class);
+ RemotingSessionListener listener = EasyMock.createNiceMock(RemotingSessionListener.class);
+ RemotingSessionListener listener2 = EasyMock.createNiceMock(RemotingSessionListener.class);
+ EasyMock.expect(connectorRegistry.getConnector(location, connectionParams)).andReturn(connector);
+ EasyMock.replay(connectorRegistry);
+ EasyMock.expect(connector.connect()).andReturn(nioSession);
+ connector.addSessionListener(listener);
+ connector.removeSessionListener(listener);
+ connector.addSessionListener(listener2);
+ EasyMock.replay(connector);
+
+ RemotingConnectionImpl remotingConnection = new RemotingConnectionImpl(location, connectionParams);
+ remotingConnection.start();
+ remotingConnection.setRemotingSessionListener(listener);
+ remotingConnection.setRemotingSessionListener(null);
+ remotingConnection.setRemotingSessionListener(listener2);
+ EasyMock.verify(connector);
+ EasyMock.verify(connectorRegistry);
+ }
+
+ class DummyDispatcher implements PacketDispatcher
+ {
+ PacketHandler handler = null;
+
+ public void register(PacketHandler handler)
+ {
+ this.handler = handler;
+ }
+
+ public void unregister(long handlerID)
+ {
+ //todo
+ }
+
+ public void setListener(PacketHandlerRegistrationListener listener)
+ {
+ //todo
+ }
+
+ public void dispatch(Packet packet, PacketReturner sender) throws Exception
+ {
+ handler.handle(packet, sender);
+ }
+
+ public void callFilters(Packet packet) throws Exception
+ {
+ //todo
+ }
+
+ public long generateID()
+ {
+ return 0;
+ }
+ }
+
+ class DummySession implements NIOSession
+ {
+ PacketDispatcher dispatcher;
+ Packet packetDispatched = null;
+ long timeToReply = 0;
+ Exception exceptionToThrow = null;
+ boolean oneWay = false;
+
+ public DummySession(PacketDispatcher dispatcher, long timeToReply, Exception toThrow, boolean oneWay)
+ {
+ this.dispatcher = dispatcher;
+ this.timeToReply = timeToReply;
+ exceptionToThrow = toThrow;
+ this.oneWay = oneWay;
+ }
+
+ public Packet getPacketDispatched()
+ {
+ return packetDispatched;
+ }
+
+ public long getID()
+ {
+ return 0;
+ }
+
+ public void write(final Packet packet) throws Exception
+ {
+ if (exceptionToThrow != null)
+ {
+ throw exceptionToThrow;
+ }
+ else if (!oneWay)
+ {
+ new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ Thread.sleep(timeToReply);
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+ packetDispatched = packet;
+ try
+ {
+ dispatcher.dispatch(packet, null);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }).start();
+ }
+
+
+ }
+
+ public boolean isConnected()
+ {
+ return true;
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list