I have a simple service written with Python using Nameko based on RabbitMQ
and RPC.
Here is the code of service.py
from nameko.rpc import rpc, RpcProxy
class Crawler(object):
name = "crawler"
@rpc
def scrapit(self):
return 'OK'
Then i start it using the command :
nameko run service --broker amqguest:guest@localhost
Finally i have a simple RPCCLient written in Java :
import com.rabbitmq.client.*;
import java.io.IOException;import java.util.UUID;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeoutException;
public class RPCClient {
private Connection connection;
private Channel channel;
private String requestQueueName = "rpc-crawler";
private String replyQueueName;
public RPCClient() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
replyQueueName = channel.queueDeclare().getQueue();
}
public String call(String message) throws IOException, InterruptedException {
String corrId = UUID.randomUUID().toString();
AMQP.BasicProperties props = new AMQP.BasicProperties
.Builder()
.correlationId(corrId)
.replyTo(replyQueueName)
.build();
channel.basicPublish("", requestQueueName, props, message.getBytes("UTF-8"));
final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1);
channel.basicConsume(replyQueueName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
if (properties.getCorrelationId().equals(corrId)) {
response.offer(new String(body, "UTF-8"));
}
}
});
return response.take();
}
public void close() throws IOException {
connection.close();
}
//...}
I call
RPCClient client = new RPCClient();
msg = client.call("crawler.scrapit()");
In my RabbitMQ management i see the message and a ack, but the client is
blocking and i do not see the returned message "OK"
Any idea Thanks a lot . Laurent