首页 \ 问答 \ 在RabbiqMQ Direct交换中仅向绑定到路由密钥的一个队列发送消息(Deliver message to only one queue bound to a routing key in RabbiqMQ Direct exchange)

在RabbiqMQ Direct交换中仅向绑定到路由密钥的一个队列发送消息(Deliver message to only one queue bound to a routing key in RabbiqMQ Direct exchange)

我是RabbitMQ的新手,这让我很困惑。 我已经设置了直接交换,并且不同的队列在此交换机上订阅了不同的路由密钥。 我想要的是,如果使用一个路由密钥发布消息,则无论订阅该路由密钥的队列数是多少,它都只从一个订户消费。

目前的情况:

交换(类型:直接)

-QueueA1  (receives message A from exchange with routing key of "TypeA")
-QueueA2  (also receive message A from exchange with routing key of "TypeA")
-QueueB  (doesn't receive message A because it subscribes to key "TypeB")

期望:

-QueueA1  (receives message A from exchange with routing key of "TypeA")
-QueueA2  (doesn't receive message A because it's already consumed by QueueA1)
-QueueB  (doesn't receive message A because it subscribes to key "TypeB")

我需要使用不同的交易所吗? 我如何实现所需的方案?


I am new to RabbitMQ and this is confusing me. I have setup a direct exchange and different queues subscribe to different routing keys on this exchange. What I want is that if a message gets published with one routing key, it gets consumed from one subscriber only, no matter how many queues subscribe to that routing key.

Current scenario:

Exchange (type: direct)

-QueueA1  (receives message A from exchange with routing key of "TypeA")
-QueueA2  (also receive message A from exchange with routing key of "TypeA")
-QueueB  (doesn't receive message A because it subscribes to key "TypeB")

Desired:

-QueueA1  (receives message A from exchange with routing key of "TypeA")
-QueueA2  (doesn't receive message A because it's already consumed by QueueA1)
-QueueB  (doesn't receive message A because it subscribes to key "TypeB")

Do I need to use a different exchange? How do I achieve desired scenario?


原文:https://stackoverflow.com/questions/36210097
更新时间:2021-10-17 09:10

最满意答案

函数doSomething()返回另一个函数,所以当你执行

var exec = doSomething();

您可以将该exec视为包含以下功能

function doThis(a){ // <- takes an argument
    console.log(1); // comes from the b argument in the outer scope
    console.log(a); // not set yet
}

因此,当你调用exec(2)你实际上调用带有参数2 doThis() ,它变成了a的值。

这是一个稍微简化的版本。 为了扩大这一点, doSomething()函数被描述为关闭 doThis()创建闭包 。 相反,函数doThis()闭包之上或之内闭合 。 闭包本身就是一个围绕函数的有限状态:

function doSomething(){ // --> defines the closure
  var b = 1; // variable only visible within doSomething()
  return function doThis(a){ //<--> function has access to everything in doSomething(). Also defines another closure
        console.log(b); // --> accesses the OUTER scope
        console.log(a); // <-- comes from the INNER scope
  } // <-- end INNER scope
} // --> end OUTER scope

当您执行doSomething() ,返回的结果仍然保留对其中的作用域的访问权限,这就是为什么doThis()可以访问值b - 它只是可访问的。 这与你如何做相似

var foo = 40;

function bar(value) {
  return foo + value;
}

console.log(bar(2));

只有在这种情况下,任何其他代码都会访问foo因为它是一个全局变量,所以如果在不同的函数中执行foo = 100 ,则会更改bar()的输出。 闭包可防止从闭包外部到达内部的代码。


The function doSomething() returns another function so when you execute

var exec = doSomething();

You can think of that exec as containing the following function

function doThis(a){ // <- takes an argument
    console.log(1); // comes from the b argument in the outer scope
    console.log(a); // not set yet
}

Thus when you call exec(2) you are actually calling doThis() with an argument 2 which becomes the value of a.

This is a slightly simplified version. To expand on that, the doSomething() function is described as closing over doThis() creating a closure. Conversely, the function doThis() is closed over or inside a closure. The closure itself is simply a limited state around the function:

function doSomething(){ // --> defines the closure
  var b = 1; // variable only visible within doSomething()
  return function doThis(a){ //<--> function has access to everything in doSomething(). Also defines another closure
        console.log(b); // --> accesses the OUTER scope
        console.log(a); // <-- comes from the INNER scope
  } // <-- end INNER scope
} // --> end OUTER scope

When you execute doSomething() the returned result still retains access to the scope within it, this is why doThis() has access to the value b - it's simply reachable for it. It's similar how you can do

var foo = 40;

function bar(value) {
  return foo + value;
}

console.log(bar(2));

Only in this instance any other code will have acces to foo as it's a global variable, so if you do foo = 100 in a different function, that will change the output of bar(). A closure prevents the code inside from being reachable from outside the closure.

相关问答

更多
  • 在第一个示例中,您在局部变量中声明数组元素的内容的副本: var item = items[i]; 正如内联注释所说,这里我们记住了闭包范围内的值。 在第二个示例中,您可以完成以下操作,而不是将i作为参数传递: (function() { var prop = i; // See here! console.log("property: " + i); // Create a nwe getter for the property that ...
  • 你不应该像这样使用全局变量(我鼓励你阅读更多关于这个主题的内容),我不确定我理解你要做什么......但是你的问题的解决方案应该是移动basket = classPicked; 行到你的showBasket方法: basket = classPicked; //This is where the array should be passed function randOrd(){ return (Math.round(Math.random())-0.5); } function showBask ...
  • 问题是代码执行时。 执行单击时,您已经更改了其值。 以下代码将起作用。 var someString = "START"; function createCallback() { var newSomeString = new String(someString); return function() { alert(newSomeString); } } document.getElementById('elem').onclick = crea ...
  • onclick处理程序的闭包内的“i”引用捕获对“i”的实时引用。 它会针对每个循环进行更新,从而影响到目前为止所创建的所有闭包。 当while循环结束时,“i”刚刚超过提及数组的末尾,因此提到[i] ==对于所有这些都没有定义。 做这个: newAnchor.onclick = (function(idx) { return function () { loadUsernameInfo(mentions[idx]) }; })(i); 强制“我”在闭包中锁定值idx。 The "i" refer ...
  • 更推荐做类似的事情 angular.element('#fileupload').fileupload({ url: '/api/upload', dataType: 'text', done: function (e, data) { $scope.landmark.avatar = data.result; } }); 它应该工作 It's more recommended to do something like angular.ele ...
  • 它创建一个函数并立即调用它,传入window 。 该函数接收一个名为window的参数,然后在其上创建一个空对象,该对象既可以作为名为test window上的属性,也可以作为名为test的局部变量。 然后它通过new调用一个函数创建一个对象,并将该对象分配给test.utils 。 我不明白最后一部分的目的是什么(窗口); ... 它在你引用的代码中并没有真正起任何作用,因为传递给main(外部)函数window的符号与接收它的参数的名称相同。 如果他们的名字不同 ,那么它就有用了,例如: (functi ...
  • 在JavaScript中,对象引用是值。 因此,对象的行为就像它们通过引用传递一样: 如果函数更改了对象属性,则会更改原始值。 对象属性的更改在函数外部可见(反映)。 http://www.w3schools.com/js/js_function_parameters.asp In JavaScript, object references are values. Because of this, objects will behave like they are passed by reference: ...
  • 将$ scopes传递给服务听起来像是内存泄漏的秘诀。 如果没有别的,那就远远不够了。 而是考虑在每个指令中执行此操作: scope.$watch('thing', function (thing) { coolService.doCoolStuffWith(thing); } 让指令监视自己的范围,并将共享功能放在服务中。 This did it, and it allows me to set other members of the scope from within the watch: ...
  • 函数doSomething()返回另一个函数,所以当你执行 var exec = doSomething(); 您可以将该exec视为包含以下功能 function doThis(a){ // <- takes an argument console.log(1); // comes from the b argument in the outer scope console.log(a); // not set yet } 因此,当你调用exec(2)你实际上调用带有参数2 doThi ...
  • 不,特别是不能使用EcmaScript 5严格模式。 然而,hackery可以在非严格模式下成功。 以下尝试通过存储的arguments对象修改闭包内的arguments ; 适用于Firefox。 var argsave, bar; (function foo(a) { argsave = arguments; bar = function () { alert("a is now: " + a); }; } (13)); bar(); // --> a is ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。