在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
最满意答案
函数
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 executevar exec = doSomething();
You can think of that
exec
as containing the following functionfunction 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 callingdoThis()
with an argument2
which becomes the value ofa
.This is a slightly simplified version. To expand on that, the
doSomething()
function is described as closing overdoThis()
creating a closure. Conversely, the functiondoThis()
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 whydoThis()
has access to the valueb
- it's simply reachable for it. It's similar how you can dovar 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 dofoo = 100
in a different function, that will change the output ofbar()
. A closure prevents the code inside from being reachable from outside the closure.
相关问答
更多-
Javascript关闭问题(Javascript closures issues)[2022-07-25]
在第一个示例中,您在局部变量中声明数组元素的内容的副本: var item = items[i]; 正如内联注释所说,这里我们记住了闭包范围内的值。 在第二个示例中,您可以完成以下操作,而不是将i作为参数传递: (function() { var prop = i; // See here! console.log("property: " + i); // Create a nwe getter for the property that ... -
传递数组 - 范围(Passing array — scope)[2019-07-27]
你不应该像这样使用全局变量(我鼓励你阅读更多关于这个主题的内容),我不确定我理解你要做什么......但是你的问题的解决方案应该是移动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 ...
-
寻找一些js范围的黑客(Looking for some js scope hack)[2023-10-22]
不,特别是不能使用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 ...