由于类似于本讨论中的原因,我正在尝试使用消息传递来代替REST,以便从一个Rails 3应用程序到另一个Rails 3应用程序进行同步RPC调用.这两个应用程序都在运行.
"server"应用程序config/initializers/amqp.rb在rubyamqp.info文档中有一个基于请求/回复模式的文件:
require "amqp"
EventMachine.next_tick do
connection = AMQP.connect ENV['CLOUDAMQP_URL'] || 'amqp://guest:guest@localhost'
channel = AMQP::Channel.new(connection)
requests_queue = channel.queue("amqpgem.examples.services.time", :exclusive => true, :auto_delete => true)
requests_queue.subscribe(:ack => true) do |metadata, payload|
puts "[requests] Got a request #{metadata.message_id}. Sending a reply..."
channel.default_exchange.publish(Time.now.to_s,
:routing_key => metadata.reply_to,
:correlation_id => metadata.message_id,
:mandatory => true)
metadata.ack
end
Signal.trap("INT") { connection.close { EventMachine.stop } }
end
Run Code Online (Sandbox Code Playgroud)
在"客户端"应用程序中,我想在视图中将同步调用的结果呈现给"服务器".我意识到这有点像amqp gem这样的固有异步库的舒适区域,但我想知道是否有办法让它工作.这是我的客户config/initializers/amqp.rb:
require 'amqp'
EventMachine.next_tick do
AMQP.connection = AMQP.connect …Run Code Online (Sandbox Code Playgroud) 我发现在经典Java死锁教程中包含对System.out.format的调用可以防止发生死锁,我无法弄清楚原因.
下面的代码与教程相同,但增加main了System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());
public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s has bowed to me!\n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s has bowed back to me!\n",
this.name, bower.getName());
}
}
public static void main(String[] args) throws InterruptedException …Run Code Online (Sandbox Code Playgroud) java ×2
amqp ×1
asynchronous ×1
clojure ×1
concurrency ×1
deadlock ×1
groovy ×1
rabbitmq ×1
scala ×1