Merge branch 'master' into bug23943

Conflicts:
	python/README.md
This commit is contained in:
Ann Witbrock 2011-04-21 15:07:31 +01:00
commit 490cba165c
15 changed files with 477 additions and 33 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
php/lib/php-amqplib
dotnet/*.exe
dotnet/lib
/java/ReceiveLog.java~

View File

@ -1,29 +1,84 @@
# Dotnet C# code for RabbitMQ tutorials
Here you can find a C# code examples from [RabbitMQ
Here you can find C# code examples for [RabbitMQ
tutorials](http://www.rabbitmq.com/getstarted.html).
You'll need erlang installed, and also access to a [RabbitMQ server](http://www.rabbitmq.com/server.html).
These are easy to [install](http://www.rabbitmq.com/install.html).
Here we present steps to run the examples using Mono on Linux.
## Requirements
### Mono on Linux
You need Mono and RabbitMQ dotnet client.
sudo apt-get install mono-devel
mkdir lib
cd lib
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.2.0/rabbitmq-dotnet-client-2.2.0-dotnet-3.0.zip
unzip rabbitmq-dotnet-client-2.2.0-dotnet-3.0.zip
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1/rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
unzip rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
cd ..
### Windows
You need the RabbitMQ dotnet client.
Go to http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1
Download rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
Extract it to rabbitmq-dotnet-client-2.1.1-dotnet-3.0 in your working folder
## Code
[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
For background, you can refer to [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
### Compile and run the C# examples using Mono on Linux.
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
MONO_PATH=lib/bin mono Send.exe
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
MONO_PATH=lib/bin mono Receive.exe
### Compile the C# examples on Windows
Ensure your system can find the c# compiler `csc.exe`
e.g. Add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your Path
If you put the whole client directory in your working directory:
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Send.cs
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Receive.cs
or, if you just copy the RabbitMQ.Client.dll client library to your working directory:
csc /r:"RabbitMQ.Client.dll" Send.cs
csc /r:"RabbitMQ.Client.dll" Receive.cs
or you could use MS Visual Studio.
### Run the example programs on Windows
Open 3 Command Prompt windows Start > Run... cmd
Use `rabbitmqctl status` to check the server is running,
and `rabbitmqctl list_queues` to inspect the queue.
In the other two windows, navigate to your working directory to run the example client programs.
In another cmd window, send a message:
Send.exe
Check queue identified as "hello" has 1 message.
In the final cmd window, set the listener going:
Receive.exe
This will keep listening (Ctrl-C in this window will stop it) for messages.
You should now see the first message, and the queue should be empty.
The Receive view should get any further messages you Send.

View File

@ -1,4 +1,3 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
@ -7,8 +6,7 @@ public class EmitLog {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv)
throws java.io.IOException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -19,9 +17,7 @@ public class EmitLog {
String message = getMessage(argv);
channel.basicPublish( EXCHANGE_NAME, "",
null,
message.getBytes());
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
@ -43,5 +39,5 @@ public class EmitLog {
}
return words.toString();
}
}

51
java/EmitLogDirect.java Normal file
View File

@ -0,0 +1,51 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class EmitLogDirect {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String severity = getSeverity(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
channel.close();
connection.close();
}
private static String getSeverity(String[] strings){
if (strings.length < 1)
return "info";
return strings[0];
}
private static String getMessage(String[] strings){
if (strings.length < 2)
return "Hello World!";
return joinStrings(strings, " ", 1);
}
private static String joinStrings(String[] strings, String delimiter, int startIndex) {
int length = strings.length;
if (length == 0 ) return "";
if (length < startIndex ) return "";
StringBuilder words = new StringBuilder(strings[startIndex]);
for (int i = startIndex + 1; i < length; i++) {
words.append(delimiter).append(strings[i]);
}
return words.toString();
}
}

51
java/EmitLogTopic.java Normal file
View File

@ -0,0 +1,51 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class EmitLogTopic {
private static final String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String routingKey = getRouting(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
channel.close();
connection.close();
}
private static String getRouting(String[] strings){
if (strings.length < 1)
return "anonymous.info";
return strings[0];
}
private static String getMessage(String[] strings){
if (strings.length < 2)
return "Hello World!";
return joinStrings(strings, " ", 1);
}
private static String joinStrings(String[] strings, String delimiter, int startIndex) {
int length = strings.length;
if (length == 0 ) return "";
if (length < startIndex ) return "";
StringBuilder words = new StringBuilder(strings[startIndex]);
for (int i = startIndex + 1; i < length; i++) {
words.append(delimiter).append(strings[i]);
}
return words.toString();
}
}

View File

@ -1,4 +1,3 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
@ -8,7 +7,7 @@ public class NewTask {
private static final String TASK_QUEUE_NAME = "task_queue";
public static void main(String[] argv) throws java.io.IOException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -43,4 +42,4 @@ public class NewTask {
}
return words.toString();
}
}
}

57
java/README.md Normal file
View File

@ -0,0 +1,57 @@
# Java code for RabbitMQ tutorials
Here you can find the Java code examples from [RabbitMQ
tutorials](http://www.rabbitmq.com/getstarted.html).
## Requirements
To run this code you need to download the RabbitMQ
[java client library package](http://www.rabbitmq.com/java-client.html),
and check its signature as described there.
Unzip it into your working directory and ensure the JAR files from the
unzipped directory are placed in your working directory:
$ unzip rabbitmq-java-client-bin-*.zip
$ cp rabbitmq-java-client-bin-*/*.jar ./
To compile you only need the Rabbitmq java client jar on the classpath.
To run them you'll need all the dependencies, see examples below.
Note: If you're on Windows,
use a semicolon instead of a colon to separate items in the classpath.
> You can set an environment variable for the jar files on the classpath e.g.
>
> $ export CP=.:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar
> $ java -cp $CP Send
>
> or on Windows:
>
> > set CP=.;commons-io-1.2.jar;commons-cli-1.1.jar;rabbitmq-client.jar
> > java -cp %CP% Send
## Code
[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-java.html):
$ javac -cp rabbitmq-client.jar Send.java Recv.java
$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send
$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Recv
[Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-java.html):
$ javac -cp rabbitmq-client.jar NewTask.java Worker.java
$ java -cp $CP NewTask
$ java -cp $CP Worker
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-java.html)
$ javac -cp rabbitmq-client.jar EmitLog.java ReceiveLogs.java
$ java -cp $CP EmitLog
$ java -cp $CP ReceiveLogs

81
java/RPCClient.java Normal file
View File

@ -0,0 +1,81 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;
import java.util.UUID;
public class RPCClient {
private Connection connection;
private Channel channel;
private String requestQueueName = "rpc_queue";
private String replyQueueName;
private QueueingConsumer consumer;
public RPCClient() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
replyQueueName = channel.queueDeclare().getQueue();
consumer = new QueueingConsumer(channel);
channel.basicConsume(replyQueueName, true, consumer);
}
public String call(String message) throws Exception {
String response = null;
String corrId = UUID.randomUUID().toString();
BasicProperties props = new BasicProperties();
props.setReplyTo(replyQueueName);
props.setCorrelationId(corrId);
channel.basicPublish("", requestQueueName, props, message.getBytes());
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
if (delivery.getProperties().getCorrelationId().equals(corrId)) {
response = new String(delivery.getBody(),"UTF-8");
break;
}
}
return response;
}
public void close() throws Exception {
connection.close();
}
public static void main(String[] argv) {
RPCClient fibonacciRpc = null;
String response = null;
try {
fibonacciRpc = new RPCClient();
System.out.println(" [x] Requesting fib(30)");
response = fibonacciRpc.call("30");
System.out.println(" [.] Got '" + response + "'");
System.out.println(" [x] Requesting fib(-1)");
response = fibonacciRpc.call("-1");
System.out.println(" [.] Got '" + response + "'");
System.out.println(" [x] Requesting fib(a)");
response = fibonacciRpc.call("a");
System.out.println(" [.] Got '" + response + "'");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (fibonacciRpc!= null) {
try {
fibonacciRpc.close();
}
catch (Exception ignore) {}
}
}
}
}

75
java/RPCServer.java Normal file
View File

@ -0,0 +1,75 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;
public class RPCServer {
private static final String RPC_QUEUE_NAME = "rpc_queue";
private static int fib(int n) {
if (n > 1) return fib(n-1) + fib(n-2);
else return n;
}
public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println(" [x] Awaiting RPC requests");
while (true) {
String response = null;
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new BasicProperties();
replyProps.setCorrelationId(props.getCorrelationId());
try {
String message = new String(delivery.getBody(),"UTF-8");
int n = Integer.parseInt(message);
System.out.println(" [.] fib(" + message + ")");
response = "" + fib(n);
}
catch (Exception e){
System.out.println(" [.] " + e.toString());
response = "";
}
finally {
channel.basicPublish( "", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}
}

View File

@ -1,16 +1,13 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLog {
public class ReceiveLogs {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -34,3 +31,4 @@ public class ReceiveLog {
}
}
}

View File

@ -0,0 +1,43 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLogsDirect {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
if (argv.length < 1){
System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
System.exit(1);
}
for(String severity : argv){
channel.queueBind(queueName, EXCHANGE_NAME, severity);
}
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
}
}
}

View File

@ -0,0 +1,43 @@
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLogsTopic {
private static final String EXCHANGE_NAME = "topic_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String queueName = channel.queueDeclare().getQueue();
if (argv.length < 1){
System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
System.exit(1);
}
for(String bindingKey : argv){
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
}
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
System.out.println(" [x] Received '" + routingKey + "':'" + message + "'");
}
}
}

View File

@ -1,4 +1,3 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
@ -8,9 +7,7 @@ public class Recv {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -29,4 +26,4 @@ public class Recv {
System.out.println(" [x] Received '" + message + "'");
}
}
}
}

View File

@ -1,4 +1,3 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
@ -7,8 +6,7 @@ public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv)
throws java.io.IOException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -23,4 +21,4 @@ public class Send {
channel.close();
connection.close();
}
}
}

View File

@ -1,4 +1,3 @@
import java.io.IOException;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
@ -8,9 +7,7 @@ public class Worker {
private static final String TASK_QUEUE_NAME = "task_queue";
public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
@ -43,3 +40,4 @@ public class Worker {
}
}
}