Friday, June 12, 2015
Call RabbitMQ in Matlab
---------------------------
###Installation Environment
```
Ubuntu 12.04, 64-bit
Matlab R2012a, 64-bit
```
---------------------------
###Install Package
Install open JDK and check the version
```bash
sudo apt-get install openjdk-6-jdk
sudo apt-get install openjdk-6-jre
javac -version
java -version
```
---------------------------
###Code of Send.java and Recv.java
Send.java
```java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}
```
Recv.java
```java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody(),"UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
}
}
```
---------------------------
###Compile and run Send.java and Recv.java
```bash
wget rabbitmq-java-client-bin-3.5.3.zip
unzip rabbitmq-java-client-bin-3.5.3.zip
cp rabbitmq-java-client-bin-3.5.3/*.jar ./
javac -cp rabbitmq-client.jar Send.java Recv.java # This will produce Send.class and Recv.class
```
In one terminal
```bash
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send
[x] Sent 'Hello World!'
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send
[x] Sent 'Hello World!'
```
In another terminal
```bash
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Recv
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!'
[x] Received 'Hello World!'
```
---------------------------
### Run in Matlab
```matlab
>> javaaddpath('/path/to/Send.class','/path/to/Recv.class', ...
'/path/to/commons-io-1.2.jar', ...
'/path/to/commons-cli-1.1.jar', '/path/to/rabbitmq-client.jar');
>> clientSend = Send;
>> javaMethod('main', clientSend, '')
[x] Sent 'Hello World!'
>> javaMethod('main', clientSend, '')
[x] Sent 'Hello World!'
>> serverRecv = Recv;
>> javaMethod('main', serverRecv, '')
[x] Received 'Hello World!'
[x] Received 'Hello World!'
```
---------------------------
###Reference
https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment