Skip to content

Commit b94b3a2

Browse files
author
Simon MacMullen
committed
Merge default into bug22964.
2 parents ae85f5a + f12188a commit b94b3a2

23 files changed

+875
-150
lines changed

README-EXAMPLES

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Overview
2+
========
3+
4+
A number of examples are packaged with the source distribution and
5+
with the tests jar in the binary distribution. These examples vary in
6+
complexity from simple clients and servers through to complex test
7+
harnesses that are used for testing the RabbitMQ server.
8+
9+
The source code for the examples can be found in the
10+
test/src/com/rabbitmq/examples directory of the source distribution.
11+
12+
13+
Running the Examples
14+
====================
15+
16+
The script runjava.sh or runjava.bat can be found in the top level
17+
directory of the binary distribution or in the scripts directory of
18+
the source distribution.
19+
20+
This script can be used to run the examples. For instance, if you are
21+
using the binary distribution and if you have a RabbitMQ broker
22+
running and you open two shell or console windows you can test basic
23+
connectivity like this:
24+
25+
shell-1$ ./runjava.sh com.rabbitmq.examples.SimpleConsumer
26+
27+
shell-2$ ./runjava.sh com.rabbitmq.examples.SimpleProducer
28+
29+
shell-1$ ./runjava.sh com.rabbitmq.examples.SimpleConsumer
30+
Message: the time is Fri Feb 26 08:28:58 GMT 2010
31+
32+
Running the examples from the source distribution requries two more
33+
preliminary steps:
34+
35+
shell-1$ ant dist
36+
37+
shell-1$ cd build/dist/
38+
39+
Now, the examples can be run exactly as detailed above.
40+
41+
The more complex examples will require additional command line
42+
arguments and it is best to examine the source of the examples to
43+
determine what arguments are available.
44+
45+
All examples live in the com.rabbitmq.examples package, so just add
46+
that package name to the examples described below to produce the full
47+
classname that is required as the first argument to the runjava
48+
script.
49+
50+
51+
Simple Examples
52+
===============
53+
54+
SimpleProducer - Sends a message to an exchange.
55+
SimpleConsumer - Reads a message from a queue.
56+
SimpleTopicProducer - Sends a message to a topic exchange.
57+
SimpleTopicConsumer - Reads a message from a topic queue.
58+
SendString - Send a string to an exchange.
59+
FileProducer - Sends a file to an exchange.
60+
FileConsumer - Reads a file from a queue.
61+
HelloClient - Performs a simple RPC call over AMQP.
62+
HelloServer - Acts as an RPC server over AMQP.
63+
HelloJsonClient - Performs a simple JSON-RPC call over AMQP.
64+
HelloJsonServer - Acts as a JSON-RPC server over AMQP.
65+
LogTail - Tails the server logs.
66+
SendString - Sends a user supplied message over AMQP.
67+
68+
69+
More Complex Examples
70+
=====================
71+
72+
Most of these examples are used in testing and tuning the RabbitMQ
73+
server.
74+
75+
BufferPerformanceMetrics - Tests buffer sizing.
76+
ChannelCreationPerformance - Test different Channel creation strategies.
77+
ConsumerMain - Measures performance of Consumers.
78+
ManyConnections - Tests many connections.
79+
MulticastMain - Measures performance of multiple consumers and producers.
80+
ProducerMain - Measures performance of Producers.
81+
SpammyTopicProducer - Sends lots of different topic messages over AMQP.
82+
StressPersister - Stress test for persister.
83+
TestMain - RabbitMQ server regression tests.
84+
TracerConcurrencyTest - Tests correct concurrency behaviour of RabbitMQ tracer.

README-TESTS

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Overview
2+
========
3+
4+
There are multiple test suites in the RabbitMQ Java client library;
5+
the source for all of the suites can be found in the test/src
6+
directory.
7+
8+
The suites are:
9+
10+
Client tests
11+
Functional tests
12+
Server tests
13+
SSL tests
14+
15+
All the tests require a conforming server listening on localhost:5672
16+
(the default settings). For details on running specific tests, see
17+
below.
18+
19+
All the test suites can be run with the command ant test-suite. To run
20+
specific test suites see the instructions below.
21+
22+
23+
Running a Specific Test Suite
24+
=============================
25+
26+
To run a specific test suite, but not the others, you should execute
27+
one of the following in the top-level directory of the source tree:
28+
29+
ant test-client
30+
ant test-functional
31+
ant test-server
32+
ant test-ssl
33+
34+
Note that to run the SSL tests, the RabbitMQ server should be
35+
configured to use SSL as per the SSL with RabbitMQ guide.
36+
37+
The server tests are meant to test a RabbitMQ broker. Broadly, they
38+
require a running RabbitMQ broker on the default host/port, the source
39+
code for rabbitmq-server in ../rabbitmq-server, and a test directory
40+
../rabbitmq-test. Running them manually is a tricky business.
41+
42+
For example, to run the client tests:
43+
44+
----------------- Example shell session -------------------------------------
45+
rabbitmq-java-client$ ant test-client
46+
Buildfile: build.xml
47+
48+
test-prepare:
49+
50+
test-build:
51+
52+
amqp-generate-check:
53+
54+
amqp-generate:
55+
56+
build:
57+
58+
test-build-param:
59+
60+
test-client:
61+
[junit] Running com.rabbitmq.client.test.ClientTests
62+
[junit] Tests run: 31, Failures: 0, Errors: 0, Time elapsed: 2.388 sec
63+
64+
BUILD SUCCESSFUL
65+
-----------------------------------------------------------------------------
66+
67+
If any tests are broken details can be found by viewing this file:
68+
build/TEST-com.rabbitmq.client.test.ClientTests.txt

build.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<pathelement path="${test.javac.out}"/>
2727
</path>
2828

29-
<property name="AMQP_SPEC_JSON_PATH" value="${codegen.dir}/amqp-${spec.version}.json ${codegen.dir}/rabbitmq-0.8-extensions.json"/>
29+
<property name="AMQP_SPEC_JSON_PATH" value="${codegen.dir}/amqp-rabbitmq-${spec.version}.json"/>
3030

3131
<target name="amqp-generate-check" description="check if codegen needs to be run">
3232
<uptodate property="amqp.generate.notRequired">
@@ -77,7 +77,7 @@
7777
</fail>
7878
</target>
7979

80-
<target name="build" depends="amqp-generate">
80+
<target name="build" depends="amqp-generate" description="Build the client library.">
8181
<mkdir dir="${javac.out}"/>
8282
<copy file="src/com/rabbitmq/client/impl/ClientVersion.java.in"
8383
tofile="${src.generated}/com/rabbitmq/client/impl/ClientVersion.java">
@@ -116,6 +116,10 @@
116116
<property name="SSL_P12_PASSWORD" value="${env.PASSWORD}"/>
117117
</target>
118118

119+
<target name="detect-umbrella">
120+
<available property="UMBRELLA_AVAILABLE" file="../rabbitmq-test"/>
121+
</target>
122+
119123
<target name="detect-tmpdir">
120124
<property environment="env"/>
121125
<condition property="TMPDIR" value="${env.TMPDIR}" else="/tmp">
@@ -317,7 +321,7 @@
317321
<property name="haltOnFailureJava" value="false" />
318322
</target>
319323

320-
<target name="test-suite" depends="test-suite-prepare, test-suite-run">
324+
<target name="test-suite" depends="test-suite-prepare, test-suite-run" description="Run all test suites.">
321325
<fail message="Errors occured in tests">
322326
<condition>
323327
<not>
@@ -329,7 +333,7 @@
329333

330334
<target name="test-suite-run" depends="test-client, test-ssl, test-server, test-functional, test-main-silent"/>
331335

332-
<target name="test-client" depends="test-build">
336+
<target name="test-client" depends="test-build" description="Run the client test suites.">
333337
<junit printSummary="withOutAndErr"
334338
haltOnFailure="${haltOnFailureJunit}"
335339
failureproperty="test.failure"
@@ -362,7 +366,7 @@
362366
<antcall target="remove-client-keystore"/>
363367
</target>
364368

365-
<target name="test-functional" depends="test-build">
369+
<target name="test-functional" depends="test-build" description="Run the functional test suite.">
366370
<junit printSummary="withOutAndErr"
367371
haltOnFailure="${haltOnFailureJunit}"
368372
failureproperty="test.failure"
@@ -375,7 +379,7 @@
375379
</junit>
376380
</target>
377381

378-
<target name="test-server" depends="test-build">
382+
<target name="test-server" depends="detect-umbrella, test-build" if="UMBRELLA_AVAILABLE">
379383
<junit printSummary="withOutAndErr"
380384
haltOnFailure="${haltOnFailureJunit}"
381385
failureproperty="test.failure"

src/com/rabbitmq/client/Channel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, b
457457
*/
458458
void basicAck(long deliveryTag, boolean multiple) throws IOException;
459459

460+
/**
461+
* Reject a message. Supply the deliveryTag from the {@link com.rabbitmq.client.AMQP.Basic.GetOk}
462+
* or {@link com.rabbitmq.client.AMQP.Basic.Deliver} method
463+
* containing the received message being rejected.
464+
* @see com.rabbitmq.client.AMQP.Basic.Reject
465+
* @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}
466+
* @param requeue true if the rejected message should be requeued rather than discarded/dead-lettered
467+
* @throws java.io.IOException if an error is encountered
468+
*/
469+
void basicReject(long deliveryTag, boolean requeue) throws IOException;
470+
460471
/**
461472
* Start a non-nolocal, non-exclusive consumer, with
462473
* explicit acknowledgements required and a server-generated consumerTag.

src/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ protected FrameHandler createFrameHandler(Socket sock)
346346
}
347347

348348
/**
349-
* Provides a hook to insert custom configuration of the sockets used
350-
* to connect to an AMQP server before they connect.
349+
* Provides a hook to insert custom configuration of the sockets
350+
* used to connect to an AMQP server before they connect.
351351
*
352-
* The default behaviour of this method is to disable Nagle's algorithm to get
353-
* more consistently low latency.
354-
* However it may be overridden freely and there is no requirement to retain
355-
* this behaviour.
352+
* The default behaviour of this method is to disable Nagle's
353+
* algorithm to get more consistently low latency. However it
354+
* may be overridden freely and there is no requirement to retain
355+
* this behaviour.
356356
*
357357
* @param socket The socket that is to be used for the Connection
358358
*/

src/com/rabbitmq/client/DefaultConsumer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class DefaultConsumer implements Consumer {
4545

4646
/**
4747
* Constructs a new instance and records its association to the passed-in channel.
48-
* @param channel the channel on which the
48+
* @param channel the channel to which this consumer is attached
4949
*/
5050
public DefaultConsumer(Channel channel) {
5151
_channel = channel;

src/com/rabbitmq/client/QueueingConsumer.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,37 @@
4040
import com.rabbitmq.utility.Utility;
4141

4242
/**
43-
* Convenience class: an implementation of {@link Consumer} with straightforward blocking semantics
43+
* Convenience class: an implementation of {@link Consumer} with
44+
* straightforward blocking semantics.
45+
*
46+
* The general pattern for using QueueingConsumer is as follows:
47+
*
48+
* <pre>
49+
* // Create connection and channel.
50+
* {@link ConnectionFactory} factory = new ConnectionFactory();
51+
* Connection conn = factory.newConnection();
52+
* {@link Channel} ch1 = conn.createChannel();
53+
*
54+
* // Declare a queue and bind it to an exchange.
55+
* String queueName = ch1.queueDeclare().{@link AMQP.Queue.DeclareOk#getQueue getQueue}();
56+
* ch1.{@link Channel#queueBind queueBind}(queueName, exchangeName, queueName);
57+
*
58+
* // Create the QueueingConsumer and have it consume from the queue
59+
* QueueingConsumer consumer = new {@link QueueingConsumer#QueueingConsumer QueueingConsumer}(ch1);
60+
* ch1.{@link Channel#basicConsume basicConsume}(queueName, false, consumer);
61+
*
62+
* // Process deliveries
63+
* while (/* some condition * /) {
64+
* {@link QueueingConsumer.Delivery} delivery = consumer.{@link QueueingConsumer#nextDelivery nextDelivery}();
65+
* // process delivery
66+
* ch1.{@link Channel#basicAck basicAck}(delivery.{@link QueueingConsumer.Delivery#getEnvelope getEnvelope}().{@link Envelope#getDeliveryTag getDeliveryTag}(), false);
67+
* }
68+
* </pre>
69+
*
70+
* <p>For a more detailed explanation, see <a href="http://www.rabbitmq.com/api-guide.html#consuming">the java api guide</a>.</p>
71+
*
72+
* <p>For a more complete example, see LogTail in the test/src/com/rabbitmq/examples
73+
* directory of the source distribution.</p>
4474
*/
4575
public class QueueingConsumer extends DefaultConsumer {
4676
private final BlockingQueue<Delivery> _queue;

src/com/rabbitmq/client/impl/ChannelN.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ public void basicAck(long deliveryTag, boolean multiple)
646646
transmit(new Basic.Ack(deliveryTag, multiple));
647647
}
648648

649+
/** Public API - {@inheritDoc} */
650+
public void basicReject(long deliveryTag, boolean requeue)
651+
throws IOException
652+
{
653+
transmit(new Basic.Reject(deliveryTag, requeue));
654+
}
655+
649656
/** Public API - {@inheritDoc} */
650657
public String basicConsume(String queue, Consumer callback)
651658
throws IOException

test/src/com/rabbitmq/client/test/BrokerTestCase.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import com.rabbitmq.client.GetResponse;
4343
import com.rabbitmq.client.MessageProperties;
4444
import com.rabbitmq.client.ShutdownSignalException;
45-
45+
import com.rabbitmq.client.AlreadyClosedException;
46+
import com.rabbitmq.client.impl.ShutdownNotifierComponent;
4647
import com.rabbitmq.client.AMQP;
4748

4849
public class BrokerTestCase extends TestCase
@@ -128,6 +129,16 @@ public void closeChannel()
128129

129130
public void checkShutdownSignal(int expectedCode, IOException ioe) {
130131
ShutdownSignalException sse = (ShutdownSignalException) ioe.getCause();
132+
checkShutdownSignal(expectedCode, sse);
133+
}
134+
135+
public void checkShutdownSignal(int expectedCode, AlreadyClosedException ace) {
136+
ShutdownNotifierComponent snc = (ShutdownNotifierComponent) ace.getReference();
137+
ShutdownSignalException sse = snc.getCloseReason();
138+
checkShutdownSignal(expectedCode, sse);
139+
}
140+
141+
public void checkShutdownSignal(int expectedCode, ShutdownSignalException sse) {
131142
Command closeCommand = (Command) sse.getReason();
132143
channel = null;
133144
if (sse.isHardError()) {
@@ -140,6 +151,20 @@ public void checkShutdownSignal(int expectedCode, IOException ioe) {
140151
}
141152
}
142153

154+
public void expectChannelError(int error) {
155+
try {
156+
channel.basicQos(0);
157+
fail("Expected channel error " + error);
158+
} catch (IOException ioe) {
159+
// If we get a channel close back when flushing it with the
160+
// synchronous basicQos above.
161+
checkShutdownSignal(error, ioe);
162+
} catch (AlreadyClosedException ace) {
163+
// If it has already closed of its own accord before we got there.
164+
checkShutdownSignal(error, ace);
165+
}
166+
}
167+
143168
protected void assertDelivered(String q, int count) throws IOException {
144169
assertDelivered(q, count, false);
145170
}

0 commit comments

Comments
 (0)